slides created by Marty Stepp

Slides:



Advertisements
Similar presentations
ML Declarations.1 Standard ML Declarations. ML Declarations.2 Declarations o Re-declaration of names in ML o The reserved words "val rec" o Pattern Matching.
Advertisements

ML Declarations.1 Standard ML Declarations. ML Declarations.2  Area of a circle: - val pi = ; val pi = : real - fun area (r) = pi*r*r;
Interfaces CSC 171 FALL 2004 LECTURE 14. Project 1 review public class Rational { private int numerator, denominator; public Rational(int numerator, int.
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
Fields, Constructors, Methods
CSE341: Programming Languages Lecture 12 Modules Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 24 Racket Modules, Abstraction with Dynamic Types; Racket Contracts Dan Grossman Fall 2011.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
CSE 341 Lecture 6 exceptions; higher order functions; map, filter, reduce Ullman 5.2, 5.4 slides created by Marty Stepp
Abstract Data Types and Encapsulation Concepts
Abstract Classes and Interfaces
CSE 341 Lecture 10 more about data types; nullable types; option Ullman ; slides created by Marty Stepp
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Packages. Package A package is a set of related classes Syntax to put a class into a package: package ; public class { …} Two rules:  A package declaration.
COSC 1P03 Data Structures and Abstraction 4.1 Abstract Data Types The advantage of a bad memory is that one enjoys several times the same good things for.
Copyright © 2007 Pearson Education, Inc. Slide R-1.
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets read slides created by Marty Stepp and Hélène Martin
Introduction to Classes in C++
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
CSE 341 Lecture 12 structures Ullman slides created by Marty Stepp
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Abstract Data Types and Modules Lecture 11 – ADT and Modules, Spring CSE3302 Programming.
Chapter SevenModern Programming Languages1 A Second Look At ML.
CSE 341 : Programming Languages Lecture 12 ML Modules Zach Tatlock Spring 2014.
CSE 341 Lecture 8 curried functions Ullman 5.5 slides created by Marty Stepp
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets
CSE341: Programming Languages Lecture 10 ML Modules
Haskell Chapter 2.
2.7 Inheritance Types of inheritance
Inheritance and Polymorphism
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Agenda Warmup AP Exam Review: Litvin A2
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
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 Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2017.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
CSE341: Programming Languages Lecture 10 ML Modules
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2018.
CSE 341 Lecture 3 let expressions; pattern matching Ullman
CSE341: Programming Languages Lecture 10 ML Modules
CSE 341 Section 5 Winter 2018.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 10 ML Modules
Java Inheritance.
Binary Search Trees continued; Tree Sets
CSE-321 Programming Languages Introduction to Functional Programming
CSE341: Programming Languages Lecture 10 ML Modules
CSE 341 Lecture 7 anonymous functions; composition of functions Ullman 5.1.3, 5.6 slides created by Marty Stepp
Chapter 14 Abstract Classes and Interfaces
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2016.
CIS 199 Final Review.
CSE 341 Lecture 2 lists and tuples; more functions; mutable state
CSE341: Programming Languages Lecture 10 ML Modules
CSE341: Programming Languages Lecture 10 ML Modules
CSG2H3 Object Oriented Programming
11.1 The Concept of Abstraction
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2019.
Chengyu Sun California State University, Los Angeles
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

slides created by Marty Stepp CSE 341 Lecture 13 signatures Ullman 8.2 slides created by Marty Stepp http://www.cs.washington.edu/341/

Recall: Why modules? organization: puts related code together decomposition: break down a problem information hiding / encapsulation: protect data from damage by other code group identifiers into namespaces; reduce # of globals provide a layer of abstraction; allows re-implementation ability to rigidly enforce data invariants provides a discrete unit for testing

A structure's signature - structure Helpers = struct fun square(x) = x*x; fun pow(x, 0) = 1 | pow(x, y) = x * pow(x, y - 1); end; structure Helpers : sig val square : int -> int val pow : int * int -> int end every structure you define has a public signature signature: Set of symbols presented by a module to clients by default, all definitions are presented in its signature

Limitations of structures Ways that Java hides information in a class? make a given field and method private, protected create an interface or superclass with fewer members; refer to the object through that type (polymorphism) signature: A group of ML declarations of functions, types, and variables exported to clients by a structure / module. combines Java's concepts of private and interface

Using signatures 1. Define a signature SIG that declares members A, B, C. 2. Structure ST1 defines A, B, C, D, E. ST1 can specify that it wants to use SIG as its signature. Now clients can call only A, B, C (not D or E). 3. Structure ST2 defines A, B, C, F, G. ST2 can also specify to use SIG as its public signature. Now clients can call only A, B, C (not F or G).

Signature syntax signature NAME = sig definitions end; a signature can contain: function declarations (using val, not fun) ... no bodies val declarations (variables; class constants), definitions exceptions type declarations, definitions, and datatypes

Function declarations val name: paramType * paramType ... -> resultType; Example: val max: int * int -> int; signatures don't have function definitions, with fun they instead have declarations, with val lists parameter types return type (no implementation)

Abstract type declarations type name; Example: type Beverage; signatures shouldn't always define datatypes this can lock the implementer into a given implementation instead simply declare an abstract type this indicates to ML that such a type will be defined later now the declared type can be used as a param/return type

Signature example (* Signature for binary search trees of integers. *) signature INTTREE = sig type intTree; val add: intTree -> intTree; val height : intTree -> int; val min : intTree -> int option; end;

Implementing a signature structure name :> SIGNATURE = struct definitions end; Example: structure IntTree :> INTTREE = ...

Signature semantics when a structure implements a signature, structure must implement all members of the signature by convention, signature names are ALL_UPPERCASE

Signature exercise Modify the Rational structure to implement a RATIONAL signature. In the signature, hide any members that clients shouldn't use directly. (What members should be in the signature?)

Signature solution 1 (* Type signature for rational numbers. *) signature RATIONAL = sig (* notice that we don't specify the innards of rational type *) type rational; exception Undefined; (* notice that gcd and reduce are not included here *) val new : int * int -> rational; val add : rational * rational -> rational; val toString : rational -> string; end;

Structure solution 2 (* invariant: for Fraction(a, b), b > 0 andalso gcd(a, b) = 1 *) structure Rational :> RATIONAL = struct datatype rational = Whole of int | Fraction of int * int; exception Undefined of string; fun gcd(a, 0) = abs(a) (* 'private' *) | gcd(a, b) = gcd(b, a mod b); fun reduce(Whole(i)) = Whole(i) (* 'private' *) | reduce(Fraction(a, b)) = let val d = gcd(a, b) in if b = d then Whole(a div d) else Fraction(a div d, b div d) end; fun new(a, 0) = raise Undefined("cannot divide by zero") | new(a, b) = reduce(Fraction(a, b)); fun add(Whole(i), Whole(j)) = Whole(i + j) | add(Whole(i), Fraction(c, d)) = Fraction(i*d + c, d) | add(Fraction(a, b), Whole(j)) = Fraction(a + j*b, b) | add(Fraction(a, b), Fraction(c, d)) = reduce(Fraction(a*d + c*b, b*d)); (* toString unchanged *)

Using a structure by its signature - val r = Rational.new(3, 4); val r = - : Rational.rational - Rational.toString(r); val it = "3/4" : string - Rational.gcd(24, 56); stdIn:5.1-5.13 Error: unbound variable or constructor: gcd in path Rational.gcd - Rational.reduce(r); stdIn:1.1-1.15 Error: unbound variable or constructor: ... - Rational.Whole(5); using the signature restricts the structure's interface clients cannot access or call any members not in the sig

A re-implementation (* Alternate implementation using a tuple of (numer, denom). *) structure RationalTuple :> RATIONAL = struct type rational = int * int; exception Undefined; fun gcd(a, 0) = abs(a) | gcd(a, b) = gcd(b, a mod b); fun reduce(a, b) = let val d = gcd(a, b) in if b >= 0 then (a div d, b div d) else reduce(~a, ~b) end; fun new(a, 0) = raise Undefined | new(a, b) = reduce(a, b); fun add((a, b), (c, d)) = reduce(a * d + c * b, b * d); fun toString(a, 1) = Int.toString(a) | toString(a, b) = Int.toString(a) ^ "/" ^ Int.toString(b); fun fromInteger(a) = (a, 1);

Another re-implementation (* Alternate implementation using a real number; imprecise due to floating point round-off errors. *) structure Rational :> RATIONAL = struct type rational = real; exception Undefined; fun new(a, b) = real(a) / real(b); fun add(a, b:rational) = a + b; fun toString(r) = Real.toString(r); end;

Signature exercise 2 Use the new signature to enforce these invariants: All fractions will always be created in reduced form. (In other words, for all fractions a/b, gcd(a, b) = 1.) Negative fractions will be represented as -a / b, not a / -b. (In other words, for all fractions a/b, b > 0.) Add the ability for clients to use the Whole constructor. Add operations such as ceil, floor, round, subtract, multiply, divide, ...