Types Polymorphic Functions
Types Polymorphic Functions
Copyright 1994 - 2005 Zhong Shao, Yale University More on ML: Page 1 of 20 Copyright 1994 - 2005 Zhong Shao, Yale University More on ML: Page 2 of 20
C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S
fun depth(LEAF _) = 1 • partial record pattern --- must fully specify the record type !
| depth(NODE(_,left,right) = 1+max(depth(left),depth(right))
val depth = fn : ’a tree -> int
type king = {name : string, born : int, crowned : int,
val t = NODE(0, LEAF 1, LEAF 2) died : int, quote : string}
val t = NODE (0,LEAF 1,LEAF 2) : int tree
Copyright 1994 - 2005 Zhong Shao, Yale University More on ML: Page 3 of 20 Copyright 1994 - 2005 Zhong Shao, Yale University More on ML: Page 4 of 20
C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S
Copyright 1994 - 2005 Zhong Shao, Yale University More on ML: Page 5 of 20 Copyright 1994 - 2005 Zhong Shao, Yale University More on ML: Page 6 of 20
C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S
type instream (* the input stream *) (* assignment operator “:=”, dereference “!” *)
type outstream (* the output stream *) let val lineNum = ref 0 (* has type int ref *)
in lineNum := !lineNum + 1;
val stdIn : instream (* the standard input stream *)
val stdOut : outstream (* the standard output stream *)
lineNum := !lineNum + 1;
val stdErr : outstream (* the standard error output stream *) lineNum
end
val openIn : string -> instream (* open a file for input *)
val openOut : string -> outstream (* open a file for output *)
val openAppend : string -> outstream (* open a file for appending*) • Assignement is different from value binding
val closeIn : instream -> unit (* close a input file *) just a value !
val closeOut : outstream -> unit (* close a output file *) local val x = 1
in fun new1() = let val x = x+1 in x end
val output : outstream * string -> unit
end
val input : instream -> string a pointer to a memory cell !
val inputLine : instream -> string local val x = ref 1
............ in fun new2() = (x := !x + 1; !x)
end
Copyright 1994 - 2005 Zhong Shao, Yale University More on ML: Page 7 of 20 Copyright 1994 - 2005 Zhong Shao, Yale University More on ML: Page 8 of 20
C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S
Copyright 1994 - 2005 Zhong Shao, Yale University More on ML: Page 9 of 20 Copyright 1994 - 2005 Zhong Shao, Yale University More on ML: Page 10 of 20
C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S
Copyright 1994 - 2005 Zhong Shao, Yale University More on ML: Page 11 of 20 Copyright 1994 - 2005 Zhong Shao, Yale University More on ML: Page 12 of 20
C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S
Copyright 1994 - 2005 Zhong Shao, Yale University More on ML: Page 13 of 20 Copyright 1994 - 2005 Zhong Shao, Yale University More on ML: Page 14 of 20
C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S
“tokens.sig” “tokens.sml”
signature Toy_TOKENS = structure Tokens : Toy_TOKENS =
sig struct
type linenum (* = int *) (* A “scaffold” structure for debugging lexers. *)
type token val makestring = Int.toString
val TYPE: linenum * linenum -> token type linenum = int
val VAR: linenum * linenum -> token type token = string
val FUNCTION: linenum * linenum -> token fun TYPE(i,j) = “TYPE “ ^ makestring(i:int)
val BREAK: linenum * linenum -> token fun VAR(i,j) = “VAR “ ^ makestring(i:int)
............ fun FUNCTION(i,j) = “FUNCTION “ ^ makestring(i:int)
val DOT: linenum * linenum -> token fun BREAK(i,j) = “BREAK “ ^ makestring(i:int)
val RBRACE: linenum * linenum -> token fun OF(i,j) = “OF “ ^ makestring(i:int)
val LBRACE: linenum * linenum -> token fun END(i,j) = “END “ ^ makestring(i:int)
val RBRACK: linenum * linenum -> token fun IN(i,j) = “IN “ ^ makestring(i:int)
val LBRACK: linenum * linenum -> token fun NIL(i,j) = “NIL “ ^ makestring(i:int)
val RPAREN: linenum * linenum -> token fun LET(i,j) = “LET “ ^ makestring(i:int)
val LPAREN: linenum * linenum -> token fun DO(i,j) = “DO “ ^ makestring(i:int)
val SEMICOLON: linenum * linenum -> token fun TO(i,j) = “TO “ ^ makestring(i:int)
val COLON: linenum * linenum -> token fun FOR(i,j) = “FOR “ ^ makestring(i:int)
val COMMA: linenum * linenum -> token ................
val STRING: (string) * linenum * linenum -> token ................
val INT: (int) * linenum * linenum -> token fun STRING(s,i,j) = “STRING(“^s^ “) “ ^ makestring(i:int)
val ID: (string) * linenum * linenum -> token fun ID(s,i,j) = “ID(“^s^“) “ ^ makestring(i:int)
val EOF: linenum * linenum -> token fun EOF(i,j) = “EOF “ ^ makestring(i:int)
end end
Copyright 1994 - 2005 Zhong Shao, Yale University More on ML: Page 15 of 20 Copyright 1994 - 2005 Zhong Shao, Yale University More on ML: Page 16 of 20
C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S
Copyright 1994 - 2005 Zhong Shao, Yale University More on ML: Page 17 of 20 Copyright 1994 - 2005 Zhong Shao, Yale University More on ML: Page 18 of 20
C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S
“driver.sml” Assignment 2
structure Parse =
struct Writing a lexical analyzer for Tiger using ML-Lex
structure Lex = Mlex
in do_it();
TextIO.closeIn file
end
end
Copyright 1994 - 2005 Zhong Shao, Yale University More on ML: Page 19 of 20 Copyright 1994 - 2005 Zhong Shao, Yale University More on ML: Page 20 of 20