Home > Back-end >  Parametric data constructor and GADTs : Not in scope data constructor
Parametric data constructor and GADTs : Not in scope data constructor

Time:11-05

I have this core Haskell file : Relation.hs
It defines some data types

{-# LANGUAGE GADTs,MultiParamTypeClasses,ScopedTypeVariables,TypeSynonymInstances,FlexibleInstances #-}

module Relation where

data SchemaField schemaField where
  SchemaField :: (Eq fieldType) => {
    name :: String,
    fieldType :: fieldType,
    nullable :: Bool
  } -> SchemaField (String,fieldType,Bool)

data ValueField valueField where
  ValueField :: (Eq valueField,Ord valueField) => valueField -> ValueField valueField

type Schema schemaField = [SchemaField schemaField]

type MaybeValueField valueField = Maybe (ValueField valueField)
type Row valueField = [MaybeValueField valueField]
type Rows valueField = [Row valueField]

data Relation schemaField valueField = Relation {
  schema :: Schema schemaField,
  rows :: Rows valueField
  }

Then, I have the test file : RelationTest.hs
It needs to use below Relation constructor :

{-# LANGUAGE MultiParamTypeClasses #-}

module RelationTest where

import Relation (FromValueToSchema,bindType,schema,rows,Relation)

data FieldType =
  StringType | IntType -- Char | Bool | Integer | Float | Double
  deriving (Show,Eq)

consistentSchema = [SchemaField "name" StringType False,SchemaField "ID" IntType True]
createdRelation = Relation {schema = consistentSchema, rows = []}

But when I compile RelationTest.hs, I have this error I do not understand : Not in scope: data constructor ‘Relation’
From understanding, Relation constructor is well defined in Relation.hs.
I can remove the details in "import Relation ..." in RelationTest.hs and it works fine.
But I would like to keep detailed import.
When this import is removed, in GHCi interpreter, I can see Relation constructor type :

*Main> import Relation
*Main Relation> :t Relation
Relation
  :: Schema schemaField
     -> Rows valueField -> Relation schemaField valueField

So, what did I miss in my detailed import ?

I check those 2 StackOverflow threads, but I did not find any solution :

CodePudding user response:

In RelationTest.hs file, you are importing specific type constructors from the Relation module using the following:

import Relation (FromValueToSchema,bindType,schema,rows,Relation)

When it comes to data types, this only imports the type constructor. The data constructor(s), possible methods and field names are not imported. Adding a (..) to the end of a type constructor or class will import all of their possible members. Like follows:

import Relation (FromValueToSchema,bindType,schema,rows,Relation(..))

Section 5.3.1 of the Haskell Report states:

Exactly which entities are to be imported can be specified in one of the following three ways:

  1. The imported entities can be specified explicitly by listing them in parentheses. Items in the list have the same form as those in export lists, except qualifiers are not permitted and the `module modid' entity is not permitted. When the (..) form of import is used for a type or class, the (..) refers to all of the constructors, methods, or field names exported from the module.
  • Related