Cs776(Prasad)L112Modules1 Modules value : type : function :: structure : signature : functor.

Slides:



Advertisements
Similar presentations
Sml2java a source to source translator Justin Koser, Haakon Larsen, Jeffrey Vaughan PLI 2003 DP-COOL.
Advertisements

Transposing F to C Transposing F to C Andrew Kennedy & Don Syme Microsoft Research Cambridge, U.K.
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
More ML Compiling Techniques David Walker. Today More data structures lists More functions More modules.
Modern Programming Languages, 2nd ed.
Type Systems and Object- Oriented Programming (III) John C. Mitchell Stanford University.
Modules Program is built out of components. Each component defines a set of logically related entities (strong internal coupling) A component has a public.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Module Language. Module language The Standard ML module language comprises the mechanisms for structuring programs into separate units. –Program units.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Data Abstraction COS 441 Princeton University Fall 2004.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Programming in the large in ML Need mechanisms for –Modularization –Information hiding –Parametrization of interfaces While retaining type inference Modules:
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
Encapsulation by Subprograms and Type Definitions
Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom.
Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML.
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich : Objects and Aspects Carnegie Mellon University.
Programming Languages and Paradigms Object-Oriented Programming.
© Kenneth C. Louden, Chapter 9 – Abstract Data Types and Modules Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Ceg860 (Prasad)L6MR1 Modularity Extendibility Reusability.
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
CSE-321 Programming Languages Introduction to Functional Programming (Part II) POSTECH March 13, 2006 박성우.
Low-Level Detailed Design SAD (Soft Arch Design) Mid-level Detailed Design Low-Level Detailed Design Design Finalization Design Document.
Evolving the ML Module System Derek Dreyer Toyota Technological Institute at Chicago April 15, 2004.
Cs776 (Prasad)L16rem1 Remaining Features of SML97 Records Exceptions Reference Types Arrays.
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)L7Class1 Classes. ceg860 (Prasad)L7Class2 Class :: Instance (Object) Static structure vs Run-time structure (Analogy -- a statue :: Lincoln.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
Programming Languages Third Edition Chapter 11 Abstract Data Types and Modules.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Abstract Data Types and Modules Lecture 11 – ADT and Modules, Spring CSE3302 Programming.
A Type System for Higher-Order Modules Derek Dreyer, Karl Crary, and Robert Harper Carnegie Mellon University POPL 2003.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
1 CS Programming Languages Class 22 November 14, 2000.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
Introduction to Functional Programming
Chapter SevenModern Programming Languages1 A Second Look At ML.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Cs776(Prasad)L6sml971 SML-97 Specifics SML/NJ 110.
1 Objective Caml (Ocaml) Aaron Bloomfield CS 415 Fall 2005.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
Prof. I. J. Chung Data Structure #1 Professor I. J. Chung.
Type Checking and Type Inference
Type Definitions cs776 (prasad) L8tdef.
11.1 The Concept of Abstraction
slides created by Marty Stepp
Lecture 9 Concepts of Programming Languages
Functions, Patterns and Datatypes
value : type : function :: structure : signature : functor
Functions, Patterns and Datatypes
Functions, Patterns and Datatypes
CSE-321 Programming Languages Introduction to Functional Programming
Functions, Patterns and Datatypes
Functions, Patterns and Datatypes
11.1 The Concept of Abstraction
Lecture 9 Concepts of Programming Languages
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

cs776(Prasad)L112Modules1 Modules value : type : function :: structure : signature : functor

cs776(Prasad)L112Modules2 SML features Programming in the small –Reliability Correctness (types) Robustness (exceptions) –Efficiency Data Structures and Algorithms (lists, arrays) –Abstraction Data (patterns) Control (higher-order functions) Declarative (enhances programmer productivity) Programming in the large –Cluster related definitions; hierarchical organization of programs (structures) –Encapsulation (localization) of implementation details, for clarity and modifiability (abstype, signatures) –Reusability and interchangeability of parts (functors). Separate compilation. Standard libraries.

cs776(Prasad)L112Modules3 Customization of Modules Controlling visibility of names (Managing environments) –To avoid potential name conflicts. Multiple implementations of an ADT (e.g, Table ). Similar operations on different ADTs (e.g., delete on Set, List, Table, etc). Programmer choice of names, to conform to domain vocabulary. –To encapsulate representation details. To enable transparent changes to the impl. To preserve data integrity (invariants) Instantiation (Specializing w.r.t. types)

cs776(Prasad)L112Modules4 SML Constructs Structure –Collection of definitions E.g., Ada/Java packages, C++/Java classes, etc Signature –Type declarations of the names E.g., Ada package specification, Java interfaces, etc Functor –Maps a structure to another structure –Parameterized structures E.g., Ada generic units, C++ templates, Java functions with interface/class parameters, etc

cs776(Prasad)L112Modules5 structure structure QueueImpl = struct datatype 'a t = Q of ('a list * 'a list); exception E; val empty = Q([],[]); fun norm (Q([],ts)) = Q(rev ts, []) | norm q = q; fun enq(Q(hs,ts), x) = norm(Q(hs, x::ts)); fun null(Q([],[])) = true | null _ = false; fun hd(Q(x::_,_)) = x | hd(Q([],_)) = raise E; fun deq(Q(x::hs,ts)) = norm(Q(hs,ts)) | deq(Q([],_)) = raise E; end end;

cs776(Prasad)L112Modules6 structure structure QueueImpl : sig datatype 'a t = Q of 'a list * 'a list exception E val deq : 'a t -> 'a t val empty : 'a t val enq : 'a t * 'a -> 'a t val hd : 'a t -> 'a val norm : 'a t -> 'a t val null : 'a t -> bool end;

cs776(Prasad)L112Modules7 Information Hiding Concerns The implementation details of QueueImpl are exported. –Client code can use pattern matching to manipulate queues. Client code can break if the implementation of QueueImpl is changed. –Client code can ignore the FIFO discipline. Semantics of queue (invariants) can be violated. Possible Solution : Restrict the view

cs776(Prasad)L112Modules8 Transparent Signature Constraints signature signature Qtip = sig type ’a t val deq : 'a t -> 'a t val empty : 'a t val enq : 'a t * 'a -> 'a t val hd : 'a t -> 'a val null : 'a t -> bool end; structure Queue : Qtip = QueueImpl;

cs776(Prasad)L112Modules9 - QueueImpl.norm; val it = fn : 'a QueueImpl.t -> 'a QueueImpl.t - Queue.norm; (* error *) - Queue.hd; val it = fn : 'a QueueImpl.t -> 'a - Queue.Q; (* error *) - Queue.empty = QueueImpl.Q ([],[]) ; - open QueueImpl; - norm(Queue.enq(Queue.empty, “a”)); val it = Q (["a"],[]) : string t

cs776(Prasad)L112Modules10 Opaque Signature Constraints structure ; structure Queue :> Qtip = QueueImpl; This enforces data abstraction. –Only operations that are explicitly defined on Queue can be invoked on Queue -values. QueueImpl.norm(Queue.enq(Queue.empty,“a”)); (* error *) –Equality tests (based on the representation) are banned. Queue.empty = QueueImpl.Q ([],[])); (* error *) To allow equality tests, use eqtype ’a t instead of type ’a t. (Cf. Ada’s Limited Private Types and Private Types)

cs776(Prasad)L112Modules11 signature SIG = sig val i : int; type t; val x : t val f:t*t -> bool end; structure STRUC = struct val i = 3; type t = int; val x = 4; fun f(a,b) = (a=b) end; open STRUC; f(x,x); f(i,x); (* val it = false : bool *) structure STRUC’:> SIG = STRUC; open STRUC’; f(i,x); (* type error *) Type t created using opaque signatures is regarded as distinct from any other type (including int and other type t created from the same structure.)

cs776(Prasad)L112Modules12 signature SIG = sig val i : int; type t; val x : t val f:t*t -> bool end; structure STRUC = struct val i = 3; type t = int; val x = 4; fun f(a,b) = (a=b) end; signature SIG’ = SIG where type t = int; structure STRUC’:> SIG’ = STRUC; open STRUC’; f(i,x); (* val it = false : bool*) Type t being int is visible because where - clause explicitly exports it.

cs776(Prasad)L112Modules13 Signature Matching structure strId : sigExp = strExp ; (* target candidate *) Target signature expresses a set of constraints that the candidate structure must satisfy. Informally, the candidate’s signature may have more components than are required by the target, may have more definitions of types than are required, and may have value components with more general types.

cs776(Prasad)L112Modules14 QUEUE_WITH_EMPTY matches QUEUE signature QUEUE = sig type 'a queue exception Empty val empty : 'a queue val insert : 'a * 'a queue -> 'a queue val remove : 'a queue -> 'a * 'a queue end signature QUEUE_WITH_EMPTY = sig include QUEUE val is_empty : 'a queue -> bool end

cs776(Prasad)L112Modules15 QUEUE_AS_LISTS matches QUEUE signature QUEUE = sig type 'a queue exception Empty val empty : 'a queue val insert : 'a * 'a queue -> 'a queue val remove : 'a queue -> 'a * 'a queue end signature QUEUE_AS_LISTS = QUEUE where type 'a queue = 'a list * 'a list

cs776(Prasad)L112Modules16 Equivalent: QUEUE_AS_LIST and QUEUE_AS_LIST0 signature QUEUE = sig type 'a queue exception Empty val empty : 'a queue val insert : 'a * 'a queue -> 'a queue val remove : 'a queue -> 'a * 'a queue end signature QUEUE_AS_LIST0 = QUEUE where type 'a queue = 'a list signature QUEUE_AS_LIST = sig type 'a queue = 'a list exception Empty val empty : 'a list val insert : 'a * 'a list -> 'a list val remove : 'a list -> 'a * 'a list end

cs776(Prasad)L112Modules17 MERGEABLE_QUEUE matches MERGEABLE_INT_QUEUE signature MERGEABLE_QUEUE = sig include QUEUE val merge : 'a queue * 'a queue -> 'a queue end signature MERGEABLE_INT_QUEUE = sig include QUEUE val merge : int queue * int queue -> int queue end

cs776(Prasad)L112Modules18 RBT_DT matches RBT signature RBT_DT = sig datatype 'a rbt = Empty | Red of 'a rbt * 'a * 'a rbt | Black of 'a rbt * 'a * 'a rbt end signature RBT = sig type 'a rbt val Empty : 'a rbt val Red : 'a rbt * 'a * 'a rbt -> 'a rbt end

cs776(Prasad)L112Modules19 (cont’d) Every type specification (resp. datatype definition) in the target must have a matching (resp. equivalent) type specification (resp. datatype definition) in the candidate. Every exception specification in the target must have an equivalent exception specification in the candidate. Every value specification in the target must be matched by a value specification in the candidate with at least as general a type.

cs776(Prasad)L112Modules20 Relationship between Structures and Signatures interchangeability of componentsIn ML, a signature may serve as an interface for many different structures, and that a structure may implement many different signatures. (interchangeability of components) ML, C++, Java† : many-to-many Modula-2 : one-to-one Ada-95 : many-to-one. Every structure has a principal signature, with the property that all other signatures for that structure are more restrictive than the principal signature.

cs776(Prasad)L112Modules21 Functors functorA functor maps structures to structure. –A functor can be viewed as a reusable unit that contains well-defined “sockets for plug-ins”. –Parameterized/Generic modules. –Useful in “client-server paradigm” applications exploiting interchangeability of parts with common interface. Developing floating point libraries that work with different precision implementations. Building common test harness to verify various implementations of an abstract data type.

cs776(Prasad)L112Modules22 Example signature Order = sig eqtype et ; val le : et -> et -> bool end; functor MkOrdSet(Ord:Order) = struct exception EmptySetEX; datatype set = SET of (Ord.et list) val empty = SET nil fun insert (SET s) n = SET (n::s) fun member (SET s) n = (List.exists (fn x => (x = n)) s) fun min (SET s) = if (null(s)) then raise EmptySetEX else foldr (fn (x,r) => if (Ord.le r x) then r else x) (hd s) (tl s); end;

cs776(Prasad)L112Modules23 structure OrdInt: Order = struct type et = int; fun le (x:et) y = x <= y end; structure intset = MkOrdSet(OrdInt); open intset; (min empty); (* uncaught exception *) val s1 = (insert empty 5); val s2 = (insert s1 3); (* val s2 = SET [3,5] : set *) (min (insert s2 8)); (* val it = 3 : OrdInt.et *)

cs776(Prasad)L112Modules24 Example Functor (Component Reuse) signature ORDER = sig type t; val compare: t*t -> bool end; structure StringOrder: ORDER = struct type t = string; val compare = String.compare end;... structure StringDict = Dictionary (StringOrder); (* structure StringDict : sig type key = StringOrder.t type 'a t exception E of key val empty : 'a t val lookup : 'a t * Key.t -> 'a val update : 'a t * key * 'a -> 'a t end *)

cs776(Prasad)L112Modules25 functor Dictionary (Key: ORDER) = struct type key = Key.t; abstype 'a t = Lf | B of key *'a*'a t *'a t with exception E of key; val empty = Lf; fun lookup (B(a,x,t1,t2), b) = (case Key.compare(a,b) of GREATER => lookup(t1, b) | EQUAL => x | LESS => lookup(t2, b)) | lookup (Lf, b) = raise E b; fun update (Lf, b, y) = B(b, y, Lf, Lf) | update (B(a,x,t1,t2), b, y) = (case Key.compare(a,b) of GREATER => B(a, x, update(t1,b,y), t2) | EQUAL => B(a, y, t1, t2) | LESS => B(a, x,t1,update(t2,b,y))); end end;

cs776(Prasad)L112Modules26 (cont’d) infix |< ; fun (d |< (k,x)) = StringDict.update(d,k,x); val dict = StringDict.empty |< (“abc”, 2) |< (“nbc”, 11) |< (“dsc”, 53) |< (“tlc”,54); (* val dict = - : int StringDict.t *) StringDict.lookup(dict, “dsc”); (* val it = 53 : int *) StringDict.lookup(dict, “cnn”); (* uncaught exception E *)

cs776(Prasad)L112Modules27 signature INT = sig val i: int end; signature REAL = sig val r: real end; functor Foo(structure I:INT and R: REAL; val x : int) = struct end; structure Int = struct val i = 0 end; structure Real= struct val r = 0.0 end; structure Bar = Foo(structure I = Int and R = Real; val x = 2); Multi-argument Functor

cs776(Prasad)L112Modules28 Sharing Constraints (for combining / integrating modules) To express equality constraints among types names. Type sharing sharing type = … = Structure sharing sharing = … = Shorthand for sharing of identically named types within the equated structures.

cs776(Prasad)L112Modules29 signature ELEMENT = sig type element; val similar : element * element -> bool; end; signature BTREE = sig structure Element: ELEMENT; eqtype elt; sharing type elt = Element.element; datatype bt = Empty | Node of elt * bt * bt; val leaf : elt -> bt; val build : elt * bt * bt -> bt; val lookup : elt * bt -> bool end;