Title: Programming Language Concepts CIS 280
1Programming Language Concepts (CIS 280)
- Elsa L Gunter
- NJIT
- Spring 2001
2- Compiler is on the AFS system at
- /usr/local/sml/bin/sml
- A (possibly better, non-PowerPoint) text version
of this lecture can be found at - http//www.cis.njit.edu/elsa/280/SML_Lect/sml-she
ll - For the SML code for todays lecture see
- http//www.cis.njit.edu/elsa/280/SML_Lect/intro.s
ml
3WWW Addresses for SML
- http//www.cis.njit.edu/elsa/280/110-smlnj.exe
- ftp//ftp.research.bell-labs.com/dist/smlnj/releas
e/110/110-smlnj.exe - http//cm.bell-labs.com/cm/cs/what/smlnj/index.htm
l - http//cm.bell-labs.com/cm/cs/what/smlnj/doc/basis
/pages/sml-std-basis.html
4Books on SML
- Textbook, Appendix A, Section A 7
- Additional texts (not required)
- Elements of ML Programming, by Jeffrey D. Ullman,
on Prentice Hall - ML for the Working Programmer, by Lawrence C.
Paulson, on Cambridge University Press
5Session in SML
- norfolk sml
- Standard ML of New Jersey, Version 110.0.6,
October 31, 1999 CM autoload enabled - ( Read-eval-print loop expressions and
declarations ) - - 2 3
- val it 5 int
- - val test 3 lt 2
- val test false bool
6SML Expressions and Declarations
- - (
- At top-level, an expression
- ltexpgt
- is treated as an abbreviation for the
declaration - val it ltexpgt
- )
- - print "Hello world\n"
- Hello world
- val it () unit
7SML Top-level Expressions and Declarations
- - "Hi there"
- val it "Hi there" string
- - ( is string concatenation )
- - it " my good friend"
- val it "Hi there my good friend" string
8Overloading for Basic Arithmetic
- - val x 5 7
- val x 12 int
- - val y x 2
- val y 24 int
- - val z 1.35 0.23
- val z 1.58 real
9Overloading but No Coercion
- - val w y z
- stdIn51.1-51.14 Error operator and operand
don't agree tycon mismatch - operator domain int int
- operand int real
- in expression
- y z
10Using SML Code From a File
- File named test.sml contains
- 3 2
- val x 5 7
- val y x 2
- val z 1.35 0.23
- val w y z
11- - use "A\\SML_Lect\\test.sml"
- opening A\SML_Lect\test.sml
- val it 5 int
- val x 12 int
- val y 24 int
- val z 1.58 real
- A\SML_Lect\test.sml5.1-5.14 Error operator and
operand don't agree tycon mismatch - operator domain int int
- operand int real
- in expression
- y z
12Booleans (aka Truth Values)
- - true
- val it true bool
- - false
- val it false bool
- - if y gt x then 25 else 0
- val it 25 int
13Booleans
- - 3 gt 1 andalso 4 gt 6
- val it false bool
- - 3 gt 1 orelse 4 gt 6
- val it true bool
- - not (4 gt 6)
- val it true bool
14Functions
- - fun plus_two n n 2
- val plus_two fn int -gt int
- - plus_two 17
- val it 19 int
- - val plus_two fn n gt n 2
- val plus_two fn int -gt int
- - val plus_two fn n gt n 2
- val plus_two fn int -gt int
- - plus_two 14
- val it 16 int
15Values fixed at declaration time
- - val x 12
- val x 12 int
- - fun plus_x y y x
- val plus_x fn int -gt int
- - plus_x 3
- val it 15 int
- - val x 7
- val x 7 int
- - plus_x 3
- val it 15 int
16Functions as arguments and results
- - fun thrice f x f (f (f x))
- val thrice fn ('a -gt 'a) -gt 'a -gt 'a
- - thrice plus_two
- val it fn int -gt int
- - it 4
- val it 10 int
- - thrice (fn s gt "Hi! " s) "Good-bye!"
- val it "Hi! Hi! Hi! Good-bye!" string
17Recursive Functions
- - fun factorial 0 1
- factorial n n factorial (n - 1)
- val factorial fn int -gt int
- - factorial 5
- val it 120 int
- fun is needed for recursion function
declarations
18Tuples
- - val s (5,"hi",3.2)
- val s (5,"hi",3.2) int string real
- - val (a,b,c) s
- val a 5 int
- val b "hi" string
- val c 3.2 real
19Tuples
- - val d ((1,4,62),("bye",15),73.95)
- val d ((1,4,62),("bye",15),73.95) (int int
int) (string int) real - - fun fst_of_3 (x,_,_) x
- val fst_of_3 fn 'a 'b 'c -gt 'a
- - fst_of_3 s
- val it 5 int
- - fst_of_3 d
- val it (1,4,62) int int int
20Records
- - val teacher Name "Elsa L. Gunter", ss
(119,73,6244), age 102 - val teacher Name"Elsa L. Gunter",age102,ss(1
19,73,6244) - Namestring, ageint, ssint int int
- - val ss SocSec, Name elsa, age years
teacher - val elsa "Elsa L. Gunter" string
- val years 102 int
- val SocSec (119,73,6244) int int int
21Records
- - val r ss teacher
- val r (119,73,6244) int int int
- - val Name,... teacher
- val Name "Elsa L. Gunter" string
22Records
- - val q (280,student Name "Joseph
Martins", - ss (325,40,1276),
- age 19,
- instructor teacher)
23Records
- val q
- (280,
- instructorName"Elsa L. Gunter",age102,ss(
119,73,6244), - studentName"Joseph Martins",age19,ss(325,
40,1276)) - int
- instructorNamestring, ageint, ssint
int int, - studentNamestring, ageint, ssint
int int
24Tuples are Records
- - val strange (1,"f",2) 3 2, 2 "f", 1
1 - val strange true bool
- - val h 2 s
- val h "hi" string
25Records and Pattern-matching
- - ( All field names must be knowable when
pattern-matching against a record ) - - fun name Name n,... n
- stdIn94.1-94.28 Error unresolved flex record
- (can't tell what fields there are besides
Name) - - fun name Name n, age _, ss _ n
- val name fn Name'a, age'b, ss'c -gt 'a
- - name (student(2 q))
- val it "Joseph Martins" string
26Recursive Types - Lists
- - val fib5 8,5,3,2,1,1
- val fib5 8,5,3,2,1,1 int list
- - val fib6 13 fib5
- val fib6 13,8,5,3,2,1,1 int list
- - (853211) fib5
- val it true bool
- - fib5 _at_ fib6
- val it 8,5,3,2,1,1,13,8,5,3,2,1,... int list
27Functions Over Lists
- - fun double_up
- double_up (x xs) (x x double_up
xs) - val double_up fn 'a list -gt 'a list
- - val fib5_2 double_up fib5
- val fib5_2 8,8,5,5,3,3,2,2,1,1,1,1 int list
- - val silly double_up "hi", "there"
- val silly "hi","hi","there","there" string
list - - fun rev
- rev (xxs) rev xs _at_ x
- val rev fn 'a list -gt 'a list
28Functions Over Lists
- - fun map f
- map f (ht) (f h) (map f t)
- val map fn ('a -gt 'b) -gt 'a list -gt 'b list
- - map plus_two fib5
- val it 10,7,5,4,3,3 int list
- - map (fn x gt x - 1) fib6
- val it 12,7,4,2,1,0,0 int list
29Scoped Bindings
- - fun fib_seq 0 1
- fib_seq 1 1,1
- fib_seq n
- let val result as (m1m2_) fib_seq (n
- 1) - in (m1 m2)result
- end
30Scoped Bindings
- stdIn121.9-121.52 Warning binding not
exhaustive - result as m1 m2 _ ...
- val fib_seq fn int -gt int list
- - fib_seq 6
- val it 13,8,5,3,2,1,1 int list
31User-defined Datatypes
- - datatype weekday Monday Tuesday Wednesday
- Thursday Friday Saturday Sunday
- datatype weekday
- Friday Monday Saturday Sunday
Thursday Tuesday Wednesday
32Functions Over Datatypes
- - fun day_after Monday Tuesday
- day_after Tuesday Wednesday
- day_after Wednesday Thursday
- day_after Thursday Friday
- day_after Friday Saturday
- day_after Saturday Sunday
- day_after Sunday Monday
- val day_after fn weekday -gt weekday
33Functions Over Datatypes
- - fun days_later 0 day day
- days_later n day
- if n gt 0
- then day_after (days_later (n - 1) day)
- else days_later (n 7) day
- val days_later fn int -gt weekday -gt weekday
- - days_later 1 Wednesday
- val it Tuesday weekday
- - days_later 4 Monday
- val it Thursday weekday
34More Datatypes
- - datatype sum_tokens
- Int_token of int Plus Lparen Rparen
- datatype sum_tokens Int_token of int Lparen
Plus Rparen - - datatype 'a option NONE SOME of 'a
- datatype 'a option NONE SOME of 'a
35Functions Over Datatypes
- - fun first_int NONE
- first_int (Int_token n _) SOME n
- first_int (_ rest) first_int rest
- val first_int fn sum_tokens list -gt int option
36Functions Over Datatypes
- - val expression Lparen, Int_token 5, Plus,
Int_token 0, Rparen, - Plus, Int_token 3, Plus, Int_token 7
- val expression
- Lparen,Int_token 5,Plus,Int_token
0,Rparen,Plus,Int_token 3,Plus, - Int_token 7 sum_tokens list
- - first_int expression
- val it SOME 5 int option
37Functions Over Datatypes
- - fun stream_of_list list
- let val token_list ref list
- in fn () gt (case !token_list of
- gt NONE
- tok rest gt (token_list
rest SOME tok)) - end
- val stream_of_list fn 'a list -gt unit -gt 'a
option - - val token_stream stream_of_list expression
- val token_stream fn unit -gt sum_tokens option
38(No Transcript)
39Exceptions
- - exception Cant_handle of string
- exception Cant_handle of string
- - fun head raise Cant_handle "head"
- head (htl) h
- fun tail raise Cant_handle "tail"
- tail (_tl) tl
- val head fn 'a list -gt 'a
- val tail fn 'a list -gt 'a list
40Exceptions
- - fun fib_seq 0 1
- fib_seq 1 1,1
- fib_seq n
- if n lt 0 then raise Cant_handle "fib_seq"
- else
- let val result as (m1m2_) fib_seq
(n - 1) - in (m1 m2)result
- end
41Exceptions
- stdIn130.6-130.49 Warning binding not
exhaustive - result as m1 m2 _ ...
- val fib_seq fn int -gt int list
- - fib_seq (15)
- uncaught exception Cant_handle
- raised at stdIn128.25-128.46
42Exceptions
- - fib_seq (2) handle Cant_handle _ gt
- val it int list
- - fun new_fib_seq n fib_seq n handle
Cant_handle "fib_seq" gt - val new_fib_seq fn int -gt int list
- - new_fib_seq (3)
- val it int list
43Exceptions
- - exception Cant_happen
- exception Cant_happen
- - fun fib_seq 0 1
- fib_seq 1 1,1
- fib_seq n
- if n lt 0 then raise Cant_handle "fib_seq"
- else
- (case fib_seq (n - 1) of
- result as (r1 r2 _) gt r1 r2
result - _ gt raise Cant_happen)
- val fib_seq fn int -gt int list
44Side Effects and Imperative Programming
- - local
- val cell ref 0
- in
- fun get_count ()
- let val count !cell
- val _ cell count 1
- in count
- end
- end
45Side Effects and Imperative Programming
- val get_count fn unit -gt int
- - get_count()
- val it 0 int
- - get_count()
- val it 1 int
- - get_count()
- val it 2 int
- ( Example of an integer stream )
46Side Effects and Imperative Programming
- - local
- val cell ref (1)
- in
- fun get_count ()
- (cell ((!cell) 1)
- !cell)
- end
- val get_count fn unit -gt int
47Side Effects and Imperative Programming
- - get_count()
- val it 0 int
- - get_count()
- val it 1 int
- - get_count()
- val it 2 int
48Side Effects and Imperative Programming
- - local
- val cell ref (1)
- in
- fun get_count ()
- let val _ cell !cell 1
- in !cell
- end
- end
- val get_count fn unit -gt int