Transcript ML1

Introduction to ML



You will be responsible for learning ML
on your own.
Today I will cover some basics
Read Robert Harper’s notes on “an
introduction to ML”

See course webpage for pointers and info
about how to get the software
Intro to ML

Highlights

Functional Language


Functions are pervasive:
First-class values


Strongly-typed language




Storable, arguments, results, nested
Every expression has a type
Certain errors cannot occur
Polymorphic types provide flexibility
Flexible Module System


Abstract Types
Higher-order modules (functors)

Interactive Language



Statically Scoped Language


Compile-time resolution of values
Call-by-value language


Type in expressions
Evaluate and print type and result
f(e)
High-level programming features




Data types
Pattern matching
Exceptions
Mutable data discouraged
Preliminaries

Read – Eval – Print – Loop
- 3 + 2;
> 5: int
- it + 7;
> 12 : int
- it – 3;
> 9 : int
- 4 + true;
Type clash in : 3 + true
Looking for a : int
I have found a : bool
- 3 div 0
Failure : Div
Type
Error
- run-time error
Basic Values
- ();
> () : unit
=> like “void” in c (sort of)
=> the uninteresting value/type
- true;
> : bool
- False;
> : bool
- if it then 3+2 else 7;
> 7 : int
- false and also loop_Forever;
> False : bool
always necessary
and also, or else short-circuit eval
Basic Values
Integers
-3 + 2j
> 5:int
- 3 + (if not true then 5 else 7);
> 10 : int
Strings
- “Dave” ^ “ “ ^ “Walker”;
> “Dave Walker”:string
- size “foo”;
> 3 : int
Reals
- 3.14;
> 3.14 : real
No division between expressions
and statements
Structured Values
Tuples
>
>
(true, 17, “Stuff”);
(true, 17, “Stuff”) : bool * int * string
(if 3 < 2 then “x” else “y”, false);
(“y”, false) : string * bool
Records
>
>
-
{name = “Dave”, ssn = 332177}
_____________ : {name = string, ssn : int}
#name it ;
“Dave” : string
#1 (true, False);
true : bool
Parametric Polymorphism






Functions like compose work on objects of
many different types
In ML, we write
compose: (‘a -> ‘b) ->
(‘c -> ‘a) ->
type variables are written with ‘
(‘c -> ‘b)
compose not :
compose not not :
compose (op + 1) not :
compose (fn x => x) :
What is the type of compose?

Fun compose =
Fn f => (fn g => Fn x => f (g x))
T3
Compose : T2 -> (T2 -> (T5 -> T6)
: T2 -> (( T7 -> T8) -> (T5 -> T6))
: (T9 -> TA) -> (T7 -> T9) -> (T7 -> TA)
: (‘a -> ‘b) -> (‘c -> ‘a) -> (‘a -> ‘c)
Compose
(fn x => x + 1)
(fn y => y * 3) 7;
Declarations
>
>
>
>
>
>
val x = 4 * 5;
Declare new name x and bind it to value
val x = 20 : int
val y = x + 1;
val y = 21 : int
val x = x + y;
reuse name
val x = 41 : int
x;
41 : int
static scoping
y;
local declaration
21 : int
scope
(let val x = 10 in (x + x, y + x) and, x);
((20, 31), 41) : (int * int) * int
Functions (the good stuff)
- not;
functions are values
> Not = fn : bool -> bool
we can’t write out a function so ML just says this
- not true;
function application is a space
> False : bool
- fun twice x = 2 * x
> Val twice = fn : int -> int
- fun fact x = if x = 0 then 1 else (fact (x-1)) * x
- fact (twice (twice 3));
functions
- fn s => x ^ “!”;
> Fn : string -> string
- val exclaim =
fn : => s ^ “!”
> …
- val compose =
fn f =>
fn g =>
an anonymous function like writing
-3
rather than - val x = 3;
fn x => f(g x);
> …
- ((compose twice) twice) 3;
> 12
- four = compose twice twice
> …
- four 7;
> …