{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture5.6 - DPL-5 Scheme in ML We now turn to a"large...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 10/3/06 From text by R. Norman & al. 1 DPL-5: μ Scheme in ML We now turn to a "large" program written in ML. It is NOT really large, just large enough to implement the μ Scheme interpreter. It makes use of many of the features of ML, and, since several subsequent exercises will require modifying interpreters written in ML, this seems like the least painful way to begin applying what was, supposedly, learned about ML. It should also give you more time to practice ML… 10/3/06 From text by R. Norman & al. 2 DPL-5: μ Scheme in ML To avoid having to do it later, prepare a file with contents (* Libraries that need to be loaded *) load "IO"; load "TextIO"; load "CommandLine"; load "StringCvt"; load "Int"; load "List"; load "ListPair"; Save it as modules.sml , and execute use "modules.sml"; as the first command of the mosml interpreter. Then use "mlscheme.sml"; 10/3/06 From text by R. Norman & al. 3 DPL-5: μ Scheme in ML - names and environments. What is a name ? - type name = string; > type name = string What is an environment ? - type 'a env = (name * 'a) list; > type 'a env = (string * 'a) list What is the empty environment ? - val emptyEnv = []; > val 'a emptyEnv = [] : 'a list - val _ = op emptyEnv : 'a env; 10/3/06 From text by R. Norman & al. 4 DPL-5: μ Scheme in ML - names and environments. Find a binding for a name in an environment - an environment is just a list of (name, value) two-tuples (call them pairs): - exception NotFound of name; (* what if not there? Allow for a message to be sent back *) > exn NotFound = fn : string -> exn - fun find (name, []) = raise NotFound name | find (name, (n, v)::tail) = if name = n then v else find(name, tail); > val 'a find = fn : string * (string * 'a) list -> 'a 10/3/06 From text by R. Norman & al. 5 DPL-5: μ Scheme in ML - names and environments. Install a binding for a name in an environment . - fun bind(name, v, rho) = (name, v)::rho; > val ('a, 'b) bind = fn : 'a * 'b * ('a * 'b) list -> ('a * 'b) list Install corresponding lists of names and values in an enviroment . - exception BindListLength; (* mismatch in lists *) > exn BindListLength = BindListLength : exn - fun bindList(n::vars, v::vals, rho) = bindList(vars, vals, bind(n, v, rho)) | bindList([], [], rho) = rho | bindList _ = raise BindListLength; > val ('a, 'b) bindList = fn : 'a list * 'b list * ('a * 'b) list -> ('a * 'b) list 10/3/06 From text by R. Norman & al. 6 DPL-5: μ Scheme in ML - names and environments. Examples: - val testRho = bindList(["a", "b", "c"], [1, 2, 3], []); > val testRho = [("c", 3), ("b", 2), ("a", 1)] : (string * int) list - find("a", testRho); > val it = 1 : int - find("d", testRho); ! Uncaught exception: ! NotFound "d" - bindList(["d"], [4, 5], testRho); ! Uncaught exception: ! BindListLength
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 10/3/06 From text by R. Norman & al. 7 DPL-5: μ Scheme in ML - names and environments. Our implementation is, actually, a bit different: names are bound to locations , and locations contain mutable values . So we really have both an environment and a store. We can implement the two together, since the (name, location) pairing is immutable . ML has the type 'a ref - a reference to something of type 'a . - val x = ref []; ! Warning: Value polymorphism: ! Free type variable(s) at top level in value identifier x > val x = ref [] : 'a list ref Notice 'a list ref - but there is a bit of a problem, since the type of the object is "too indeterminate". Solution: specify!
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}