user1313623
user1313623

Reputation: 55

Why does the below continuation function in SML not working?

   type VAR = identifier
   type code = instruction list
   type environment = identifier list
   type EXPR = environment -> code * environment

   fun ADD_OP expr expr' env = DUMMY
   fun SUB_OP expr expr' env = DUMMY
   fun MUL_OP expr expr' env = DUMMY
   fun DIV_OP expr expr' env  = DUMMY    
   fun MOD_OP expr expr' env = DUMMY
   fun NUM n env =  ([I_Int n],env)

I want to write a continuation function here. This code is a part of the larger code which is designed to execute the machine instructions. The function of this code is to translate the abstract syntax code to machine code. Below are the set of instruction of machine code, which takes two operands from the stack perform the operation and pushes result on to stack

I_Add 
I_Mod
I_Sub
I_Div

while

I_Int

Pushes the integer value on to the stack. The code will signal abort when trying to execute an instruction when it doesn't find the required operand on the stack. suppose say you are performing I_Add, then there has to be two operand on the stack otherwise it will signal abort.

till know I have only managed to write the right code for I_Int operation

fun NUM n env =  ([I_Int n],env)

This code pushes the integer n onto the stack. I know the strategy here is to first execute the "expr" and "expr'" and then add the instruction on to the list I tried to write the code for this in below way

fun ADD_OP expr expr' env = expr env 
                                (fn ((c,_),env) => expr' env
                                    (fn ((c',_),env) => ((c::c'::[I_Add]),env)))

But this results in the type mismatch error

 Error: value type in structure doesn't match signature spec
 name: ADD_OP
 spec:   ?.EXPR -> ?.EXPR -> ?.EXPR
 actual: ('a -> ('b * 'c -> Machine.Code.instruction list * 'c) -> 'd)
      -> 'e -> 'a -> 'd

Please can someone point me in the right direction on how to solve this error. I know my approach is right but I don't understand continuations. I am trying this for more than 2 days but cannot work this out. Please if someone know any tutorial on how to write contiuations in SML that would also be helpful.

Upvotes: 2

Views: 161

Answers (1)

molbdnilo
molbdnilo

Reputation: 66371

I'm not sure that this is your problem, but this won't fit in a comment and I'm unable to find a duplicate (there are a few).

Mysterious "weird" types like ?.EXPR occur when you have changed a type definition after defining a function that uses it.

Example:

- datatype None = A;;
datatype None = A
- fun f A = 3;;
val f = fn : None -> int
- f A;;
val it = 3 : int
- datatype None = A;;
datatype None = A
- f A;;
stdIn:11.1-11.4 Error: operator and operand don't agree [tycon mismatch]
  operator domain: ?.None
  operand:         None
  in expression:
    f A

Upvotes: 2

Related Questions