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

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Programming with Lists
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.
A First Look at ML.
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
F28PL1 Programming Languages Lecture 14: Standard ML 4.
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,
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Data  Consists of constructions that can be inspected, taken apart, or joined to form.
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 =
Chapter ElevenModern Programming Languages1 A Fourth Look At ML.
A First Look at ML Chapter FiveModern Programming Languages, 2nd ed.1.
5/11/2015IT 3271 Types in ML (Ch 11) datatype bool = true | false; datatype 'element list = nil | :: of 'element * 'element list n Predefined, but not.
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.
Crash course on SML Wojciech Moczydłowski SML – functional programming language Complete formal semantics Extremely convenient for writing compilers, interpreters,
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.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
F28PL1 Programming Languages Lecture 13: Standard ML 3.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
Ceg860 (Prasad)LADT1 Specification and Implementation of Abstract Data Types Algebraic Techniques.
CS 2104 – Prog. Lang. Concepts Functional Programming II Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
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:
CSED101 INTRODUCTION TO COMPUTING SUM TYPE 유환조 Hwanjo Yu.
Cs776(Prasad)L112Modules1 Modules value : type : function :: structure : signature : functor.
A FIRST LOOK AT ML Chapter Five Modern Programming Languages 1.
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.
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
1.SML Docs Standard Basis 2.First-Class Functions Anonymous Style Points Higher-Order 3.Examples Agenda.
Types John Mitchell CS 242. Type A type is a collection of computable values that share some structural property. uExamples Integers Strings int  bool.
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
Principles of programming languages 12: Functional programming
ML: a quasi-functional language with strong typing
Type Definitions cs776 (prasad) L8tdef.
Lesson 4 Typed Arithmetic Typed Lambda Calculus
PROGRAMMING IN HASKELL
Agenda SML Docs First-Class Functions Examples Standard Basis
Agenda SML Docs First-Class Functions Examples Standard Basis
Functions, Patterns and Datatypes
CSE 341 Section 5 Winter 2018.
Functions, Patterns and Datatypes
Types and Classes in Haskell
PROGRAMMING IN HASKELL
Abstract Syntax cs7100 (Prasad) L7AST.
Functions, Patterns and Datatypes
CSE-321 Programming Languages Introduction to Functional Programming
Functions, Patterns and Datatypes
CSE-321 Programming Languages Introduction to Functional Programming
Sub-system interfaces
Functions, Patterns and Datatypes
Drew Wyborski Programming Languages
Presentation transcript:

cs7120 (prasad)L7-TDEF1 Type Definitions

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.

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.

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 *)

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);

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;

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 *)

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. *)

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;

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?

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)

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 })

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

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

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;

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);

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. *)

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;