Type Definitions - PowerPoint PPT Presentation

About This Presentation
Title:

Type Definitions

Description:

Type Definitions cs7120 (prasad) L7-TDEF* – PowerPoint PPT presentation

Number of Views:72
Avg rating:3.0/5.0
Slides: 19
Provided by: TK166
Learn more at: http://cecs.wright.edu
Category:

less

Transcript and Presenter's Notes

Title: Type Definitions


1
Type Definitions
2
Concrete Types
  • Primitive types ( int, bool, char, string, etc )
  • Type constructors ( -gt, 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
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
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
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 -gt directions
  • fun isEast x (x East)

6
  • 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
  • (treeinttree)
  • E.g.,
  • node (leaf 25,10,leaf 20) tree

7
Defining Functions
  • fun add (Zero, n) n
  • add (Succ(m),n) Succ(add(m,n))
  • val add fn number number -gt number
  • fun mul (Zero, n) Zero
  • mul (Succ(m),n) add(n,mul(m,n))
  • val mul fn number number -gt number
  • mul ( Succ(Succ Zero), Succ Zero )
  • Succ(Succ Zero)
  • ( Expression evaluation - Normalization )

8
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
Semantics of Concrete Types
  • Algebra (Values, Operations)
  • E.g, vector/matrix algebra, group theory, etc.
  • 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 tt

10
  • datatype t e u of t p of tt
  • Signatures
  • e t
  • u t -gt t
  • p tt -gt t
  • What are the values in the type?
  • What are the operations on the type?

11
Values
  • 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
Operations
  • u (function from terms to terms)
  • e gt u(e)
  • u(e) gt u(u(e))
  • p (function from termsterms to terms)
  • e, e gt p(e,e)
  • u(e),e gt p(u(e),e)
  • p(e,e),e gt p(p(e,e),e)
  • Semantics of t
  • (e,u(e),p(e,e),, u,p)

13
Abstract Type
  • Specification and Implementation through Examples

14
Integer Sets Algebraic specification
  • empty intset
  • insert intset -gt int -gt intset
  • remove intset -gt int -gt intset
  • member intset -gt int -gt bool
  • for all s e intset, m,n e int
  • member empty n false
  • member (insert s m) n
  • (nm) orelse (member s n)
  • remove empty n empty
  • remove (insert s m) n
  • if (nm) then remove s n
  • else insert (remove s n) m

15
  • abstype
  • intset Empty Insert of intsetint
  • with
  • val empty Empty
  • fun insert s n Insert(s,n)
  • fun member Empty n false
  • member (Insert(s,m)) n
  • (nm) orelse (member s n)
  • fun remove Empty n Empty
  • remove (Insert(s,m)) n
  • if (nm) then remove s n
  • else Insert(remove s n, m)
  • end

16
  • 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
  • abstype
  • intset Set of int list
  • with
  • val empty Set
  • fun insert (Set s) n Set(ns)
  • fun member (Set s) n
  • List.exists (fn i gt (in)) s
  • fun remove (Set s) n
  • Set (List.filter (fn i gt (in)) s)
  • end
  • ( member and remove are not primitives in
    structure List because they are defined only
    for equality types. )

18
  • abstype
  • intset Set of (int -gt bool)
  • with
  • val empty
  • Set (fn n gt false)
  • fun insert (Set s) n
  • Set (fn m gt (m n) orelse (s m))
  • fun member (Set s) n (s n)
  • fun remove (Set s) n
  • Set (fn m gt (not(m n)) andalso (s m))
  • end
Write a Comment
User Comments (0)
About PowerShow.com