(* Programming Languages (F28PL) lecture transcript - 28th September at 15:15 *) (* true and false are two members of bool datatype *) true; false; (); (* A unit *) (* = operator has type ''a * ''a -> bool, ''a is alpha primed *) op =; true = true; (* returns true because bool is an equality type and also because as a datum it is equal to itself *) 5 = 5; 5 = 4; 5.0 = 4.0; (* Type error, because real is not an equality type *) (5,"4"); (* has type int * string *) (5,"4") = (7, "4"); (* is an equality type, returns false *) (5.0,"4") = (7, "4"); (* type error because of the reals *) (5,"4.0") = (7,"4.0"); (* no equality type error as there are no reals *) (fn x => x) = (fn x => x); (* any function type is not an equality type *) (fn (x:int) => x) = (fn (x:int) => x); (fn (x:bool) => x) = (fn (x:bool) => x); (* Both still gives a type error *) (fn (x:unit) => x) = (fn (x:unit) => x); (* ML will still not allow you to compare them for equality *) (* Extentional equality function for bools *) fun exteq f g = f true = g true andalso f false = g false; (* Extentional equality function for ints *) fun exteqn f g n = (f n = g n) andalso (exteqn f g (n+1)); fun exteq f g = exteqn f g 0; (* starter function *) (* Two functions which we can try to check for equality *) fun f1 10000 = 0 | f1 _ = 1; fun f2 x = 1; exteq f1 f2; exteq f2 f2; (* takes forever *) fun exteqn f g n = (exteqn f g (n+1)) andalso (f n = g n); (* This function will always never end no matter if it finds the equality or not *) exteq f1 f2; (* exteq is still referring the old exteqn, so this will end *) fun exteq f g = exteqn f g 0; (* We overridden the exteq with the new exteqn function *) exteq f1 f2; (* now this will take forever *) (* For division of integers *) div; (* For division of reals we use / operator *) 5.0 / 4.3; op /; false andalso (5 div 0 = 4); (* doesn't throw an exception *) (5 div 0 = 4) andalso false; (* will throw an exception *) (* Tuples can have different types of elements *) (1, "one"); [1, "one"]; (* Raises an error as you cannot have different types in a list *) [1,2]; (* Is okay because all the elements have the same type (int) *) [fn x => x, fn x => x]; (* ('a -> 'a) list *) [fn x => x, fn x => x*x]; (* It will assume type int -> int for all the functions *) [fn x => x andalso x, fn x => x*x]; (* First function has type bool -> bool, and second function type is int -> int which will raise an error *) 5.0 = 5.0; (* You cannot check reals for equality *) 5.0 >= 5.0; (* Returns true *) (* Our real equality function *) fun fakeeq r1 (r2:real) = r1 >= r2 andalso r1 <= r2; fakeeq 5.0 5.0; (* A type alias for string *) type name = string; "Jamie"; (* will return a string *) "Jamie":name; (* We specify the type *) val x = "Jamie":name; x^" Jim"; (* This will be of type string *)