


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A quick reference for the standard ml (sml) programming language, covering its basic properties, interacting with the sml interactive environment, built-in types, type system, and recursion. It also includes information on user-defined types and expression syntax.
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



The Standard ML^2 interactive environment is installed at /tools/cs/smlnj/bin/sml on CSEL machines. You can interact with it as follows: X; Evaluate expression X and infer its type (note the semicolon) use("file.sml ") Evaluate the contents of file.sml as if they had been typed in, and list all newly defined entities by name and type it This expression contains the result of the previous computation 〈Ctrl 〉 − 〈C 〉 Abort computation/input completion (“=” prompt), return to regular (“-”) prompt 〈Ctrl 〉 − 〈D 〉 Quit SML, only works from the regular (“-”) prompt
The following is only a selection of the most important types. A full listing is available from the http://www.smlnj.org website. Type name Values of this type Description unit () The type with only one element int ~1073741824... 1073741823 Integers (~1 denotes −1) bool true; false Booleans char #"A"; #"1"; #"\001" Characters string ""; "a"; "foo"; "\t---\n" Character strings ’a list nil; []; [1]; [true,false,true] Polymorphic lists
SML uses type judgements to tell the types of things: If it says x : T , then x is of type T. These judgements can be specified by programmers; in that case, they are called type annotations. Judgement Description Requirements c : T Literal value iff c is a value of T. (x 1 ,... , xn) : T 1 ∗... ∗ Tn Tuple construction iff, for all i ∈ { 1... n}, xi : Ti. (fn x => y) : T → U Function construction iff x : T and y : U. (f g) : U Function application iff f : T → U and g : T. (^1) Copyright ©c2003 Programming Languages Group, CU Boulder
e-mail: [email protected] (^2) See http://www.smlnj.org for the full distribution and manuals.
name type semantics ~ int → int Negation abs int → int Absolute value (div) int ∗ int → int Integer division (mod) int ∗ int → int Modulo (*) int ∗ int → int Multiplication (+) int ∗ int → int Addition (-) int ∗ int → int Subtraction (<) int ∗ int → bool Less-than operator (>) int ∗ int → bool Greater-than operator (<=) int ∗ int → bool Less-than-or-equal operator (>=) int ∗ int → bool Greater-than-or-equal operator (=) int ∗ int → bool Equality test
name type semantics not bool → bool Negates a boolean value (=) bool ∗ bool → bool Equality test
name type semantics ord char → int Maps a character to its ASCII value chr int → char Interprets a number as an ASCII character (=) char ∗ char → bool Equality test
name type semantics explode string → char list Turns a string into a list of characters implode char list → string Concatenates characters in a list into a string size string → int Determines the length of a string (^) string ∗ string → string Concatenates two strings (=) string ∗ string → bool Equality test (by value)
name type semantics nil ’a list Same as [] (the empty list) hd ’a list → ’a Returns the head (first element) of a list, raises an exception on the empty list tl ’a list → ’a Returns the tail of a list (all elements but the first one), raises exception on the empty list length ’a list → int Determines the length of a list map (’a → ’b) → ’a list → ’b list Applies a function to all elements in a list rev ’a list → ’a list Reverses a list (@) (’a list ∗ ’a list) → ’a list Concatenates two lists (=) string ∗ string → bool Equality test (by value) For ’a and ’b, you can substitute any type.
〈Expr〉 −→ 〈Literal〉 Denotes the literal value 〈Literal〉 of one of the built- in types. | ( 〈Expr〉 1 ,... , 〈Expr〉n) Denotes a tuple of n expressions. | 〈Expr〉 〈Op〉 〈Expr〉 Infix operator/constructor application of 〈Op〉. | 〈Expr〉 1 〈Expr〉 2 Function application of 〈Expr〉 1 to 〈Expr〉 2. | let 〈DLst〉 in 〈Expr〉 end Evaluates to whatever 〈Expr〉 evaluates if all defini- tions in 〈DList〉 (temporarily) hold. | (〈Expr〉 1 ;...; 〈Expr〉n) Computes all contained expressions in ascending se- quence, but evaluates to 〈Expr〉n. | case 〈Expr〉 of 〈Optns〉 Matches the value of 〈Expr〉 to one of the patterns in 〈Optns〉 and selects the corresponding branch. | fn 〈Optns〉 Denotes a function which evaluates to an expression matching some pattern within 〈Optns〉.
〈DLst〉 −→ ε | 〈Decl〉 〈DLst〉
〈Decl〉 −→ val 〈Pat〉 = 〈Expr〉 Introduce global name(s), set to the result of the evaluation of 〈Expr〉. | fun 〈NmOpts〉 Syntactic sugar for val 〈Name〉 = fn 〈Opts〉. Also allows recursion.
〈NmOpts〉 −→ 〈NmOpt〉 | 〈NmOpt〉 | 〈NmOpts〉 A sequence of options with function names. All func- tion names must be the same.
〈NmOpt〉 −→ 〈Name〉 〈Pat〉 〈TAnn〉 = 〈Expr〉 If pattern 〈Pat〉 is matched, 〈Expr〉 is executed. The type annotation 〈TAnn〉 is optional.
〈Pat〉 −→ 〈Name〉 〈TAnn〉 A pattern can be a simple name. | (〈Pat〉,... , 〈Pat〉) 〈TAnn〉 A tuple pattern construction. | 〈Name〉 〈Pat〉 〈TAnn〉 Where 〈Name〉 is a constructor. | 〈Literal〉 〈TAnn〉 Literal values can form patterns. | 〈TAnn〉 The wildcard pattern
〈TAnn〉 −→ ε | : 〈Type〉 Optional type annotation
〈Type〉 −→ 〈Name〉 Any of the built-in types (int, string etc.) or any user-defined type. | 〈Type〉 * 〈Type〉 Tuple construction. | 〈Type〉 -> 〈Type〉 Function construction.
〈Optns〉 −→ 〈Optn〉 | 〈Optn〉 | 〈Optns〉 One or more options, separated by bars.
〈Optn〉 −→ 〈Pat〉 => 〈Expr〉 Evaluates to 〈Expr〉 iff the input matches 〈Pat〉 and no previous pattern was matched.
〈Name〉 −→ a | b |... Any name, except for the names of operators (such as o).