Creating a Function Representation for Environments in SML -
okay, working on creating function representation environments in sml:
type name = string type 'a fenv = name -> 'a
i'm little stuck on how proceed this. first thing need define value fenvempty of type 'a fenv, represents empty environment (the environment not bind names). far, have this:
val fenvempty = let fun empty (name) = ??? in empty end
i'm not sure if i'm on right track or anything. supposed return function empty? putting in 0, null, or none won't work.
later, have write functions returns data associated particular name in environment (a find function), , function binds data particular name in environment ( bind function), i'm stuck on fenvempty part.
i presume teacher wants raise
exception when attempting unbound value:
type name = string type 'a fenv = name -> 'a exception unboundname of name fun fenvempty name = raise unboundname name
but think more if first try use version of fenv
returns none
unbound variables. believe because, opposed exceptions, there's more type structure program guide while writing implementation (see below). so, first try write implementation fenv
is:
type 'a fenv = name -> 'a option
now, later part of question, you'll have define function associating name
value
existing environment. function's type:
val fenvassoc : 'a fenv -> name -> 'a -> 'a fenv
notice above returns result of type 'a fenv
. remember fenv
is. it's "just" function:
type 'a fenv = name -> 'a
so, returned value, function, needs somehow "remember" name
, value
have been associated it. how functions "remember" things they've "seen" in context they've been defined?
to name's value, need third function, takes existing environment , name , returns value name, if any:
val fenvlookup : 'a fenv -> name -> 'a
again, remember type of 'a fenv
is:
type 'a fenv = name -> 'a
i'm emphasizing because type of 'a fenv
restricts in operations can apply it. can either: a) create function or b) apply one.
Comments
Post a Comment