|
Algol and ML
CptS 355 - Programming Language Design Washington State University |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Algol & MLAncient Roots
Read in the book (ch.5) how Algol influenced a long chain of languages, especially.
Pascal is interesting because for many years it was initial teaching language in many CS programs. Delphi is probably the most Pascal-like of modern industrial strength languages. ML (Meta-Language)
Interacting with ML SystemLike scheme, ML system is fundamentally based on Read-Eval-Print loop.- 3 + 4; val it = 7 : int How to think of ML
Basic types
Basic value declaration
Lists[] (the empty list) also nil [exp1, exp2,... expn] (note that all the expressions must have the same type - why?What is the type of a list?
The cons operator :: can be used to create a list. This operator appends an item to the head of an already existing list.
What is the type for []? TuplesFixed number of ordered items of possibly different types(3, true) : int * bool (fn x => x + 1, fn y => y * 1.0) : (int -> int) * (real -> real) RecordsFixed number of named values of possibly different types
{left : Tree, val : int, right : Tree}
Tuples and Records have accessor functions.
Tuple is essentially a record. But you can not write {#1=3, #2=true}
to represent
(3,true) Datatypedatatype <id> = <constructor_clause>+More generally, a datatype may be parameterized with some type variables: where a typevarlist is either a single type variable, such as 'a, or a parenthesized
list of type variables, for example ('a, 'b, 'c).
Datatypes are one of the more powerful features of the ML languages and there are many ways to use them.
datatype <id> = <id> of <type> | <id> of <type> | ...The type of Address above is union. One handy union type is the option type. Option typesdatatype intOption = SOMEINT int | NOINTThese types are often used to extend a partial function of some type to a total function with a related range type. fun mydiv (_, 0) = NOINT | mydiv (x, y) = SOMEINT(x div y) It would be tedious if we had to create such a type and name the constructors for each possible base type. Polymorphism comes to the rescue here. Data types like functions can be polymorphic. datatype 'a Option = NONE | SOME 'a; List-like typesWe have talked about list as a built-in type. Suppose it weren't and we had to build a list-like type by hand. In C you would have declared the list type as
In ML you define a list-like type by saying
As with the option type, by using polymorphism we can write a re-usable type for lists that works for any element type.
Implementation: Of course behind the scenes the language implementation is doing much the same as what you do in C.
Tree typesRecursive types are also natural for trees.datatype 'a Tree = Leaf of 'a | Interior of Tree * TreeIf you want data in the interior nodes
| Interior of 'a * Tree * Tree
If you want to name the fields
| Interior of {data:'a, left:Tree, right:Tree}
ML BookA very nice book on ML is available on-line from Prof. Robert Harper at Carnegie-Mellon University. http://www-2.cs.cmu.edu/~rwh/smlbook/offline.pdf |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||