120 likes | 241 Views
Introduction to ML. History Special features Interacting with ML ML’s basic types ML’s composite types Math. background for type description. History. Early 1970s: Design and implementation of ML by Milner, Morris, and Wadsworth at the Univ. of Edinburgh, Scotland.
E N D
Introduction to ML • History • Special features • Interacting with ML • ML’s basic types • ML’s composite types • Math. background for type description. CSE 341 -- S. Tanimoto Introduction to ML
History Early 1970s: Design and implementation of ML by Milner, Morris, and Wadsworth at the Univ. of Edinburgh, Scotland. ML: Metalanguage of the Edinburgh LCF system. LCF = Logic for Computable Functions Gordon, Michael, Robin Milner, and Christopher Wadsworth. "Edinburgh LCF: A Mechanized Logic of Computation," Lecture Notes in Computer Science, Vol.78, Springer-Verlag, NY, 1979. Mid 1980s: Standardization of ML. CSE 341 -- S. Tanimoto Introduction to ML
Special Features A higher-order functional language. Statically type-checked, but polymorphic. Incorporates automatic type inference. Incorporates pattern matching. Garbage-collected. Has a formal semantics. CSE 341 -- S. Tanimoto Introduction to ML
Interaction with ML ML, like Lisp, provides a READ-EVAL-PRINT loop. Whenever ML prints the value of an expression, it also prints the TYPE of the value. ML keeps track of the types of variables and functions by solving systems of constraints. ML does not use assignment. On the other hand, it does use binding. Thus it uses the ‘=‘ sign in a mathematical sense. val flavor = "strawberry" ; binds a string to an identifier. CSE 341 -- S. Tanimoto Introduction to ML
Sample Interactions 3 * 5 ; val it = 8 : int real(8); val it = 8.0 : real 8.0 + real(1); val it = 9.0 : real CSE 341 -- S. Tanimoto Introduction to ML
ML’s Basic Types 3 * 5 ; val it = 8 : int 3.0 * 5.0 ; val it = 8.0 : real true ; val it = true : bool "rock" ^ "candy" ; val it = "rockcandy" : string #"a" ; val it = #"a" : char CSE 341 -- S. Tanimoto Introduction to ML
ML’s Composite Types Tuples: val mytuple = (26, "Oct." , 2001); val mytuple = (26, "Oct." , 2001) : int * string * int Each component can have a different type. Lists: [1, 2, 3] ; val it = [1, 2, 3] : int list ["nice"]; val it = ["nice"] : string list All components must have the same type. CSE 341 -- S. Tanimoto Introduction to ML
Math Background to Formal Representation of Types Set: a collection of items. Binary relation: a set of ordered pairs, whose elements come from a first set and a second set, which may be identical, partially overlapping, or disjoint. Cartesian product S1 S2 ... Sn of sets S1, S2, ..., Sn. Function: A special kind of binary relation. CSE 341 -- S. Tanimoto Introduction to ML
Set A collection of items. Must be well-defined, so that there is, in principle, a criterion that can be used to decide the membership of any item in the set. The set of all letters of the English alphabet. The set of all positive integers. NOT: The set of all sets that are not members of themselves. (SOASTANMOT). Is SOASTANMOT in SOASTANMOT? If so, then SOASTANMOT violates its own definition; if not,then it SHOULD be in SOASTANMOT. This is known as Russell’s paradox. CSE 341 -- S. Tanimoto Introduction to ML
Binary Relation A set of ordered pairs. First element comes from a set called the domain. Second element comes from a set called the codomain. D1 = {a, b, c} C1 = {0, 1} B1 = {(a, 0), (a, 1), (c, 0), (c, 1)} B1 is a binary with domain D1 and codomain C1. Often, however, the domain and codomain are identical. D2 = C2 = {a, b, c} B2 = { (a, a), (b, b), (c, c), (a, c), (c, a)} B2 is a binary relation on D2. CSE 341 -- S. Tanimoto Introduction to ML
Cartesian Product A way to combine sets to get new sets. Let S1 = {a, b, c} Let S2 = {1, 2} S1 S2 = { (a,1), (a,2), (b,1), (b,2), (c,1), (c,2) } A three-way cartesian product: S2 S2 S2 = { (1,1,1), (1,1,2), (1,2,1), (1,2,2), (2,1,1), (2,1,2), (2,2,1), (2,2,2) } This is not equivalent to (S2 S2 ) S2 or S2 (S2 S2 ). CSE 341 -- S. Tanimoto Introduction to ML
Function Function: A binary relation whose first components are from a set called the domain, and whose second components are from a set called the range, and such that each domain element is paired with one and only one range element. D = {a, b, c}; R = {0, 1} F = { (a,0), (b,0), (c,1)} is a function from D to R. F: D R B = { (a,0), (a,1), (b,0), (c,0) } is not a function. CSE 341 -- S. Tanimoto Introduction to ML