Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cs776(Prasad)L6sml971 SML-97 Specifics SML/NJ 110.

Similar presentations


Presentation on theme: "Cs776(Prasad)L6sml971 SML-97 Specifics SML/NJ 110."— Presentation transcript:

1 cs776(Prasad)L6sml971 SML-97 Specifics SML/NJ 110

2 cs776(Prasad)L6sml972 Getting Started Invoking interpreter (actually a compiler in interactive mode!) % sml Loading ML program - use “~/eg.sml”; Accessing a structure element –Int.max; Opening a structure –open Real; Top-level environment contains a selected set of useful functions. A more complete set of standard functions is organized into structures ( Int, Real, String, etc) that forms the standard basis. - Real.Math.sqrt(4.0) - List.take(2,[1,2,3])

3 cs776(Prasad)L6sml973 Expressions –~ 1 + 21 ; val it = 20 : int; –“1\t\na\t\n”; tab, newline,… –“\\\”” ^ “abc”; Slash, double quotes, concatenation - #“a”; character –#“Z” < #“a” ; –“ab” < “acd”; –1 <> 2; –1 < 2 orelse 0/0; –if true andthen not (1>=2) then 3.0 else 3.142 ; if-then control structure is legal, but not expression.

4 cs776(Prasad)L6sml974 “Tip of the” Type Errors —1 + 2.0; coercion —real(1) + 2.0; —1 + floor(2.0); —1/2; —#“a” ^ “bb”; —if true then #“a” else “bb”; —1::[2.0]; real is not an equality type. –Portability reasons. (Cf. (int->int) is not an equality type for computability reasons.) In the absence of rounding errors, (r=s) is equivalent to (r<=s) andalso (s<=r).

5 cs776(Prasad)L6sml975 Identifiers Alphanumeric Identifiers a’1_b2, B2_bomber, …, etc –ordinary variables ’a’b’c, ’’a, etc –type variables Symbolic Identifiers <>=, @%$#, |?:&, +, /, etc Separators (, ), [,],{,}, ”,.,,, ; ML is case-sensitive.

6 cs776(Prasad)L6sml976 Variables Identifiers vs Variables vs Values Environment vs Store Adding new variable and binding val i = 10; val i = [1,2,3]; (Cf., “ := ” in Pascal and “ = ” in C.) Static scoping (Closure) val a = 2; fun ax(x) = a * x; val a = 5; ax 10; (* ax 10 = 20 *)

7 cs776(Prasad)L6sml977 Patterns fun merge (L as x::xs, M as y::ys) = … fun comb(_,0) = … fun f ((x,y)::z::xs) = … Illegal cases: fun length (xs @ [x]) = … fun f (x+1) = … fun g (0.0) = … Term Matching (cf. Unification)

8 cs776(Prasad)L6sml978 fun same (n,n) = 0 | same _ = 1; Error : duplicate variable in pattern (* requires equational reasoning *) fun fp nil = 0 | fp (x::xs) = 1; val fp = fn : 'a list -> int fun feq xs = if (xs = nil) then 0 else 1; val feq = fn : ''a list -> int

9 cs776(Prasad)L6sml979 Match Expressions val rec reverse = fn nil => nil | x::xs => reverse(xs) @ [x] ; rule : pattern => expression case x < y of true => x | false => y ; if x < y then #“1” else 1; Error: Type of rules do not agree.

10 cs776(Prasad)L6sml9710 Limitations on the use of polymorpic functions fun id x = x; (* id :’a -> ’a *) id 0; (* id : int -> int *) id id 0; (* id :(int -> int) -> (int -> int) *) val id = fn x => x; (* id :’a -> ’a *) To ensure that a bound variable has the same meaning as its binding, the variable introduced by a val- declaration is allowed to be polymorphic only if the right-hand side is a value (and not a computation).

11 cs776(Prasad)L6sml9711 val J = id id; (*illegal*) Motivation: Contexts (such as (J 5, J “a”) ) that replicate the binding may impose conflicting requirements on the type. (cf. Multiple evaluation of side-effect causing expressions.) (cf. Referential transparency violated if two occurrences of an expression is not equivalent to two occurrences of its value.) Value Restriction is too conservative. val k = nil @ nil; (* illegal *)

12 cs776(Prasad)L6sml9712 Value Restriction on Polymorphic Declaration ILLEGAL: id id; id foldr; LEGAL: fun J x = id id x; id id “a”; id Int.max; let val v = id id in v “a” end;

13 cs776(Prasad)L6sml9713 Polymorphic type variable: ’a Generalizable ( universal quantification ) for every type T, there is an instance of this object Non-generalizable ( existential quantification ) there exists a fixed but arbitrary type T for this object For compile-time guarantees, SML-97 requires that the expressions at the top- level be such that the generalizable interpretation of type variables is appropriate.

14 cs776(Prasad)L6sml9714 Non-expansive Expressions A constant or a variable is non-expansive. A function definition is non-expansive. A tuple of non-expansive expressions is non- expansive. A non-expansive expression may be preceded by a data/exception constructor. Expressions that are not of these forms are expansive and are not allowed to have type variables. –Generates a “type variable not generalizable” error.

15 cs776(Prasad)L6sml9715 Non-generalizable type variable error All the following conditions must be met for the error to occur: –The expression is at the top-level. –The type of the expression involves at least one type variable. –The form of the expression does not meet the conditions for it to be non-expansive.

16 cs776(Prasad)L6sml9716 Examples —id id ; (* error *) —(id, id) ; val it = : ('a -> 'a)*('b -> 'b) —id (id: int-> int ) ; —[id, id] val it = [fn,fn] : ('a -> 'a) list (* Allowed by SML97 even though according to Ullman’s book it is illegal? *)

17 cs776(Prasad)L6sml9717 (cont’d) —let val x = (id id) in x(1) end; (* Legal. (id id) is not top-level. *) —let val x = (id id) in (x 1, x “a”) end; (* Error: operator and operand don't agree.*) —(id 1, id “a”) ; (* val it = (1,"a") : int * string *) —let val x = id in (x 1, x “a”) end;


Download ppt "Cs776(Prasad)L6sml971 SML-97 Specifics SML/NJ 110."

Similar presentations


Ads by Google