Error: unbound type constructor: TypeInteger

67 Views Asked by At

Updated Code:

    val changeTypeMapTable = 
        fn (x: TypeMapTable) =>
        fn (a: Variable, Type_Integer) =>
                (fn (y: Variable) =>
                    if y = a then InternalTypeBoolean else x(y))

But still I'm not sure how it's working? The only difference is

fn (a: Variable, b: Type_Integer) was changed to fn (a: Variable, Type_Integer)

(* Define a datatype for representing variables as strings *)
datatype Variable = GLL_Variable of string;

(* Define a datatype for variable types (boolean or integer) *)
datatype Type = TypeBoolean | TypeInteger;

(* Create a variable 'num' with the name "num" and type TypeInteger *)
val num = GLL_Variable("num");
val declaringNum = (num, TypeInteger);

(* Define a datatype for internal types: InternalNoType, InternalTypeInteger, and InternalTypeBoolean *)
datatype InternalType = InternalNoType | InternalTypeInteger | InternalTypeBoolean;

(* Define a type alias for a function mapping variables to their internal types *)
type TypeMapTable = (Variable -> InternalType);

(* Define a function 'changeTypeMapTable' that takes a TypeMapTable 'x', 
   a variable 'a' of type Variable 
   another variable 'b' of type TypeInteger, 
   and a variable 'y' of Type Variable. It checks 
   if 'y' is equal to 'a'. If yes, it returns InternalTypeInteger, 
   otherwise, it queries the 'TypeMapTable' 'x' to determine the type. *)
val changeTypeMapTable =
    fn (x: TypeMapTable) =>
        fn (a: Variable, b: TypeInteger) =>
            fn (y: Variable) =>
                if y = a then
                    InternalTypeInteger
                else
                    x(y);

In summary, this code defines a set of data types and a function to handle type mapping for variables, particularly checking whether they are of type integer or not, with the help of 'changeTypeMapTable'.

but while compiling the code, I'm getting the error Error: unbound type constructor: TypeInteger

Note: You can ignore the else part

2

There are 2 best solutions below

1
Chris On

Your problem occurs in the below declaration. Everything before this is fine.

val changeTypeMapTable =
    fn (x: TypeMapTable) =>
        fn (a: Variable, b: TypeInteger) =>
            fn (y: Variable) =>
                if y = a then
                    InternalTypeInteger
                else
                    x(y);

You have used the constructor TypeInteger as a typename in b: TypeInteger. While I'm not sure if your code would actually make sense, it would be syntactically valid to write: b: Type.

Note that b is never actually used.

You might also write this as follows with currying to achieve the same result.

fun changeTypeMapTable
    (x: TypeMapTable)
    (a: Variable, b: Type)
    (y: Variable) =
  if y = a then
    InternalTypeInteger
  else
    x(y);
0
molbdnilo On

TypeInteger is not a type, it is a value of the type Type.

From the name, I would assume that this function should take a table of Variable to Type mappings and add another mapping to it (from the "variable" a to the "type" b), and that it's supposed to look something like this:

fun toInternal TypeBoolean = InternalTypeBoolean
  | toInternal TypeInteger = InternalTypeInteger;

(* Define a function 'changeTypeMapTable' that takes a TypeMapTable 'x', 
   a pair of a variable 'a' of type Variable and a variable 'b' of type Type, 
   and a variable 'y' of Type Variable. 
   If 'y' is equal to 'a', it returns the InternalType corresponding to 'b'.
   Otherwise, it queries the 'TypeMapTable' 'x' to determine the type. *)
val changeTypeMapTable =
    fn (x: TypeMapTable) =>
        fn (a: Variable, b: Type) =>
            fn (y: Variable) =>
                if y = a then
                    toInternal b
                else
                    x(y);

Example:

- val emptyTypeTable = fn (v:Variable) => InternalNoType;
val emptyTypeTable = fn : Variable -> InternalType
- val tableWithNum = changeTypeMapTable emptyTypeTable declaringNum;
val tableWithNum = fn : Variable -> InternalType
- tableWithNum num;
val it = InternalTypeInteger : InternalType
- emptyTypeTable num;
val it = InternalNoType : InternalType
- val withBool = changeTypeMapTable tableWithNum (GLL_Variable "num", TypeBoolean);
val withBool = fn : Variable -> InternalType
- withBool num;
val it = InternalTypeBoolean : InternalType