Practical Generic Programming with OCaml



Similar documents
Adding GADTs to OCaml the direct approach

Towards Interface Types for Haskell

Lecture Data Types and Types of a Language

The Clean programming language. Group 25, Jingui Li, Daren Tuzi

The Needle Programming Language

Lecture 12: Abstract data types

ML for the Working Programmer

MLS module Sealing and Functor Design

Programming Language Features (cont.) CMSC 330: Organization of Programming Languages. Parameter Passing in OCaml. Call-by-Value


: provid.ir

Gluing things together with Haskell. Neil Mitchell

Extensible Effects An Alternative to Monad Transformers

Anatomy of Programming Languages. William R. Cook


Cloud Haskell. Distributed Programming with. Andres Löh. 14 June 2013, Big Tech Day 6 Copyright 2013 Well-Typed LLP. .Well-Typed

Special Directions for this Test

TypeScript for C# developers. Making JavaScript manageable

sqlpp11 - An SQL Library Worthy of Modern C++

Translating a Subset of OCaml into System F. Jonatha Protzenk under the dire ion of François Po ier

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

CSc 372. Comparative Programming Languages. 21 : Haskell Accumulative Recursion. Department of Computer Science University of Arizona

Outline Basic concepts of Python language

Python 2 and 3 compatibility testing via optional run-time type checking

Lecture 1: Introduction

One Dimension Array: Declaring a fixed-array, if array-name is the name of an array

Fun with Phantom Types

Unboxed Polymorphic Objects for Functional Numerical Programming

POLYTYPIC PROGRAMMING OR: Programming Language Theory is Helpful

>

UNIVERSITY OF LONDON (University College London) M.Sc. DEGREE 1998 COMPUTER SCIENCE D16: FUNCTIONAL PROGRAMMING. Answer THREE Questions.

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

RIPL. An Image Processing DSL. Rob Stewart & Deepayan Bhowmik. 1st May, Heriot Watt University

Analyse et Conception Formelle. Lesson 5. Crash Course on Scala

Translating Generalized Algebraic Data Types to System F

Programming Language Rankings. Lecture 15: Type Inference, polymorphism & Type Classes. Top Combined. Tiobe Index. CSC 131! Fall, 2014!

Chapter 15 Functional Programming Languages

Functional Programming in C++11

Cassandra. References:

Polymorphic Type Inference and Abstract Data Types

Chapter 7: Functional Programming Languages

First Class Overloading via Insersection Type Parameters

Programming in Standard ML

Overview. Elements of Programming Languages. Advanced constructs. Motivating inner class example

Advanced Functional Programming (9) Domain Specific Embedded Languages

IVR Studio 3.0 Guide. May Knowlarity Product Team

Timber: A Programming Language for Real-Time Embedded Systems

Programming and Reasoning with Side-Effects in IDRIS

Type Inference for First-Class Messages with Match-Functions

Semester Review. CSC 301, Fall 2015

Topic VII. Data abstraction and modularity SML Modules a. References: Chapter 7 of ML for the working programmer (2ND

Novas Software, Inc. Introduction. Goal. PART I: Original Technology Donation 1. Data Dump Reader

Monitor Your Key Performance Indicators using WSO2 Business Activity Monitor

A Value Transmission Method for Abstract Data Types

Datatype-generic data migrations

Pretty-big-step semantics

Lambda Architecture. CSCI 5828: Foundations of Software Engineering Lecture 29 12/09/2014

UNIVERSITY OF MALTA THE MATRICULATION CERTIFICATE EXAMINATION ADVANCED LEVEL COMPUTING. May 2011

Introduction to Java. CS 3: Computer Programming in Java

William E. Hart Carl Laird Jean-Paul Watson David L. Woodruff. Pyomo Optimization. Modeling in Python. ^ Springer

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Generating a Recognizer from Declarative Machine Descriptions

Programming and Reasoning with Algebraic Effects and Dependent Types

MongoDB. Or how I learned to stop worrying and love the database. Mathias Stearn. N*SQL Berlin October 22th, gen

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

opalang - Rapid & Secure Web Development

FreshML: Programming with Binders Made Simple

Compiling Recursion to Reconfigurable Hardware using CLaSH

From Interpreter to Compiler and Virtual Machine

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

Supporting Data Set Joins in BIRT

Functional Programming. Functional Programming Languages. Chapter 14. Introduction

TECHNICAL UNIVERSITY OF CRETE DATA STRUCTURES FILE STRUCTURES

Regression Verification: Status Report

F# Web Tools: Rich client/server web applications in F#

C++ INTERVIEW QUESTIONS

PostgreSQL Functions By Example

MapReduce. MapReduce and SQL Injections. CS 3200 Final Lecture. Introduction. MapReduce. Programming Model. Example

Big Data Analytics with Cassandra, Spark & MLLib

Fundamentals of Programming Languages

IENG2004 Industrial Database and Systems Design. Microsoft Access I. What is Microsoft Access? Architecture of Microsoft Access

Asymptotic Improvement of Computations over Free Monads

Towards CNC Programming Using Haskell

Transcription:

Practical Generic Programming with OCaml Jeremy Yallop LFCS, University of Edinburgh ML Workshop 2007

Instead of this... type α tree = Node of α Branch of (α tree) (α tree) val show_tree : (α string) (α tree string) let rec show_tree show_a = function Node a -> "Node " ^ show_a a Branch (l, r) -> "Branch ("^ show_tree l ^ "," ^ show_tree r ^ ")" show_list (show_pair (show_tree show_int) show_bool) t

You can write this! type α tree = Node of α Branch of (α tree) (α tree) deriving (Show) Show.show<(int tree * bool) list> t

Outline Basic idea Customization More customization: pickling Conclusions

Haskell type classes as OCaml modules 1 class Show a where show :: a String module type Show = sig type a val show : a string end Type class as signature 1 Dreyer, Harper, Chakravarty and Keller. Modular Type Classes (POPL 07)

Haskell type classes as OCaml modules instance Show Int where show = showint module ShowInt : Show with type a = int = struct type a = int let show = string_of_int end Instance as structure

Haskell type classes as OCaml modules instance (Show a) => Show [a] where show l = "[" ++ intersperse "," (map show l) ++ "]" module ShowList (A : Show) : Show with type a = A.a list = struct type a = A.a list let show l = "[" ^ concat "," (map A.show l) ^ "]" end Parameterized instance as functor

Haskell type classes as OCaml modules data Tree α = Node α Branch (Tree α) (Tree α) deriving (Show) type α tree = Node of α Branch of (α tree) (α tree) deriving (Show)

Haskell type classes as OCaml modules data Tree α = Node α Branch (Tree α) (Tree α) deriving (Show) instance Show a => Show (Tree a) where show =... type α tree = Node of α Branch of (α tree) (α tree) deriving (Show)

Haskell type classes as OCaml modules data Tree α = Node α Branch (Tree α) (Tree α) deriving (Show) instance Show a => Show (Tree a) where show =... type α tree = Node of α Branch of (α tree) (α tree) deriving (Show) module Show_tree (A : Show) : Show with type a = A.a tree = struct type a = A.a tree let show =... end

Haskell type classes as OCaml modules show t Show.show<τ> t

Outline Basic idea Customization More customization: pickling Conclusions

Customization type intset = int list deriving (Show) Show.show<intset> [4; 1; 2; 3] = "[4; 1; 2; 3]"

Customization type intset = int list module Show_intset : Show.Show with type a = intset = Show.Defaults(struct let format fmt t = Format.fprintf fmt "{%s}" (concat "," (map Show.show<int> (sort compare t))) end) Show.show<intset> [4; 1; 2; 3] = "{1, 2, 3, 4}"

Outline Basic idea Customization More customization: pickling Conclusions

More customization: pickling The Pickle class marshals values Pickle preserves and increases sharing wherever possible Sharing detection depends on the definition of equality We can customize pickling by customizing equality

What is equality? For values? For references? For functions? For user-defined types?

Sharing λ terms type name = string deriving (Eq, Typeable, Pickle) type exp = Var of name of exp exp Lam of name exp deriving (Eq, Typeable, Pickle)

Sharing λ terms type name = string deriving (Typeable, Pickle) type exp = Var of name of exp exp Lam of name exp deriving (Typeable, Pickle) module Eq_name : Eq.Eq with type a = name = struct type a = name let eq = (=) end module Eq_exp : Eq.Eq with type a = name = struct type a = exp let eq l r = (* α-equivalence *)... end

Lam (a) a Lam Lam b d Var Lam Var Lam a c a e Var Var Var Var c Var Var e b b d d

Lam (a) Lam (c) a a Lam Lam Lam b d b Var Lam Var Lam Var Lam a c a e c Var Var Var Var Var c Var Var e Var b b d d mar

Outline Basic idea Customization More customization: pickling Conclusions

Who can use deriving? Regular users Advanced users Experts use generic functions customize generic functions write generic functions

Coverage Supported: base types variants tuples records mutable types polymorphic variants type aliases parameterized types (mutually) recursive types modules constraints (a bit) private types type replication Not supported: non-regular recursion polymorphic record fields class types private rows

Remaining work more classes user-defined overloaded functions (not class methods) (* print : Show α α unit *) let print<a:show> v = print_endline (show<a> v)

Thank you! http://code.google.com/p/deriving (or google ocaml and deriving )