Polytech Paris Saclay
2024-2025
Adrien Durier
OCaml
OCaml
Typage Statique
Typage Statique
MAIS POURQUOI ?
Types Simples
4 + 1 - 2 * 2 ;;
(* - : int = 1 *)
5 / 2 ;;
(* - : int = 2 *)
1_000_005 mod 2 ;;
(* - : int = 1 *)
max_int + 1 ;;
(* - : int = - 4611686018427387904 *)int représente les entiers compris entre \( -2^{62} \) et \( 2^{62} - 1\)
INT
Types Simples
let x = 4.3e4 +. 1.2 *. -2.3
let y = 5. /. 2.
+. -. *. /. sqrt cos ...
float_of_int et inversement truncateFLOAT
Types Simples
let x = false || true
let x = 3 <= 1
let x = not (0=2) && 1 >= 3
let x = if 2 < 0 then 2.0 else (4.6 *. 1.2)true, false, not (NON), && (ET), || (OU)...
<= est un peu comme une fonction qui prend 2 entiers et retourne un booléen !!
BOOL
if exp1 then exp2 else exp3l'expression exp1 doit être de type bool
let x = if 2 < 0 then 2.0 else (4.6 *. 1.2)if exp1 then exp2 else exp3exp1 doit être de type bool
let x = false || true
let x = 3 <= 1
let x = not (0=2) && 1 >= 3
let x = if 2 < 0 then 2.0 else (4.6 *. 1.2)Types Simples
let x = false || true
let x = 3 <= 1
let x = not (0=2) && 1 >= 3
let x = if 2 < 0 then 2.0 else (4.6 *. 1.2)true, false, not (NON), && (ET), || (OU)...
<= est un peu comme une fonction qui prend 2 entiers et retourne un booléen !!
BOOL
if exp1 then exp2 else exp3l'expression exp1 doit être de type bool
OCaml
Expressions
Les expressions sont les blocs de bases d'OCaml
⚠ Deux Règles:
let x = 3 * 4Donc, ceci n'est pas une expression !!!!
Expressions
Les expressions sont les blocs de bases d'OCaml
3 * 4Par contre, ceci est une expression !!!!
⚠ Deux Règles:
Expressions
Les expressions sont les blocs de bases d'OCaml
⚠ Deux Règles:
3 * xPar contre, ceci est une expression !!!!
Expressions
x + 7
true
x > 8
if x then y else zLes lignes suivantes sont également des expressions (et ont donc un type) !
bool
int
bool
?
→ Si on connaît le type des variables !
Expressions
if
x
then
y
else
z?
x: ?
z: ?
y: ?
Expressions
if
x
then
y
else
z?
x: bool
z: ?
y: ?
=
=
Expressions
if
x
then
(if b
then (0 + 4)
else 1)
else
z?
x: bool
z: ?
Expressions
if
x
then
(if b
then (0 + 4)
else 1)
else
zint
x: bool
z: int
b: bool
Expressions
(if
x
then
(if b
then (0 + 4)
else 1)
else
z)
+ 7int
x: bool
z: int
b: bool
Expressions
(if
(0 < 3)
then
(if b
then (0 + 4)
else 1)
else
z)
+ 7int
x: bool
z: int
Expressions
(if
(0 <
(if (not b)
then z
else (z + 1)))
then
(if b
then (0 + 4)
else 1)
else
z)
+ 7int
x: bool
z: int
b: bool
(if
(0 <
(if (not z)
then z
else (z + 1)))
then
(if b
then (0 + 4)
else 1)
else
z)
+ 7(if
(0 <
(if (not b)
then z
else (z + 1)))
then
(if b
then (0 + 4)
else 1)
else
z)
+ 7let f b z =
(if
(0 <
(if (not b)
then z
else (z + 1)))
then
(if b
then (0 + 4)
else 1)
else
z) + 7
let f b z =
(if
(0 <
(if (not b)
then z
else (z + 1)))
then
(if b
then (0 + 4)
else 1)
else
z) + 7
let x = f true 3OCaml
1. Un programme est une succession de déclarations let
let x = 3
let y = 4
let f x = x * y
let y = f 52. On ne peut pas utiliser une déclarations let dans une expression
let f x = let x = 4;
blablabla + x ...3. Il n'y a pas de variables, que des constantes.
Donc, elles sont immuables!
let x = 3
let y = 4 * x
let x = 53. Il n'y a pas de variables, que des constantes.
Donc, elles sont immuables!
let x = 3
let y = 4 * x
let x = 5y?3. Il n'y a pas de variables, que des constantes.
Donc, elles sont immuables!
let x = 3
let y = 4 * x
let x = 5y vaut bien 12 (une bonne fois pour toutes!)
y?3. Il n'y a pas de variables, que des constantes.
Donc, elles sont immuables!
let x = 3
let y = 4 * x
let x = 5y vaut bien 12 (une bonne fois pour toutes!)
4. Si je veux lancer une fonction mais que je me fiche du résultat, je peux écrire :
let _ = Printf.printf "AAAAAH MON PROF EST FOU"5. Je peux déclarer des fonctions des deux façons suivante:
let f x = x * 3
let f = fun x -> x * 3Et donc, en toute logique......
fun x -> x * 3Est une expression !
Quel est son type?
int -> intOCaml
Curryfication
Quand je déclare une fonction à plusieurs arguments, je l'écris ainsi:
let f x y = x + y
(* ou *)
let f = fun x y -> x + yCar cette fonction est de Type:
int -> int -> intlet f1 = fun x -> x + 1
let f2 = fun x y -> x *. y
let f3 = fun x y z -> x <= y + z
let id = fun x -> x
Quel est le type de ces fonctions?
f1: int -> intf2: float -> float -> floatf3: int -> int -> int -> boolid: 'a -> 'alet f1 = fun x -> x + 1
let f2 = fun x y -> x *. y
let f3 = fun x y z -> x <= y + z
let id = fun x -> x
let f1 = fun x -> x + 1
let f2 = fun x y -> x *. y
let f3 = fun x y z -> x <= y + z
let id = fun x -> x
let f1 = fun x -> x + 1
let f2 = fun x y -> x *. y
let f3 = fun x y z -> x <= y + z
let id = fun x -> x