lec9 - CSE 130 : Fall 2007 Previously: Polymorphism enables...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CSE 130 : Fall 2007 Programming Languages Ranjit Jhala UC San Diego Lecture 9: Modules and Signatures Previously: Polymorphism enables Reuse • Can reuse generic functions: map :’a * ’b ’b * ’a filter : (’a bool * ’a list) ’a list rev : ’a list ’a list length : ’a list int swap : ’a * ’b ’b * ’a sort: (’a ’a bool * ’a list) ’a list compose: (’a ’b * ’c ’b) ’c ’a • If function (algorithm) is “independent” of type, can reuse code for all types ! Today: Divide-Conquer Programming • We’ve seen very small programs so far – 4 yr education chopped into quarters • Typical software is thousands, millions LOC • How can a PL help to manage complexity ? – Only one technique humans know about Rational Numbers • Say you want a software package that uses rational numbers heavily • Remember rationals ? – Ratio of two integers: m/n • Well, seems simple enough: – Use a pair of integers to represent rationals! – OCaml type: int * int Rational Numbers • Well, every pair of isn’t a rational… – (5,0) ? – 2nd elem “denominator” must not be zero • How do we print a rational ? • When are two rationals “equal” ? – (6,4) and (30,20) ? – Need to normalize rationals – But need to fix to_string Many functions related to type Pack into a module • Types, Values, Exceptions bound in Name are : Name.t, Name.x , Name.f • Bind module to diff name module Name = struct bindings end module NewName = Rational;; NewName.add;; NewName.to_string;;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Many functions related to type Pack into a module • Types, Values, Exceptions bound in Name are : Name.t, Name.x , Name.f • Can open module – Everything comes tumbling out module Name = struct bindings end open Rational;; add;; to_string;; Many functions related to type • Can pack into a module • Variables, functions, exceptions “bound” in Name are : Name.x , Name.f • Bind module to diff name • Open module • Not much better than diff files! – Whats the problem ? structure Name = struct bindings end Information Hiding • Prevent the client from passing in any pair – Hide implementation from client • Control usage by restricting the interface • Interface : what any client needs to know Specify Interfaces by Signatures Signature = Type of a module • Specifies what module provides – types – values and their types – exceptions and their types Name : SIGNAME Name implements SIGNAME • Code using Name knows only the entities in signature module type SIGNAME = sig type-bindings end module Name: SIGNAME = struct bindings end Implementing a Signature • ML checks if
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/16/2010 for the course CSE 101 taught by Professor Staff during the Winter '08 term at UCSD.

Page1 / 9

lec9 - CSE 130 : Fall 2007 Previously: Polymorphism enables...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online