type item = int; (* operations needed on items *) fun leq(p: item, q: item): bool = p <= q; infix leq; fun max(p,q) = if p leq q then q else p and min(p,q) = if p leq q then p else q ; datatype tree = L of item | N of item * tree * tree; val t = N(7, L 11, N(9, L 17, L 15)); fun top(L i) = i | top(N(i,_,_)) = i; fun depth(L _) = 1 | depth(N(i,l,r)) = 1 + max(depth l, depth r); depth t; fun isHeap(L _): bool = true | isHeap(N(i,l,r)) = i leq top l andalso i leq top r andalso isHeap l andalso isHeap r; val initial = 0; exception InitHeap; fun initHeap n = if n < 1 then raise InitHeap else if n = 1 then L(initial) else let val t = initHeap(n - 1) in N(initial, t, t) end; fun paranoid_initHeap n = initHeap n handle InitHeap => L(0); fun replace(i,h) = (top h, insert(i,h)) and insert(i, L _) = L(i) | insert(i, N(_,l,r)) = if i leq min(top l, top r) then N(i,l,r) else if (top l) leq (top r) then N(top l, insert(i,l), r) else (* (top r) < min(i, top l) *) N(top r, l, insert(i,r)); val (out1, t1) = replace(10,t); t; val (out2, t2) = replace(20, t1); structure C_Heap = struct type item = int; fun op leq(p: item, q: item): bool = p <= q; infix leq; fun max(p,q) = if p leq q then q else p and min(p,q) = if p leq q then p else q ; datatype tree = L of item | N of item * tree * tree; val t = N(7, L 11, N(9, L 17, L 15)); fun top(L i) = i | top(N(i,_,_)) = i; fun depth(L _) = 1 | depth(N(i,l,r)) = 1 + max(depth l, depth r); fun isHeap(L _): bool = true | isHeap(N(i,l,r)) = i leq top l andalso i leq top r andalso isHeap l andalso isHeap r; val initial = 0; exception InitHeap; fun initHeap n = if n < 1 then raise InitHeap else if n = 1 then L(initial) else let val t = initHeap(n - 1) in N(initial, t, t) end; fun replace(i,h) = (top h, insert(i,h)) and insert(i, L _) = L(i) | insert(i, N(_,l,r)) = if i leq min(top l, top r) then N(i,l,r) else if (top l) leq (top r) then N(top l, insert(i,l), r) else (* (top r) < min(i, top l) *) N(top r, l, insert(i,r)); end; (* C_Heap *) val smallHeap = C_Heap.initHeap(1); C_Heap.replace(20, smallHeap); signature C_HEAP = sig type item val leq : item * item -> bool infix leq val max : item * item -> item val min : item * item -> item datatype tree = L of item | N of item * tree * tree val t : tree val top : tree -> item val depth : tree -> item val isHeap : tree -> bool val initial : int exception InitHeap val initHeap : int -> tree val replace : item * tree -> item * tree val insert : item * tree -> tree end; (* C_HEAP *) signature HEAP = sig type item val leq : item * item -> bool type tree val top : tree -> item exception InitHeap val initHeap : int -> tree val replace : item * tree -> item * tree end; (* HEAP *) structure A_Heap : HEAP = C_Heap; signature ITEM = sig type item val leq: item * item -> bool val initial: item end; functor Heap(Item: ITEM): HEAP = struct type item = Item.item fun op leq(p: item, q: item): bool = Item.leq(p,q); infix leq; fun intmax(i: int, j: int) = if i <= j then i else j fun max(p,q) = if p leq q then q else p and min(p,q) = if p leq q then p else q ; datatype tree = L of item | N of item * tree * tree; fun top(L i) = i | top(N(i,_,_)) = i; fun depth(L _) = 1 | depth(N(i,l,r)) = 1 + intmax(depth l, depth r); fun isHeap(L _): bool = true | isHeap(N(i,l,r)) = i leq top l andalso i leq top r andalso isHeap l andalso isHeap r; exception InitHeap; fun initHeap n = if n < 1 then raise InitHeap else if n = 1 then L(Item.initial) else let val t = initHeap(n - 1) in N(Item.initial, t, t) end; fun replace(i,h) = (top h, insert(i,h)) and insert(i, L _) = L(i) | insert(i, N(_,l,r)) = if i leq min(top l, top r) then N(i,l,r) else if (top l) leq (top r) then N(top l, insert(i,l), r) else (* (top r) < min(i, top l) *) N(top r, l, insert(i,r)); end; (* Heap *) structure StringItem = struct type item = string fun op leq(i:item, j:item) = (i <= j) val initial = " " end; structure StringHeap = Heap(StringItem); val (out1, t1) = StringHeap.replace("abe", StringHeap.initHeap(1)); val (out2, t2) = StringHeap.replace("man", t1); signature OTable = sig type table exception Lookup val lookup: table * Sym.sym -> Val.value val update: table * Sym.sym * Val.value -> table end; signature TTable = sig datatype table = TBL of (Sym.sym * Val.value)list IntMap.map exception Lookup val lookup: table * Sym.sym -> Val.value val update: table * Sym.sym * Val.value -> table end; structure SymTbl: TTable = struct datatype table = TBL of (Sym.sym * Val.value)list IntMap.map exception Lookup fun find(sym,[]) = raise Lookup | find(sym,(sym',v)::rest) = if sym = sym' then v else find(sym,rest); fun lookup(TBL map, s) = let val n = Sym.hash(s) in let val l = IntMap.apply(map,n) in find(s,l) end end handle IntMap.NotFound => raise Lookup (* ... *) end; structure SmallTbl: OTable = SymTbl functor SymTblFct( structure IntMap: IntMapSig structure Val: ValSig structure Sym: SymSig): sig type table exception Lookup val lookup: table * Sym.sym -> Val.value val update: table * Sym.sym * Val.value -> table end = struct datatype table = TBL of (Sym.sym * Val.value)list IntMap.map exception Lookup fun find(sym,[]) = raise Lookup | find(sym,(sym',v)::rest) = if sym = sym' then v else find(sym,rest); fun lookup(TBL map, s) = let val n = Sym.hash(s) in let val l = IntMap.apply(map,n) in find(s,l) end in find(s,l) end handle IntMap.NotFound => raise Lookup (* ... *) end; structure MyTbl = SymTblFct(structure IntMap = FastIntMap structure Val = Data structure Sym = Identifier) signature SymTblSig = sig structure Val: ValSig structure Sym: SymSig type table exception Lookup val lookup: table * Sym.sym -> Val.value val update: table * Sym.sym * Val.value -> table end; functor SymTblFct( structure IntMap: IntMapSig structure Val': ValSig structure Sym': SymSig): SymTblSig = struct structure Val = Val' structure Sym = Sym' datatype table = TBL of (Sym.sym * Val.value)list IntMap.map exception Lookup (* ... *) end; signature LExSig = sig structure Sym : SymSig val getsym : unit -> Sym.sym end; functor ParseFct(structure SymTbl: SymTblSig structure Lex: LexSig) = struct (* ...*) let val next = Lex.getsym() in SymTbl.update(table, next, "declared") end end; functor ParseFct(structure SymTbl: SymTblSig structure Lex: LexSig sharing SymTbl.sym = Lex.Sym and type SymTbl.Val.value = string) = struct (* ...*) let val next = Lex.getsym() in SymTbl.update(table, next, "declared") end end; structure Val = ValFct() structure Sym = SymFct() structure TTable = SymTblFct(structure IntMap = IntMapFct() structure Val = Val strucutre Sym = Sym) structure Lex = LexFct(Sym) structure Parser = ParseFct(structure SymTbl = TTable structure Lex = Lex) (* format of a typical .sml file with functor definition *) use "signatures.sml"; functor ParseFct( ... (* signatures.sml file *) use "symbol.sig"; use "value.sig"; use "symtbl.sig"; use "lex.sig"; use "parse.sig"; signature BigTbl = sig include SmallTbl datatype DebugInfo = (* ... *) val printInfo : unit -> unit end; structure Parser = struct structure Lex = Lex structure MyPervasives = MyPervasives structure ErrorReports = ErrorReports structure PrintFcns = PrintFcns structure Table = Table structure BigTable = BigTable structure Aux = Aux fun f(...) = ... Table.lookup ... end; structure Parser = struct structure Lex = Lex open MyPervasives ErrorReports PrintFcns Table BigTable Aux fun f(...) = ... lookup ... end; signature S = sig type table exception Lookup val lookup: table * Identifier.sym -> Data.value val update: table * Identifier.sym * Data.value -> table end; signature S' = sig type table exception Lookup val lookup: table * string -> real val update: table * string * real -> table end; structure Stack = struct type elt = int datatype stack = ST of elt list ref val initStack = ST(ref[]) end; structure StackUser1 = struct structure Stack1 = Stack ... end; structure StackUser2 = struct structure Stack2 = Stack ... datatype stack = ST of elt list ref end; structure Stack__n1 = struct type elt__int = int datatype stack__t1 = ST of elt list ref val initStack__t1 = ST(ref[]) end; structure StackUser1__n2 = struct structure Stack1__n1 = Stack ... end; structure StackUser2__n38 = struct structure Stack2__n1 = Stack ... datatype stack__t23 = ST of elt list ref end; signature StackSig__(m1,s1,s2) = sig__m1 type elt__s1 type stack__s2 val new__(unit->s2) : unit -> stack end; signatue TranspSig__(m1,s1) = sig__m1 type elt__s1 type stack__t1 sharing type stack__t1 = Stack.stack__t1 val new__(unit->t1) : unit -> stack signature SymSig__(m1,s1) = sig__m1 eqtype sym__s1 val hash__(s1->int) : sym -> int end; signature LexSig__(m2,m1(...),n1,...) = sig__m2 structure Sym__(m1(sym__s1,hash__(s1->int))) : SymSig ... end; structure Stack__n1 = struct type elt__int = int datatype stack__t1 = ST of elt list ref fun new__(unit->t1) () = ST(ref[]) end; signature StackSigA__(m1,s1,s2) = sig__m1 type elt__s1 type stack__s2 val new__(unit->s2) : unit -> stack end; signature StackSigB__(m1,s1) = sig__m1 type elt__s1 datatype stack__t1 = ST of elt list ref sharing type stack__t1 = Stack.stack__t1 val new__(unit->t1) : unit -> stack end; structure OldStr__n4 = struct type elt__int = int val test__bool = false end; signature WrongSig__(m1,s1) = sig__m1 type elt__s1 val test__s1 : elt end; structure Tree__n1 = struct datatype 'a tree__t1 = LEAF of 'a | NODE of 'a tree * 'a tree type intTree__(int t1) = int tree fun max(a:int, b:int) = if a > b then a else b fun depth__('a t1->int) (LEAF _) = 1 | depth (NODE(left,right) = max(depth left, depth right) end; signature TreeSig__(m1,s1,s2) = sig__m1 type 'a tree__s1 type intTree__s2 fun depth__(s2->int): intTree -> int end; structure Tree: TreeSig = struct ... end; functor StackFct() = struct datatype stack = ST of int list ref val data = ST(ref []) ... end; struct Stack1 = StackFct(); struct Stack2 = StackFct(); functor StackFct() =__(m1,s1) struct__m1 datatype stack__s1 = ST of int list ref val data__s1 = ST(ref []) ... end; struct Stack1__n7(stack__t10,data__t10) = StackFct(); struct Stack2__n8(stack__t11,data__t11) = StackFct(); structure MyPervasives__n9 = struct__n9 datatype num__t12 = NUM of int ... end; functor StackFct'() =__(m2) struct__m2 structure MyPer__n9 = MyPervasives type stack__(t12 list ref) = MyPer.num list ref val data__(t12 list ref) : stack = ref [] end; struct Stack1'__n10(MyPer__n9,stack__(t12 list ref), data__(t12 list ref)) = StackFct'(); struct Stack2'__n11(MyPer__n9,stack__(t12 list ref), data__(t12 list ref)) = StackFct'(); signature SymSig__(m1,s1) = sig__m1 eqtype sym__s1 end; functor SymDir(Sym: SymSig) =__(m2,s2) struct__m2 datatype dir__s2 = DIR of Sym.sym -> int fun update ... end; structure Actual__n12 = struct type sym__string = string end; structure Result__n13(dir__t13,update__...) = SymDir(Actual) functor SymDir'(Sym: SymSig__(m1,s1)) =__(m2) struct__m2 type dir__(s1->int) = Sym.sym -> int fun update ... end; structure Result2__n14(dir__string,update__...) = SymDir'(Actual);