SML-97 Specifics - PowerPoint PPT Presentation

About This Presentation
Title:

SML-97 Specifics

Description:

SML-97 Specifics SML/NJ 110 cs7120(Prasad) L4-SML * – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 19
Provided by: wrightEdu
Learn more at: http://cecs.wright.edu
Category:
Tags: sml | specifics

less

Transcript and Presenter's Notes

Title: SML-97 Specifics


1
SML-97 Specifics
  • SML/NJ 110

2
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
Expressions
  • 1 21
  • val it 20 int
  • 1\t\na\t\n
  • tab, newline,
  • \\\ abc
  • Slash, double quotes, concatenation
  • - a
  • character
  • Z lt a
  • abd lt ac
  • 1 ltgt 2
  • 1lt2 orelse 1.0/0.0
  • if true andthen
  • not (1gt2)
  • then 3.0
  • else 3.142
  • if-then control structure is legal, but not
    expression.

4
Tip of the Type Errors
  • 1 2.0
  • No coercion
  • real(1) 2.0
  • 1 floor(2.0)
  • 1/2
  • a bb
  • if true
  • then a else bb
  • 12.0
  • real is not an equality type.
  • Portability reasons.
  • (Cf. (int-gtint) is not an equality type for
    computability reasons.)
  • In the absence of rounding errors, (rs) is
    equivalent to (rlts) andalso (sltr).

5
Identifiers
  • Alphanumeric Identifiers
  • a1_b2, B2_bomber, , etc
  • ordinary variables
  • abc, a, etc
  • type variables
  • Symbolic Identifiers
  • ltgt, _at_, ?, , /, etc
  • Separators
  • ( , ) , , , , , , . , , ,
  • ML is case-sensitive.

6
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
Patterns
  • fun merge (L as xxs, M as yys)
  • fun comb(_,0)
  • fun f ((x,y)zxs)
  • Illegal cases
  • fun length (xs _at_ x)
  • fun f (x1)
  • fun g (0.0)
  • Term Matching (cf. Unification)

8
  • fun same (n,n) 0
  • same _ 1
  • Error duplicate variable in pattern
  • ( requires equational reasoning )
  • fun fp nil 0
  • fp (xxs) 1
  • val fp fn 'a list -gt int
  • fun feq xs
  • if (xs nil) then 0 else 1
  • val feq fn ''a list -gt int

9
Match Expressions
  • val rec reverse fn
  • nil gt nil
  • xxs gt reverse(xs) _at_ x
  • rule pattern gt expression
  • case x lt y of
  • true gt x false gt y
  • if x lt y then 1 else 1
  • Error Type of rules do not agree.

10
Skip what follows
11
Limitations on the use of polymorpic functions
  • fun id x x ( id a -gt a )
  • id 0 ( id int -gt int )
  • id id 0 ( id (int -gt int) -gt (int -gt int) )
  • val id fn x gt x ( id a -gt 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).

12
  • 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 _at_ nil ( illegal )

13
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

14
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.

15
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.

16
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.

17
Examples
  • id id ( error )
  • (id, id)
  • val it ltpoly-recordgt
  • ('a -gt 'a)('b -gt 'b)
  • id (idint-gt int)
  • id, id
  • val it fn,fn ('a -gt 'a) list
  • ( Allowed by SML97 even though according
    to Ullmans book it is illegal? )

18
(contd)
  • 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
Write a Comment
User Comments (0)
About PowerShow.com