Type Definitions cs776 (prasad) L8tdef.

Slides:



Advertisements
Similar presentations
Programming with Lists
Advertisements

Modern Programming Languages, 2nd ed.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists A list is a finite sequence of elements. [3,5,9] ["a", "list" ] [] Elements may appear more than once [3,4]
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
0 PROGRAMMING IN HASKELL Chapter 10 - Declaring Types and Classes.
String is a synonym for the type [Char].
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
CS 2104 : Prog. Lang. Concepts. Functional Programming I Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
CSE-321 Programming Languages Introduction to Functional Programming (Part II) POSTECH March 13, 2006 박성우.
Cs7100(Prasad)L4-5ADT1 Specification and Implementation of Abstract Data Types.
Chapter 9: Functional Programming in a Typed Language.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Types and Classes Dr. Hyunyoung Lee.
Cs7120 (prasad)L7-TDEF1 Type Definitions. cs7120 (prasad)L7-TDEF2 Concrete Types Primitive types ( int, bool, char, string, etc ) Type constructors (
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
Ceg860 (Prasad)LADT1 Specification and Implementation of Abstract Data Types Algebraic Techniques.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Chapter SevenModern Programming Languages1 A Second Look At ML.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
Cs776(Prasad)L112Modules1 Modules value : type : function :: structure : signature : functor.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
Cs776(Prasad)L6sml971 SML-97 Specifics SML/NJ 110.
Cs776 (Prasad)L2HOF1 Higher-Order Functions. cs776 (Prasad)L2HOF2 Higher-Order Functions A function that takes a function as argument and/or returns a.
Abstract Syntax cs7100 (Prasad) L7AST.
String is a synonym for the type [Char].
Type Checking and Type Inference
Programming Languages and Compilers (CS 421)
Principles of programming languages 12: Functional programming
ML: a quasi-functional language with strong typing
More important details More fun Part 3
CS 326 Programming Languages, Concepts and Implementation
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2017.
PROGRAMMING IN HASKELL
Abstract Syntax Prabhaker Mateti 1.
Functions, Patterns and Datatypes
CSE 341 Section 5 Winter 2018.
ML’s Type Inference and Polymorphism
Types and Classes in Haskell
Statement-Level Control Structures
PROGRAMMING IN HASKELL
Abstract Syntax cs7100 (Prasad) L7AST.
Functions, Patterns and Datatypes
ML’s Type Inference and Polymorphism
CSE-321 Programming Languages Introduction to Functional Programming
Chapter 7 Expressions and Assignment Statements.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2016.
Functions, Patterns and Datatypes
CSE-321 Programming Languages Introduction to Functional Programming
Sub-system interfaces
CS 242 Types John Mitchell.
CS 242 Types John Mitchell Reading: Chapter 6.
ML’s Type Inference and Polymorphism
ML’s Type Inference and Polymorphism
Functions, Patterns and Datatypes
Presentation transcript:

Type Definitions cs776 (prasad) L8tdef

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. Certain errors cannot be mechanically detected if directions are simulated using integers. Encoding of days of a week as numbers spelt out in comments in the absence of concrete types. Guard against addition of days of a week…. (Enumerated/Scalar types) cs776 (prasad) L8tdef

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. cs776 (prasad) L8tdef

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 *) Cf. C++ typedef cs776 (prasad) L8tdef

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); In contrast with Pascal and Ada, relational operations such as “<“ are not defined on enumerated types. (very restricted overloads) The type contains a finite set of values. datatype mine = a | false; // legal true : boolean; // bool constants are not reserved. false : mine; // predefined constant got redefined!! cs776 (prasad) L8tdef

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; Similarly, tagging with units (centigrade, farenheit, absolute, …), or with their logical role. Language provides mechanisms to make explicit programmer intent, to enable mechanical checking for reliability. Primitive type : Finite type : Built on infinite primitive type : New infinite type cs776 (prasad) L8tdef

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 *) Structural induction : completeness in the definition cs776 (prasad) L8tdef

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. *) Beware of errors in patterns containing symbolic terms 1. A typo in constructor name may lead to an interpretation of a catch-all variable. 2. A missing blank separator between a unary constructor and its argument (possibly wild card) may be misinterpreted as a variable name. 3. Ambiguity in dangling case clauses for nested cases can be resolved using parenthesis. 4. Sometimes compiler warnings such as “unreachable clauses” or “incomplete matches” may flag such problems. cs776 (prasad) L8tdef

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 t*t; cs776 (prasad) L8tdef

What are the values in the type? What are the operations on the type? 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? cs776 (prasad) L8tdef

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) cs776 (prasad) L8tdef

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}) cs776 (prasad) L8tdef

Specification and Implementation through Examples Abstract Type Specification and Implementation through Examples Not equality types. cs776 (prasad) L8tdef

Integer Sets: Algebraic specification empty : intset insert : intset -> int -> intset remove : intset -> int -> intset member : intset -> int -> bool for all s e intset, m,n e 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 Multiple Implementations: 1. Same representation different data invariants a. multiplicity allowed, unordered b. single entries, unordered. c. single entries, ordered 2. Different representation : function … (Finite (limited vs unbound) vs infinite sets) 3. Type parameterization cs776 (prasad) L8tdef

intset = Empty | Insert of intset*int with val empty = Empty 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; Literal translation of the ADT spec. Representation in terms of term trees. Uninterpreted symbols. (Duplication present - Unordered.) Generalizable to equality types. type intset val empty = - : intset val insert = fn : intset -> int -> intset val member = fn : intset -> int -> bool val remove = fn : intset -> int -> intset Difference between algebraic spec and the abstype definition is in the definition of equality. In the former, we can write equations, in the latter, we need to give a constructive definition of “same”. cs776 (prasad) L8tdef

val s1 = (insert empty 5); val s2 = (insert s1 3); (member s1 8); (member s1 5); (member s1 1); val s3 = (remove s1 5); (member s3 5); No side-effects cs776 (prasad) L8tdef

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. *) Other implementations : sort the list, remove duplicates (representations satisfy additional properties : invariants) cs776 (prasad) L8tdef

intset = Set of (int -> bool) with val empty = 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; Unorthodox impl. in terms of functions. - “infinite” sets can be described - but cannot check for empty set! cs776 (prasad) L8tdef