2

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
  • The comment is a bit confusing, as TypeInteger is not a type. It also doesn't mention 'b' anywhere, only 'a', but a Variable does not have a Type... I would expect something more like datatype Type = TypeBoolean | TypeInteger; datatype Variable = Variable of string * Type; ... Commented Oct 13, 2023 at 6:53
  • I updated the comment, hope it's more clear now @molbdnilo Commented Oct 20, 2023 at 4:52

2 Answers 2

0

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);
Sign up to request clarification or add additional context in comments.

1 Comment

My requirement is to use the same pattern that I defined in the question. I have put the updated code as well in the question. If you can tell me how it's working? That would be helpful @Chris
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.