• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
OCaml Walkthrough
 

OCaml Walkthrough

on

  • 2,437 views

a quick overview of what ocaml has to offer

a quick overview of what ocaml has to offer

Statistics

Views

Total Views
2,437
Views on SlideShare
2,436
Embed Views
1

Actions

Likes
2
Downloads
30
Comments
0

1 Embed 1

http://a0.twimg.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    OCaml Walkthrough OCaml Walkthrough Presentation Transcript

    • What is ocaml? basic programmingmodules/signatures/functors object orientation tools things to read OCaml walkthrough Romain Slootmaekers June 24, 2011 Romain Slootmaekers OCaml walkthrough
    • What is ocaml? basic programming modules/signatures/functors object orientation tools things to readTable of Contents I 1 What is ocaml? 2 basic programming functions types polymorphism higher order functions imperative features labels/variants 3 modules/signatures/functors modules and signatures first class modules functors 4 object orientation Romain Slootmaekers OCaml walkthrough
    • What is ocaml? basic programming modules/signatures/functors object orientation tools things to readTable of Contents II What is there? ad hoc objects basic OO parametrization ad hoc interfaces 5 tools 6 things to read Romain Slootmaekers OCaml walkthrough
    • What is ocaml? basic programming modules/signatures/functors object orientation tools things to readWhat is OCaml? ML variant other ML variants:SML, F#, Moscow ML, Mythryl, . . . OCaml variants:MetaOCaml, JoCaml, OCamljs, . . . compiled/interpreted byte-code interpreter ←→ native code (speed ≈ * 7) type inference # let x = 5;; val x : int = 5 strict typing # [ 3 ; ”XX ” ] ; ; E r r o r : This e x p r e s s i o n has type s t r i n g but an e x p r e s s i o n was e x p e c t e d o f t y p e i n t Gc,OO,simple FFI, . . . Romain Slootmaekers OCaml walkthrough
    • What is ocaml? functions basic programming types modules/signatures/functors polymorphism object orientation higher order functions tools imperative features things to read labels/variants Trivial Examples(1)let square x = x ∗ xl e t rec fac n = if n = 0 $ ocamlc -i trivial.ml then 1 val square : int -> int e l s e n ∗ f a c ( n−1) val fac : int -> intl e t rec fac2 = function val fac2 : int -> int | 0 −> 1 | n −> n ∗ f a c ( n−1) Romain Slootmaekers OCaml walkthrough
    • What is ocaml? functions basic programming types modules/signatures/functors polymorphism object orientation higher order functions tools imperative features things to read labels/variantsTrivial Examples(2) l e t avg x y = ( x +. y ) / . 2 . l e t ( ∗∗∗ ) = avg $ ocamlc -i trivial2.ml val avg : float -> float -> float val ( *** ) : float -> float -> float Romain Slootmaekers OCaml walkthrough
    • What is ocaml? functions basic programming types modules/signatures/functors polymorphism object orientation higher order functions tools imperative features things to read labels/variants builtinslet l0 = 1 : : 2 : : [ ] ; ; $ ocamlc -i simple data.mllet l1 = l0 @ [ 3 ; 4 ] ; ;let l2 = [(1 ,1);(2 ,3)] ; ; val l0 : int listl e t s 0 = "A string " ; ; val l1 : int lists 0 . [ 0 ] <− ’ a ’ ; ; val l2 : (int * int) listlet i = 42;; val s0 : stringl e t i 3 2 = 42 l ; ;l e t i 6 4 = 42L ; ; val i : intl e t a0 = [ | 1 . 1 ; 2 . 2 | ] ; ; val i32 : int32a0 . ( 0 ) <− 0 . 0 ; ; val i64 : int64s0 ; ; val a0 : float array Romain Slootmaekers OCaml walkthrough
    • What is ocaml? functions basic programming types modules/signatures/functors polymorphism object orientation higher order functions tools imperative features things to read labels/variantsbuild your own type f l i s t = | Empty | F l i s t of ( f l o a t ∗ f l i s t ) let ( ˆ. ) a b = F l i s t (a , b) l e t rec s i z e = function | Empty −> 0 | Flist ( , s ) −> 1 + s i z e s let () = l e t f l = 3 . 0 ˆ . 2 . 0 ˆ . 1 . 0 ˆ . Empty i n let s = s i z e f l in P r i n t f . p r i n t f " the size is %in" s ; ; Romain Slootmaekers OCaml walkthrough
    • What is ocaml? functions basic programming types modules/signatures/functors polymorphism object orientation higher order functions tools imperative features things to read labels/variantsrecords type s u i t = | H e a r t s | Diamonds | C l u b s | S p a d e s type r a n k = L o f i n t | J | Q | K | A type c a r d = { s u i t : s u i t ; rank : rank } type hand = c a r d a r r a y l e t make card s r = { s u i t = s ; rank = r } l e t my hand = [ | m a k e card Hearts (L 2); make card Diamonds ( L 3 ) ; make card Clubs (L 4 ) ; make card Spades ( L 5 ) ; |] Romain Slootmaekers OCaml walkthrough
    • What is ocaml? functions basic programming types modules/signatures/functors polymorphism object orientation higher order functions tools imperative features things to read labels/variantspolymorphic functions l e t rec s i z e = function | [ ] −> 0 | : : r e s t −> 1 + s i z e r e s t let () = P r i n t f . p r i n t f " s1 = %in; s2 = %in" ( size [1;2;3]) ( size [ ’c ’; ’h ’; ’ a ’; ’ r ’ ] ) ; ; $ ocamlc -i pfunc.ml val size : ’a list -> int = <fun> Romain Slootmaekers OCaml walkthrough
    • What is ocaml? functions basic programming types modules/signatures/functors polymorphism object orientation higher order functions tools imperative features things to read labels/variantspolymorphic functions(2) l e t rec qsort = function | [ ] −> [ ] | p : : r e s t −> let i s l e s s x = x < p in let l , r = List . partition i s l e s s rest in qsort l @ [ p ] @ qsort r $ ocamlc -i pfunc2.ml val qsort : ’a list -> ’a list = <fun> Romain Slootmaekers OCaml walkthrough
    • What is ocaml? functions basic programming types modules/signatures/functors polymorphism object orientation higher order functions tools imperative features things to read labels/variantspolymorphic types type ’ a b t r e e = | Empty | Node o f ’ a ∗ ’ a b t r e e ∗ ’ a b t r e e let rec i n s e r t a = function | Empty −> Node ( a , Empty , Empty ) | Node ( e , l , r ) as n when a = e −> n | Node ( e , l , r ) when a < e −> Node ( e , i n s e r t a l , r ) | Node ( e , l , r ) −> Node ( e , l , i n s e r t a r ) l e t rec f r o m l i s t t r e e = function | [ ] −> t r e e | e : : e s −> f r o m l i s t ( i n s e r t e t r e e ) e s l e t c t r e e = f r o m l i s t Empty [ ’ a ’ ; ’ l ’ ; ’ u ’ ; ’ m’ ; ’ i ’ ; ’ n ’ ; ’ u ’ ; ’ m ’ ] l e t i t r e e = f r o m l i s t Empty [ 3 ; 4 ; 5 ; 1 2 ; 5 ; 6 ; 1 ; 3 ] ; ; Romain Slootmaekers OCaml walkthrough
    • What is ocaml? functions basic programming types modules/signatures/functors polymorphism object orientation higher order functions tools imperative features things to read labels/variantshigher order functions l e t rec f o l d l e f t f acc = function | [ ] −> a c c | x : : x s −> f o l d l e f t f ( f a c c x ) x s let = P r i n t f . p r i n t f " sum =% in" ( f o l d l e f t (+) 0 [ 1 ; 2 ; 3 ; 4 ; 5 ] ) ; ; $ ocamlc -i ho.ml val fold_left : (’a -> ’b -> ’a) -> ’a -> ’b list -> ’a = <fun> Romain Slootmaekers OCaml walkthrough
    • What is ocaml? functions basic programming types modules/signatures/functors polymorphism object orientation higher order functions tools imperative features things to read labels/variants curryingopen L i s t $ ocamlc -i curry.mll e t sum = f o l d l e f t (+) 0 val sum : int list -> intl e t prod = f o l d l e f t ( ∗ ) 1 val prod : int list -> int val len : ’a list -> int val oops : ’_a list -> intl e t len xs = f o l d l e f t ( fun a −> a+1) 0 x s The type of this expression, ’_a list -> int,l e t oops = f o l d l e f t contains type variables ( fun a −> a+1) 0 that cannot be generalized Romain Slootmaekers OCaml walkthrough
    • What is ocaml? functions basic programming types modules/signatures/functors polymorphism object orientation higher order functions tools imperative features things to read labels/variants for/while/referenceslet () = f o r i = 0 to 5 do P r i n t f . p r i n t f "i =% i " i done ; print newline (); $ ocaml for.ml let j = r e f 0 in i=0 i=1 i=2 i=3 i=4 i=5 w h i l e ! j <= 5 do j=0 j=1 j=2 j=3 j=4 j=5 P r i n t f . p r i n t f "j =% i " ! j ; incr j done ; print newline ();; Romain Slootmaekers OCaml walkthrough
    • What is ocaml? functions basic programming types modules/signatures/functors polymorphism object orientation higher order functions tools imperative features things to read labels/variants labelstype p o i n t = { x : f l o a t ; y : f l o a t ; z : f l o a t }l e t make point ˜x ˜y ˜z = {x ; y ; z}l e t p2s p = P r i n t f . s p r i n t f "{x =%.2 f;y =%.2 f;z =%.2 f}" p.x p.y p.zlet () = l e t a , b , c = 1 . 0 , 2 . 0 , 3 . 0 i n $ ocaml labels.ml l e t p0 = m a k e p o i n t p0={x=1.00;y=2.00;z=3.00} ˜x : a ˜y : b ˜z : c in p1={x=4.00;y=5.00;z=6.00} let x , y , z = 4.0 , 5.0 , 6.0 in l e t p1 = m a k e p o i n t ˜x ˜y ˜z in P r i n t f . p r i n t f " p0 =% s np1 =% sn" ( p 2 s p0 ) ( p 2 s p1 ) ; ; Romain Slootmaekers OCaml walkthrough
    • What is ocaml? functions basic programming types modules/signatures/functors polymorphism object orientation higher order functions tools imperative features things to read labels/variantsvariants let f = function | ‘On −> 1 | ‘ O f f −> 0 | ‘ Number n −> n | −> −1;; L i s t . map f [ ‘ On ; ‘ O f f ; ‘ Whatever ] ; ; $ ocamlc -i variants.ml val f : [> ‘Number of int | ‘Off | ‘On ] -> int Romain Slootmaekers OCaml walkthrough
    • What is ocaml? basic programming modules and signatures modules/signatures/functors first class modules object orientation functors tools things to readExample:lookup l e t empty k = r a i s e N o t f o u n d l e t lookup t k = t k let insert t a b k = if k = a then b else t k $ ocamlc -i mod1.ml val empty : ’a -> ’b val lookup : (’a -> ’b) -> ’a -> ’b val insert : (’a -> ’b) -> ’a -> ’b -> ’a -> ’b Romain Slootmaekers OCaml walkthrough
    • What is ocaml? basic programming modules and signatures modules/signatures/functors first class modules object orientation functors tools things to read in a modulemodule Lookup = s t r u c t $ ocamlc -i mod2.ml l e t empty k = r a i s e N o t f o u n d l e t lookup t k = t k module Lookup : sig let insert t a b k = val empty : ’a -> ’b if k = a then b val lookup : (’a -> ’b) -> ’ else t k -> ’bend val insert : (’a -> ’b) -> ’ -> ’bl e t t 0 = Lookup . empty ; ; endopen Lookup val t0 : ’a -> ’bl e t t 1 = i n s e r t t 0 5 " five " ; ; val t1 : int -> stringl e t t2 = lookup t0 5 ; ; val t2 : ’a Romain Slootmaekers OCaml walkthrough
    • What is ocaml? basic programming modules and signatures modules/signatures/functors first class modules object orientation functors tools things to readwith signature module L o o k u p f : s i g type ( ’ a , ’ b ) t = ’ a −> ’ b v a l empty : ( ’a , ’b) t v a l l o o k u p : ( ’ a , ’ b ) t −> ’ a −> ’ b v a l i n s e r t : ( ’ a , ’ b ) t −> ’ a −> ’ b −> ( ’ a , ’ b ) t end = s t r u c t type ( ’ a , ’ b ) t = ’ a −> ’ b l e t empty k = r a i s e N o t f o u n d l e t lookup t k = t k let insert t a b k = if k = a then b else t k end l e t t 0 = L o o k u p f . empty ; ; l e t t 1 = L o o k u p f . i n s e r t t 0 5 " five " ; ; l e t t2 = Lookup f . lookup t0 5 ; ; Romain Slootmaekers OCaml walkthrough
    • What is ocaml? basic programming modules and signatures modules/signatures/functors first class modules object orientation functors tools things to read multiple implementations open Lookupmodule type L = s i g module F = ( s t r u c t type t type t = i n t −> s t r i n g v a l empty : u n i t −> t l e t empty k = r a i s e N o t f o u n d val lookup : l e t lookup t k = t k t −> i n t −> s t r i n g let insert t a b k = val insert : if k = a t −> i n t −> s t r i n g −> t then bend else t k end : L ) Romain Slootmaekers OCaml walkthrough
    • What is ocaml? basic programming modules and signatures modules/signatures/functors first class modules object orientation functors tools things to readmultiple implementations(2) module A = ( s t r u c t type t = ( i n t ∗ s t r i n g ) l i s t l e t empty ( ) = [ ] l e t r e c l o o k u p t k = match t wi th | [ ] −> r a i s e N o t f o u n d | ( a , b ) : : r e s t −> if k = a then b else lookup r e s t k let insert t a b = (a , b ) : : t end : Lookup . L ) Romain Slootmaekers OCaml walkthrough
    • What is ocaml? basic programming modules and signatures modules/signatures/functors first class modules object orientation functors tools things to readmultiple implementations(3) module H = ( s t r u c t type t = ( i n t , s t r i n g ) H a s h t b l . t l e t empty ( ) = H a s h t b l . c r e a t e 17 l e t lookup t k = Hashtbl . f i n d t k l e t i n s e r t t a b = H a s h t b l . add t a b ; t let nobody will know () = () end : Lookup . L ) Romain Slootmaekers OCaml walkthrough
    • What is ocaml? basic programming modules and signatures modules/signatures/functors first class modules object orientation functors tools things to readexample usage open L o o k u p f open Lookup h open L o o k u p a l e t f 0 = F . empty ( ) ; ; l e t f 1 = F . i n s e r t f 0 5 " five " ; ; l e t fv = F . lookup f0 5 ; ; l e t l 0 = A . empty ( ) ; ; l e t l 1 = A . i n s e r t l 0 5 " five " ; ; l e t l v = A. lookup l1 5 ; ; l e t h0 = H . empty ( ) ; ; l e t h1 = H . i n s e r t h0 5 " five " ; ; l e t hv = H . l o o k u p h1 5 ; ; Romain Slootmaekers OCaml walkthrough
    • What is ocaml? basic programming modules and signatures modules/signatures/functors first class modules object orientation functors tools things to readwatch out open Lookup h open L o o k u p a l e t no way= H . empty ( ) ; ; l e t o o p s = A . i n s e r t no way 5 ” f i v e ” ; ; $ ocamlbuild liskov.byte File "liskov.ml", line 5, characters 20-26: Error: This expression has type Lookup_h.H.t but an expression was expected of type Lookup_a.A.t Romain Slootmaekers OCaml walkthrough
    • What is ocaml? basic programming modules and signatures modules/signatures/functors first class modules object orientation functors tools things to readexample module type DEVICE = s i g v a l i n i t : u n i t −> u n i t end module PDF = s t r u c t l e t i n i t ( ) = ( ) end module SVG = s t r u c t l e t i n i t ( ) = ( ) end l e t d e v i c e s = H a s h t b l . c r e a t e 17 l e t ( ) =H a s h t b l . add d e v i c e s " PDF " ( module PDF : DEVICE ) l e t ( ) =H a s h t b l . add d e v i c e s " SVG " ( module SVG : DEVICE ) module D e v i c e = ( val ( t r y Hashtbl . f i n d d e v i c e s Sys . argv . ( 1 ) wi t h N o t f o u n d −> p r e r r e n d l i n e " Unknown device " ; e x i t 2 ) : DEVICE ) l e t () = Device . i n i t ( ) ; ; Romain Slootmaekers OCaml walkthrough
    • What is ocaml? basic programming modules and signatures modules/signatures/functors first class modules object orientation functors tools things to readMap example module S t r i n g M a p = Map . Make ( S t r i n g ) open S t r i n g M a p let () = l e t s 0 = add "5" " five " empty i n l e t v = f i n d "5" s 0 i n P r i n t f . p r i n t f "v =% sn" v ; ; Romain Slootmaekers OCaml walkthrough
    • What is ocaml? basic programming modules and signatures modules/signatures/functors first class modules object orientation functors tools things to read ROYmodule type E = s i g type t l e t r e c add x s= v a l compare : t −> t −> i n t f a i l w i t h " todo "end let r e c mem x = f u n c t i o nmodule S e t = f u n c t o r ( E l t : E ) −> | Empty −> r a i s e N o t f o u n dstruct | Node ( e , , ) when e = x −> type e l e m e n t = E l t . t | Node ( e , l , r ) −> type t r e e = i f E l t . compare e x < 0 | Empty then mem x l | Node o f ( e l e m e n t ∗ e l s e mem x r tree ∗ tree ) end l e t empty = Empty module S t r i n g S e t = S e t ( S t r i n g ) Romain Slootmaekers OCaml walkthrough
    • What is ocaml? What is there? basic programming ad hoc objects modules/signatures/functors basic OO object orientation parametrization tools ad hoc interfaces things to readobjectsclassesinterfacesinitializers(multiple) inheritancepublic/privateparametrizationopen types Romain Slootmaekers OCaml walkthrough
    • What is ocaml? What is there? basic programming ad hoc objects modules/signatures/functors basic OO object orientation parametrization tools ad hoc interfaces things to readimmediate objects l e t p =o b j e c t v a l mutable x = 0 method g e t x = x method move d = x <− x + d end $ ocamlc -i ad hoc.ml val p : < get_x : int; move : int -> unit > Romain Slootmaekers OCaml walkthrough
    • What is ocaml? What is there? basic programming ad hoc objects modules/signatures/functors basic OO object orientation parametrization tools ad hoc interfaces things to read simple class with immutable objectsclass point x y z = l e t l = s q r t ( x ∗ . x +. y ∗ . y +. z ∗ . z ) i nobject ( s e l f ) method l e n g t h = l method t o s t r i n g = $ ocamlc -i point class.ml Printf . sprintf class point : " {%.2 f ;%.2 f ;%.2 f}" x y z float ->end float -> float -> objectlet () = method length : float l e t p = new p o i n t 1 . 2 . 3 . method to_string : string in Printf . printf end "p =% s ;|| p || = %2. fn" (p # to string ) (p # length ) ; ; Romain Slootmaekers OCaml walkthrough
    • What is ocaml? What is there? basic programming ad hoc objects modules/signatures/functors basic OO object orientation parametrization tools ad hoc interfaces things to read class typesc l a s s type p r i n t a b l e = o b j e c t method t o s : u n i t −> s t r i n gendclass point x y = l e t p0 = new p o i n t 0. 0.o b j e c t ( s e l f : #p r i n t a b l e ) l e t c0 = new c i r c l e p0 1 . 0 method t o s ( ) = l e t o b j e c t s = [ p0 ; c0 ] P r i n t f . s p r i n t f " {%.2 f ;%.2 f}" let () = x y List . iterend ( fun o −>class circle p r = l e t os = o # to s () ino b j e c t ( s e l f : #p r i n t a b l e ) Printf . printf "%sn" o s ) method t o s ( ) = objects ; ; P r i n t f . s p r i n t f "C (%s ,%2. f)" (p # to s ()) rend Romain Slootmaekers OCaml walkthrough
    • What is ocaml? What is there? basic programming ad hoc objects modules/signatures/functors basic OO object orientation parametrization tools ad hoc interfaces things to readgotchas calls on self during construction? downcasts? polymorphic methods? polymorphic exceptions? Romain Slootmaekers OCaml walkthrough
    • What is ocaml? What is there? basic programming ad hoc objects modules/signatures/functors basic OO object orientation parametrization tools ad hoc interfaces things to readparametrization class [ ’ a ] l i n k e d l i s t (a : ’ a) = object v a l mutable n e x t = None method s e t n e x t ( t a i l : ’ a l i n k e d l i s t ) = n e x t <− Some t a i l end let () = l e t l 0 = new l i n k e d l i s t "a" i n l e t l 1 = new l i n k e d l i s t "b" i n l0 # set next l1 ; ; Romain Slootmaekers OCaml walkthrough
    • What is ocaml? What is there? basic programming ad hoc objects modules/signatures/functors basic OO object orientation parametrization tools ad hoc interfaces things to readexample let log o o x = l e t k s = l e t os = o # t o s t r i n g ( ) in L w t l o g . debug ( o s ˆ": " ˆ s ) in Printf . ksprintf k x $ ocamlfind ocamlc -i -package lwt.unix log o.ml val log_o : < to_string : unit -> string; .. > -> (’a, unit, string, unit Lwt.t) format4 -> ’a Romain Slootmaekers OCaml walkthrough
    • What is ocaml? basic programming modules/signatures/functors object orientation tools things to readocamlc, ocamlopt, ocamlc.opt, ocamlopt.optocamlfindocamlbuildemacs, eclipse, omlet, camelia, . . .others Romain Slootmaekers OCaml walkthrough
    • What is ocaml? basic programming modules/signatures/functors object orientation tools things to readocamlfind knows which packages you have fills in details for the compilers Example $ ocamlopt EchoServer.ml File "EchoServer.ml", line 1, characters 0-1: Error: No implementations provided for the following modules: Unix referenced from EchoServer.cmx $ ocamlopt.opt /.../lib/ocaml/unix.cmxa EchoServer.ml $ ocamlfind ocamlopt -package unix -linkpkg EchoServer.ml Romain Slootmaekers OCaml walkthrough
    • What is ocaml? basic programming modules/signatures/functors object orientation tools things to readocamlbuild builds things declarative target extension plugins for (c)libraries, documentation, profiler,... tags myocamlbuild.ml Romain Slootmaekers OCaml walkthrough
    • What is ocaml? basic programming modules/signatures/functors object orientation tools things to readIDEs emacs: tuareg eclipse: ocaide http://www.algo-prog.info/ocaide/ camelia http://camelia.sourceforge.net/ vim: omlet . . . lots of dead projects Romain Slootmaekers OCaml walkthrough
    • What is ocaml? basic programming modules/signatures/functors object orientation tools things to readdeveloping applications with OCamlhttp://caml.inria.fr/pub/docs/oreilly-book/The Objective Caml system http://caml.inria.fr/pub/docs/manual-ocaml/index.htmlJane street bloghttp://ocaml.janestreet.com/?q=node/89Lambda The Ultimate http://lambda-the-ultimate.org/ Romain Slootmaekers OCaml walkthrough
    • What is ocaml? basic programming modules/signatures/functors object orientation tools things to readadvanced stuff . . . Next time Romain Slootmaekers OCaml walkthrough