>
OCaml est:
Langage développé à l'INRIA (équipes Formel, Cristal, puis Gallium).
Recherche sur les systèmes de types:
Quelques dates:
Aujourd'hui OCaml est largement utilisé dans la communauté académique (pas seulement pour l'enseignement) ET dans l'industrie.
$ ocamlou
$ ocaml fichier.ml ...
# 1 + 1;; - : int = 2
$ ocamlc -c -I .... fichier.ml $ ocamlc -o mon_prog fichier.cmo fichier2.cmo ...
$ ocamlopt -c -I .... fichier.ml $ ocamlopt -o mon_prog fichier.cmx fichier2.cmx ...
# 1 + 1;; - : int = 2
# let x = "bonjour " ^ "le monde";; val x : string = "bonjour le monde"
# let x = 3.0 ** 2. ;; val x : float = 9.
# let y = let x = 3 in 39 + x;; val y : int = 42
# x;; - : float = 9.
# let f1 = fun x -> x + 1;; val f1 : int -> int = <fun>
# let f2 x = x * 2;; val f2 : int -> int = <fun>
# let f3 x y z = x *. y +. z;; val f3 : float -> float -> float -> float = <fun>
# let f3 = fun x -> fun y -> fun z -> x *. y +. z;; val f3 : float -> float -> float -> float = <fun>
# let f3 = fun x y -> let t = x *. y in fun z -> t +. z;; val f3 : float -> float -> float -> float = <fun>
# let f x = x ^ " le monde";; val f : string -> string = <fun> # f "bonjour";; - : string = "bonjour le monde"
# let f x y z = x + y - z;; val f : int -> int -> int -> int = <fun> # f 1 2;; - : int -> int = <fun>
# let g = f 1 2;; val g : int -> int = <fun>
# g 3;; - : int = 0 # f 1 2 3;; - : int = 0
# let f1 = fun x -> x + 1;; val f1 : int -> int = <fun>
# let f2 x = x * 2;; val f2 : int -> int = <fun>
# let h f g x = x + f x * g x;; val h : (int -> int) -> (int -> int) -> int -> int = <fun>
# h f1 f2 42;; - : int = 3654
# h f1 (fun x -> x + 10) 42;; - : int = 2278
# let g f x = (f x) + 1;; val g : ('a -> int) -> 'a -> int = <fun>
# let f1 = fun x -> x + 1;; val f1 : int -> int = <fun> # g f1 40;; - : int = 42
# let f2 x = int_of_float (x -. 4.) ;; val f2 : float -> int = <fun> # g f2 45. ;; - : int = 42
# g int_of_string;; - : string -> int = <fun> # g int_of_string "41";; - : int = 42
# g string_of_int 1;; Error: This expression has type int -> string but an expression was expected of type int -> int Type string is not compatible with type int
# List.map;; - : ('a -> 'b) -> 'a list -> 'b list = <fun>
# [];; - : 'a list = []
# let ma_liste = [ 1 ; 2 ; 3 ; 4];; val ma_liste : int list = [1; 2; 3; 4]
# let ma_liste2 = List.map string_of_int ma_liste;; val ma_liste2 : string list = ["1"; "2"; "3"; "4"]
# let ma_liste3 = List.map (fun x -> x + 1) ma_liste;; val ma_liste3 : int list = [2; 3; 4; 5]
# (1, "coucou", 1.0, [ 'c' , 'd']);; - : int * string * float * (char * char) list = (1, "coucou", 1., [('c', 'd')])
type 'a list
# ["maitre" ; "corbeau" ; "sur" ; "un" ; "arbre" ; "perché"];; - : string list = ["maitre"; "corbeau"; "sur"; "un"; "arbre"; "perch\195\169"] # [ [1.0 ; 2.0] ; [ 3.14 ; 2.72] ];; - : float list list = [[1.; 2.]; [3.14; 2.72]]
type 'a array
# [| 1 ; 2 ; 3 |];; - : int array = [|1; 2; 3|] # [| "tenait" ; "en" ; "son" ; "bec" ; "un" ; "fromage" |];; - : string array = [|"tenait"; "en"; "son"; "bec"; "un"; "fromage"|] # [| [ 'a' ; 'b' ; 'c'] ; [ 'd' ; 'e' ; 'f'] |];; - : char list array = [|['a'; 'b'; 'c']; ['d'; 'e'; 'f']|]
union
en C,struct
en C.# type 'a tree = Node of 'a tree list | Leaf of 'a;; type 'a tree = Node of 'a tree list | Leaf of 'a
# let arbre = Node [ Node [ Leaf 11 ; Leaf 9 ] ; Leaf 3 ; Node [ Leaf 4 ; Leaf 15 ] ];; val arbre : int tree = Node [Node [Leaf 11; Leaf 9]; Leaf 3; Node [Leaf 4; Leaf 15]]
# let rec tree_map f t = match t with Leaf v -> Leaf (f v) | Node nodes -> Node (List.map (tree_map f) nodes) ;; val tree_map : ('a -> 'b) -> 'a tree -> 'b tree = <fun>
# let arbre_chaines = tree_map string_of_int arbre;; val arbre_chaines : string tree = Node [Node [Leaf "11"; Leaf "9"]; Leaf "3"; Node [Leaf "4"; Leaf "15"]]
# tree_map (fun x -> x + 1) arbre;; - : int tree = Node [Node [Leaf 12; Leaf 10]; Leaf 4; Node [Leaf 5; Leaf 16]]
# List.fold_left;; - : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a = <fun>
# let f acc n = acc + n;; val f : int -> int -> int = <fun> # List.fold_left f 0 [ 1 ; 2 ; 3 ; 4 ; 5 ; 6 ] (* f (f (f (f (f (f 0 1) 2) 3) 4) 5) 6 *) ;; - : int = 21
# let rec height tree = match tree with Leaf _ -> 1 | Node nodes -> 1 + List.fold_left (fun acc t -> max acc (height t)) min_int nodes ;; val height : 'a tree -> int = <fun> # height arbre;; - : int = 3
# let cpt = ref 0;; val cpt : int ref = {contents = 0}
# print_string "coucou"; print_string "toto"; flush stdout;; coucoutoto- : unit = ()
for
et while
:
# for i = 1 to 10 do cpt := !cpt + i ; done;; - : unit = () # while !cpt < 100 do cpt := !cpt + 10; done;; - : unit = () # !cpt;; - : int = 105
# class point x y = object val mutable x = x val mutable y = y method set_x n = x <- n method set_y n = y <- n method x = x method y = y method to_string = Printf.sprintf "(%d, %d)" x y end;; class point : int -> int -> object val mutable x : int val mutable y : int method set_x : int -> unit method set_y : int -> unit method to_string : string method x : int method y : int end
# class colored_point x y color = object(self) inherit point x y as super method color = color method to_string = Printf.sprintf "%s in %s" super#to_string color end;; class colored_point : int -> int -> string -> object val mutable x : int val mutable y : int method color : string method set_x : int -> unit method set_y : int -> unit method to_string : string method x : int method y : int end
# let p = new point 0 0 ;; val p : point = <obj> # let cp = new colored_point 1 1 "blue";; val cp : colored_point = <obj> # cp#to_string;; - : string = "(1, 1) in blue"
# let debug = fun truc -> "debug: " ^ truc#to_string;; val debug : < to_string : string; .. > -> string = <fun>
# let dummy = object method to_string = "dummy" end;; val dummy : < to_string : string > = <obj> # debug p;; - : string = "debug: (0, 0)" # debug cp;; - : string = "debug: (1, 1) in blue" # debug dummy;; - : string = "debug: dummy"
# debug (object method to_int = 1 end);; Error: This expression has type < to_int : int > but an expression was expected of type < to_string : string; .. > The first object type has no method to_string
Points forts: