Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cs7120 (prasad)L7-TDEF1 Type Definitions. cs7120 (prasad)L7-TDEF2 Concrete Types Primitive types ( int, bool, char, string, etc ) Type constructors (

Similar presentations


Presentation on theme: "Cs7120 (prasad)L7-TDEF1 Type Definitions. cs7120 (prasad)L7-TDEF2 Concrete Types Primitive types ( int, bool, char, string, etc ) Type constructors ("— Presentation transcript:

1 cs7120 (prasad)L7-TDEF1 Type Definitions

2 cs7120 (prasad)L7-TDEF2 Concrete Types Primitive types ( int, bool, char, string, etc ) Type constructors ( ->, list, etc ) Real world concepts can be modeled better with concrete types rather than simulated using primitive types. datatype decision = (yes,no,maybe); –Readability (self-documenting) –Reliability (automatic detection of errors) operations on values of a type are independent of the operations on their representations.

3 cs7120 (prasad)L7-TDEF3 Language Aspects Naming the new type Constructing values of the type Inspecting values and testing their type for defining functions Canonical representation (for equality tests) In ML, one can use symbolic terms to construct values in a type, and use patterns to define functions on this type.

4 cs7120 (prasad)L7-TDEF4 Introducing Type Names type pair = int * int; type points = int list; _ Type Equivalence Structural Equivalence - val x = (1,2) : pair; - (1,1) : int * int; - x = (1,1); (* val it = false : bool *)

5 cs7120 (prasad)L7-TDEF5 Enumerated types with constants datatype directions = North | South | East | West ; fun reverse North = South | reverse South = North | reverse East = West | reverse West = East ; val reverse = fn : directions -> directions fun isEast x = (x = East);

6 cs7120 (prasad)L7-TDEF6 Constructors with Arguments datatype phy_quant = Pressure of real | Volume of real ; datatype position = Coord of int * int; (Tagged values) (Cf. Ada Derived Types) Recursive Types datatype number = Zero | Succ of number; datatype tree = leaf of int | node of (tree*int*tree); E.g., node (leaf 25,10,leaf 20) : tree;

7 cs7120 (prasad)L7-TDEF7 Defining Functions fun add (Zero, n) = n | add (Succ(m),n) = Succ(add(m,n)); val add = fn : number * number -> number fun mul (Zero, n) = Zero | mul (Succ(m),n) = add(n,mul(m,n)); val mul = fn : number * number -> number mul ( Succ(Succ Zero), Succ Zero ) = Succ(Succ Zero); (* Expression evaluation - Normalization *)

8 cs7120 (prasad)L7-TDEF8 New Type Operators datatype ’e matrix = Matrix of (’e list list); datatype 'a matrix = Matrix of 'a list list datatype ’a list = nil | :: of (’a * ’a list); datatype 'a list = :: of 'a * 'a list | nil (* Using ‘a instead of ’a generates errors. *) (* Using [] instead of nil generates an error. *)

9 cs7120 (prasad)L7-TDEF9 Semantics of Concrete Types Algebra = (Values, Operations) E.g, vector/matrix algebra, group theory, etc. Free Algebra Construction Free Algebra Construction is a way of defining an algebra from a collection of CONSTRUCTORS using SIGNATURE information. E.g., datatype t = e | u of t | p of t*t;

10 cs7120 (prasad)L7-TDEF10 datatype t = e | u of t | p of t*t; Signatures e : t u : t -> t p : t*t -> t What are the values in the type? What are the operations on the type?

11 cs7120 (prasad)L7-TDEF11 Values ee u(e), p(e,e) u(u(e)), u(p(e,e)), p(e,u(e)), p(e,p(e,e)), p(u(e),e), p(p(e,e),e), … ... (* Unique name hypothesis *) Grammar V := e | u (V) | p (V,V)

12 cs7120 (prasad)L7-TDEF12 Operations u (“function from terms to terms”) e  u(e) u(e)  u(u(e)) … p (“function from terms*terms to terms”) e, e  p(e,e) u(e),e  p(u(e),e) p(e,e),e  p(p(e,e),e) … Semantics of t ({ e,u(e),p(e,e),… }, { u,p })

13 cs7120 (prasad)L7-TDEF13 Abstract Type Specification and Implementation through Examples

14 cs7120 (prasad)L7-TDEF14 Integer Sets: Algebraic specification empty : intset insert : intset -> int -> intset remove : intset -> int -> intset member : intset -> int -> bool for all s  intset, m,n  int : member empty n = false member (insert s m) n = (n=m) orelse (member s n) remove empty n = empty remove (insert s m) n = if (n=m) then remove s n else insert (remove s n) m

15 cs7120 (prasad)L7-TDEF15 abstype intset = Empty | Insert of intset*int with val empty = Empty fun insert s n = Insert(s,n) fun member Empty n = false | member (Insert(s,m)) n = (n=m) orelse (member s n) fun remove Empty n = Empty | remove (Insert(s,m)) n = if (n=m) then remove s n else Insert(remove s n, m) end;

16 cs7120 (prasad)L7-TDEF16 val s1 = (insert empty 5); val s2 = (insert s1 3); val s1 = (insert s2 8); (member s1 8); (member s1 5); (member s1 1); val s3 = (remove s1 5); (member s3 5); (member s1 5);

17 cs7120 (prasad)L7-TDEF17 abstype intset = Set of int list with val empty = Set [] fun insert (Set s) n = Set(n::s) fun member (Set s) n = List.exists (fn i => (i=n)) s fun remove (Set s) n = Set (List.filter (fn i => (i=n)) s) end; (* member and remove are not primitives in structure List because they are defined only for equality types. *)

18 cs7120 (prasad)L7-TDEF18 abstype intset = Set of (int -> bool) with val empty = Set (fn n => false) fun insert (Set s) n = Set (fn m => (m = n) orelse (s m)) fun member (Set s) n = (s n) fun remove (Set s) n = Set (fn m => (not(m = n)) andalso (s m)) end;


Download ppt "Cs7120 (prasad)L7-TDEF1 Type Definitions. cs7120 (prasad)L7-TDEF2 Concrete Types Primitive types ( int, bool, char, string, etc ) Type constructors ("

Similar presentations


Ads by Google