smlmlmosml

How to create an empty environment represented by a function


I came across this problem online which I found interesting, it had a decent explanation but I was confused about the solution. So given

type 'a fenv = name -> 'a 

create a value of type

'a fenv

which would be our empty env. I thought this to be something along the following

exception NotFound of name
val empty = fn name => raise NotFound name

Where it might return a Notfound name exception but I may be doing this wrong because I continue to get

Type clash: expression of type
   'a alist -> 'a alist alist
cannot have type
   'a alist alist
Toplevel input:
val (_: 'a fenv) = empty
Unbound type constructor: fenv

Sorry if this is simple still new at sml could someone explain how I would go about getting the solution?

Thanks


Solution

  • How to create an empty environment represented by a function

    You could do:

    type name = string
    exception NotFound of name
    fun empty name = raise NotFound name
    

    I may be doing this wrong because I continue to get

    Type clash: expression of type
       'a alist -> 'a alist alist
    cannot have type
       'a alist alist
    

    You do realize that your code mentions nothing of 'a alist, right? You're probably either using a REPL that has an outdated definition for type 'a fenv, or your file contains multiple definitions, one involving this 'a alist and another containing this 'a fenv.