CMSC 330: Organization of Programming Languages. Basic OCaml Modules

Similar documents
OCaml Data CMSC 330: Organization of Programming Languages. User Defined Types. Variation: Shapes in Java

CMSC 330: Organization of Programming Languages. Objects and Abstract Data Types

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

EPITA - Practical Programming 06 - Advanced Module Usage

Programming Languages and Techniques (CIS120)

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Data Abstraction. Hwansoo Han

CS 11 Ocaml track: lecture 4. Today: modules

CMSC 330, Fall 2013, Practice Problems 3

CMSC 330: Organization of Programming Languages. Lets, Tuples, Records

Modular Programming. Prof. Clarkson Fall Today s music: "Giorgio By Moroder" by Daft Punk

CMSC 330: Organization of Programming Languages

CMSC 330, Fall 2013, Practice Problem 3 Solutions

CMSC 330: Organization of Programming Languages. Rust Basics

Background. CMSC 330: Organization of Programming Languages. Useful Information on OCaml language. Dialects of ML. ML (Meta Language) Standard ML

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

CIS 120 Midterm I February 16, 2015 SOLUTIONS

Today s lecture. CS 314 fall 01 C++ 1, page 1

Object Oriented Programming in C#

CMSC 330: Organization of Programming Languages

Programming Languages. Example 5. Example 4. CSE 130 : Fall type, can reuse code for all types! let rec cat l = match l with

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CS558 Programming Languages

Inheritance and Interfaces

CMSC 631. Functional Programming with OCaml

G52CPP C++ Programming Lecture 9

Programming Languages and Techniques (CIS120)

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

CMSC330. Objects, Functional Programming, and lambda calculus

Subclassing for ADTs Implementation

Programming Languages and Techniques (CIS120)

Some Advanced ML Features

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Lecture 38: Python. CS 51G Spring 2018 Kim Bruce

Lecture 19: Signatures, Structures, and Type Abstraction

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

Modules and Abstract Data Types

CMSC330 Fall 2013 Practice Problems 6 Solutions

1 Inheritance (8 minutes, 9 points)

CS61B Lecture #13: Packages, Access, Etc.

CPS 506 Comparative Programming Languages. Programming Language

CS61B Lecture #13: Packages, Access, Etc.

PIC 10A Objects/Classes

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Principles of Functional Programming

17 Multiple Inheritance and ADT Extensions

CMSC 330: Organization of Programming Languages. OCaml Data Types

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

CIS 120 Midterm I October 2, Name (printed): Username (login id):

CS3110 Spring 2016 Lecture 6: Modules

Functors signature Order = sig functor name type elem (structname:signature) = structure definition; val le : elem*elem -> bool end; elem

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Modules and Representation Invariants

Programming Languages and Techniques (CIS120)

CS18000: Programming I

Typed Racket: Racket with Static Types

Project 6 Due 11:59:59pm Thu, Dec 10, 2015

Reference Cells. Example

CSE341, Fall 2011, Lecture 12 Summary

Networked Embedded System Patterns for C Developers. Overview of C (& C++) Programming Styles. Douglas C. Schmidt

Princeton University Computer Science 217: Introduction to Programming Systems. Modules and Interfaces

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

COM2001 Advanced Programming Techniques. Mike Stannett Department of Computer Science The University of Sheffield

Software Design and Analysis for Engineers

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

CS112 Lecture: Working with Numbers

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

So far. Specifications, conclusion. Abstract Data Types (ADTs) Comparison by Logical Formulas. Outline

CMSC 330: Organization of Programming Languages

Implementing an abstract datatype. Linked lists and queues

Smart Pointers, deleted functions, and 2-3 trees

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

Topic 7: Algebraic Data Types

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

The Trouble with Types

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

5.6.1 The Special Variable this

CMSC 430 Introduction to Compilers. Fall Everything (else) you always wanted to know about OCaml (but were afraid to ask)

Programming in Standard ML: Continued

Adam Blank Lecture 5 Winter 2019 CS 2. Introduction to Programming Methods

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

Code Reuse: Inheritance

CSE 331 Software Design & Implementation

CSE 331 Software Design & Implementation

After a lecture on cosmology and the structure of the solar system, William James was accosted by a little old lady.

Chapter 1 Getting Started

Lists. Michael P. Fourman. February 2, 2010

CSCI-GA Scripting Languages

CMSC 132: Object-Oriented Programming II

Programming Exercise 14: Inheritance and Polymorphism

CMSC 341 Lecture 7 Lists

Lecture 13: more class, C++ memory management

Transcription:

CMSC 330: Organization of Programming Languages Basic OCaml Modules 1

Modules So far, most everything we ve defined has been at the top-level of OCaml This is not good software engineering practice A better idea: Use modules to group associated types, functions, and data together Avoid polluting the top-level with unnecessary stuff For lots of sample modules, see the OCaml standard library, e.g., List, Str, etc. 2

Creating A Module In OCaml module ISet = struct type set = EMPTY INS of int * set let empty = EMPTY let isempty(s) = s = EMPTY let insert(s, i) = INS(i,s) let rec contains(s, i) = match s with EMPTY -> false INS(j,r) -> if i = j then true else contains(r,i) end;; 3

Creating A Module In OCaml (cont.) module ISet = struct type set = let empty = let isempty = let insert = let contains = end;; # empty;; ERROR: unbound value empty # ISet.empty;; - : ISet.set = ISet.EMPTY # ISet.contains (Set.empty, 1);; - bool = false # open ISet;; (* add ISet names to curr scope *) # empty;; - : ISet.EMPTY (* now defined *) 4

Module Signatures Entry in signature Supply function types module type FOO = sig val add : int -> int -> int end;; module Foo : FOO = Give type to module struct let add x y = x + y let mult x y = x * y end;; Foo.add 3 4;; (* OK *) Foo.mult 3 4;; (* not accessible *) 5

Module Signatures (cont.) Convention: Signature names in all-caps This isn't a strict requirement, though Items can be omitted from a module signature This provides the ability to hide values The default signature for a module hides nothing This is what OCaml gives you if you just type in a module with no signature at the top-level 6

Abstraction = Hiding Signatures hide module implementation details Why do that? Doesn t that reduce flexibility? This is good software engineering practice Ensures data structure invariants maintained clients can t construct arbitrary data structures, only ones our module s functions create Facilitates code collaboration Write code to the interface as implementation worked out Clients do not rely details that may change Changing set representation later won t affect clients 7

Abstract Data Types Idea: Hide data value s internal representation from its clients Invented by Barbara Liskov in the CLU programming language Professor at MIT since 1971 Won Turing Award for ADTs and other contributions in 2008 http://amturing.acm.org/award_winners/liskov_1108679.cfm 8

Abstract Data Types In OCaml Sigs module type ISET = sig type set val empty : set val isempty : set -> bool val insert : set * int -> set val contains : set * int -> bool end;; module ISet : ISET = struct type set = EMPTY INS of int * set... let insert (s,i) = INS(i,s) end The definition of set is hidden from ISet clients 9

Quiz 1: Evaluation on ADTs # ISet.empty - : ISet.set = <abstr> (* OCaml won t show impl *) # ISet.EMPTY Unbound Constructor ISet.EMPTY # ISet.isEmpty (ISet.insert(ISet.empty, 0)) - : bool = false # open ISet;; (* doesn t make anything abstract accessible *) # ISet.insert (ISet.empty, 0) A.- : ISet.set = <abstr> B.Type Error C.- : ISet.INS (0, ISet.EMPTY) 10

Quiz 1: Evaluation on ADTs # ISet.empty - : ISet.set = <abstr> (* OCaml won t show impl *) # ISet.EMPTY Unbound Constructor ISet.EMPTY # ISet.isEmpty (ISet.insert(ISet.empty, 0)) - : bool = false # open ISet;; (* doesn t make anything abstract accessible *) # ISet.insert (ISet.empty, 0) A.- : ISet.set = <abstr> B.Type Error C.- : ISet.INS (0, ISet.EMPTY) 11

Multiple representations module ISetBST : ISET = struct type set = TIP BIN of int * set * set... let rec insert (s,i) = match s with TIP -> BIN(i,TIP, TIP) BIN(j,l,r) -> if i = j then s else if i < j then BIN(j,insert(l,i),r) else BIN(j,l,insert(r,i)) end Now set is a binary search tree (why?) 12

Quiz 2: Mixing ADTs? # ISetBST.empty - : ISetBST.set = <abstr> (* OCaml won t show impl *) # ISetBST.insert (ISetBST.empty, 0) - : ISetBST.set = <abstr> # ISetBST.contains (ISetBST.empty, 0) - : bool = false # ISet.insert (ISetBST.empty, 0) A.- : ISet.set = <abstr> B.- : ISetBST.set = <abstr> C.Type Error D.- : ISetBST.INS (0, ISet.EMPTY) 13

Quiz 2: Mixing ADTs? # ISetBST.empty - : ISetBST.set = <abstr> (* OCaml won t show impl *) # ISetBST.insert (ISetBST.empty, 0) - : ISetBST.set = <abstr> # ISetBST.contains (ISetBST.empty, 0) - : bool = false # ISet.insert (ISetBST.empty, 0) A.- : ISet.set = <abstr> B.- : ISetBST.set = <abstr> C.Type Error D.- : ISetBST.INS (0, ISet.EMPTY) 14

Different ADTs are different The set type of ISet and ISetBST look the same, but are not Both modules are an instance of the ISET signature But because the type s definition is hidden, it is not safe to mix them This distinction is enforced by the type system set type is an abstract type the instances of modules having the ISET signature (which has an abstract type) are called abstract data types (ADTs) 15

Other Module Systems How OCaml s approach compare to modularity in... Java? C? Ruby? 16

Modules In Java Java classes are like modules Provide implementations for a group of functions But classes can also Instantiate objects Inherit attributes from other classes Java interfaces are like module signatures Defines a group of functions that may be used Implementation is hidden But: Objects and modules/adt not the same Next lecture topic! 17

Modules In C.c files are like modules Provides implementations for a group of functions.h files are like module signatures Defines a group of functions that may be used Implementation is hidden Usage is not enforced by C language Can put C code in.h file 18

Modules In Ruby Ruby explicitly supports modules Modules defined by module end Modules cannot Instantiate objects Derive subclasses puts Math.sqrt(4) # 2 puts Math::PI # 3.1416 include Math # open Math puts sqrt(4) # 2 puts PI # 3.1416 19