• Aucun résultat trouvé

Practical Generic Programming with OCaml

N/A
N/A
Protected

Academic year: 2022

Partager "Practical Generic Programming with OCaml"

Copied!
26
0
0

Texte intégral

(1)

LFCS, University of Edinburgh

ML Workshop 2007

(2)

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

(3)

type α tree = Node of α | Branch of tree) × tree) deriving (Show)

Show.show<(int tree * bool) list> t

(4)

Basic idea

Customization

More customization: pickling

Conclusions

(5)

class Show a where show :: a String

module type Show = sig type a

val show : a string end

Type class as signature

1Dreyer, Harper, Chakravarty and Keller. Modular Type Classes (POPL 07)

(6)

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

(7)

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

(8)

data Tree α = Node α

| Branch (Tree α) (Tree α) deriving (Show)

type α tree = Node of α

| Branch of tree) × tree) deriving (Show)

(9)

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)

(10)

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

(11)

show t Show.show<τ> t

(12)

Basic idea

Customization

More customization: pickling

Conclusions

(13)

type intset = int list deriving (Show)

Show.show<intset> [4; 1; 2; 3] = "[4; 1; 2; 3]"

(14)

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}"

(15)

Basic idea

Customization

More customization: pickling

Conclusions

(16)

I The “Pickle” class marshals values

I Pickle preserves and increases sharing wherever possible

I Sharing detection depends on the definition of equality

I We can customize pickling by customizing equality

(17)

I For values?

I For references?

I For functions?

I For user-defined types?

(18)

deriving (Eq, Typeable, Pickle)

type exp = Var of name

| App of exp × exp

| Lam of name × exp deriving (Eq, Typeable, Pickle)

(19)

deriving (Typeable, Pickle)

type exp = Var of name

| App of exp × exp

| Lam of name × exp deriving (Typeable, Pickle)

: 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

(20)

d d b

b

App

Var Var

App

App

App e

a c a

Lam Lam

Var Var

Lam Lam

Var Var Var

Var c e

App App

b d

Lam

App a

(a)

Var Var App

App c

Lam Var

mar

(21)

d d b

b

App

Var Var

App

App

App e

a c a

Lam Lam

Var Var

Var Var Var

Var c e Var

Var App

App c

Lam Var

mar

(22)

Basic idea

Customization

More customization: pickling

Conclusions

(23)

Regular users use generic functions Advanced users customize generic functions Experts write generic functions

(24)

I base types

I variants

I tuples

I records

I mutable types

I polymorphic variants

I type aliases

I parameterized types

I (mutually) recursive types

I modules

I constraints (a bit)

I private types

I type replication

I non-regular recursion

I polymorphic record fields

I class types

I private rows

(25)

I more classes

I user-defined overloaded functions (not class methods) (* print : Show α α unit *)

let print<a:Show> v = print_endline (show<a> v)

(26)

(or google “ocaml” and “deriving”)

Références

Documents relatifs

Observer les interactions familiales en considérant l' ensemble des membres de la famille permet d'aller chercher davantage d ' information que l'observation des dyades à

J¨oricke Deformation of CR-manifolds, minimal points and CR-manifolds with the microlocal analytic extension property, which contains also a proof of Theorem 3.4 and Theorem 3.6..

In the natural context of ergodic optimization, we provide a short proof of the assertion that the maximizing measure of a generic continuous function has zero

Generic ontologies were introduced as an extension (Generic DOL) of the Distributed Ontology, Modeling and Specification Language, DOL, with the aim to provide a language for

Our main result states that for generic such control systems, all constant inputs of sufficient length N are universally regular, ex- cept for a discrete set.. We also show that

Below, it will be shown that local computation in covering join trees as defined in Section 3 can be applied to valuation algebras with partial marginalization too, and in

Our main contributions include the following: (1) we identify mild general conditions on the leakage function under which it is possi- ble to derive general and significant results

The main class or function template that implements a data struc- ture or an algorithm is typically instantiated with yet another class, referred to as a traits class, that defines