27/02/1432 Présentation du cours • Cours 18 h ; TD 18 h ; TP 18 h • 9 groupes de TD et TP Programmation fonctionnelle commencent la semaine prochaine Cours 1 : les types simples Licence 1 Année 2010 – 2011 • Contrôle – Contrôle continu • 3 CC : 14 mars , 18 avril et 16 mai • 1 mini-projet sur machine séance 12 en TP – Pas d’examen Par N. VINCENT Licence 1 - programmation fonctionnelle Contenu du cours Rappels • 1er semestre Structures de contrôle de base en langage C • Objectif du 2e semestre – Apprendre un nouveau langage – programmation fonctionnnelle – Conforter les éléments d’algorithmie – Structures – fonctions – Instructions 3 Le langage CAML Licence 1 - programmation fonctionnelle 4 Caractéristiques du langage • Issu du -calcul, en 1977, le langage ML • 10 ans plus tard le langage Caml : Categorical Abstract Machine Language • Langage fonctionnel : • Types de données très rigoureux • Les types sont évalués au cours des calculs – Pas nécessaire de les déclarer – déclaration de fonctions, – les résultats des fonctions étant de tout type • Langage évolué • Langage interprété 1 Problème à résoudre Analyse de la solution choisie Écriture de l’algorithme - validation Choix d’un langage • Écriture du programme – Permet la programmation fonctionnelle et impérative Licence 1 - programmation fonctionnelle • • • • – Ici Caml • Le choix du Caml Licence 1 - programmation fonctionnelle 2 5 • Une variable ne peut pas être utilisée dans un calcul sans avoir une valeur • Un programme est une suite de définitions de fonctions et de données • On ne peut utiliser une fonction avant qu’elle ait été définie Licence 1 - programmation fonctionnelle 6 27/02/1432 Langage interprété L'interpréteur Écriture d’une expression 2+2 Lecture par l’interpréteur 2 + 2 ;; Évaluation par l’interpréteur Retour du type et de la valeur ou message d’erreur #2 + "caml" ;; Toplevel input: >2 + "caml" ;; > ^^^^^^ This expression has type string, but is used with type int. Licence 1 - programmation fonctionnelle # 2+2 ;; - : int = 4 • Écriture d’une expression – Après le « prompt » # – Jusqu'au lancement ;; • Retour – Indication de réponse – Type de l'expression déduit – Valeur de l'expression 7 - : int -> int = <fun> #let ... = ... ;; #let x = 5 ;; x : int = 5 La valeur ne peut être modifiée connue dans la session #let y = `c` ;; y : char = `c` #let f = fun x -> x+1 ;; f : int -> int = <fun> #let x = 2 + 5 ;; x : int = 7 Ou mieux #let g x = x + 1 ;; g : int -> int = <fun> #g (-8) ;; - : int = -7 #f 3 ;; - : int = 4 #let x = 3 and y = 5 ;; x : int = 3 y : int = 5 Licence 1 - programmation fonctionnelle • 'a : pas encore défini #let f x y = x > y;; f : 'a -> 'a -> bool = <fun> #let f x y = x > y;; 9 Licence 1 - programmation fonctionnelle 10 Conversions de type Opérateurs arithmétiques float_of_int Liés au type des valeurs char_of_int entiers #float_of_int 4 ;; - : float = 4.0 #char_of_int 69 ;; - : char = `E` #string_of_int 69 ;; string_of int - : string = "69" + - * / mod abs réels int_of_char +. -. *. /. sqrt exp A valeur booléenne int_of_float Égalité = , comparaison < > <= >= int_of_string Les deux arguments doivent être de même type 2 8 Définition et affectation true false 2 =2 bool : booléen 30 30 int : entier entre -2 et 2 -1 228 'c' char : caractère string : chaîne de caractères "nom" float : réel (non compatible avec les entiers) 2. () unit : unique valeur le vide #fun x -> x + 1 ;; fun : fonction Licence 1 - programmation fonctionnelle - : int 2 Licence 1 - programmation fonctionnelle Types simples • • • • • • • # 1 + 1;; 11 #int_of_char `E` ;; - : int = 69 #int_of_float 7.3 ;; - : int = 7 #int_of_float 7.8 ;; - : int = 7 #int_of_string "452" ;; - : int = 452 #int_of_string "caml" ;; 12 Uncaught Failure "int_of_string" Licence 1 -exception: programmation fonctionnelle 27/02/1432 La rigueur des types #3 + 5 ;; - : int = 8 #3 + 1.2 ;; Toplevel input: >3 + 1.2 ;; > ^^^ This expression has type float, but is used with type int. #3 +. 1.2 ;; Toplevel input: >3 +. 1.2 ;; >^ This expression has type int, but is used with type float. Le type indéterminé #let x = 3. and y = 5. and z = 1 ;; x : float = 3.0 y : float = 5.0 z : int = 1 #let f x y = x > y;; f : 'a -> 'a -> bool = <fun> #x+.y;; - : float = 8.0 #x + z ;; Toplevel input: >x + z ;; >^ This expression has type float, but is used with type int. Licence 1 - programmation fonctionnelle #f true false ;; - : bool = true #f 3.7 4.9 ;; - : bool = false #f false true ;; - : bool = false #f `A` `a` ;; - : bool = false #f "Caml" "C" ;; - : bool = true #x +. float_of_int z ;; - : float = 4.0 #3. +. 1.2 ;; - : float = 4.2 #f 25 40 ;; - : bool = false 13 Licence 1 - programmation fonctionnelle Opérateurs logiques Égalité = Impression Une fonction existe pour chaque type #(2>1)=(`c`>`a`) ;; - : bool = true #let x = 5 ;; x : int = 5 #x = 6 ;; - : bool = false Et & && #2 < 3 && (-3) < 3 ;; - : bool = true #2<3 & (-3)<3 ;; - : bool = true print_float Ou or || # x < y || y < x ;; - : bool = true #x<y or y<x ;; - : bool = true print_char x : char = `c` Non not #let x = 5 ;; x : int = 5 #print_int x ;; 5- : unit = () print_int #let x = `c` ;; #let x = 5. ;; x : float = 5.0 #print_float x ;; 5.0- : unit = () #print_char x ;; c- : unit = () #not (2 > 1) ;; - : bool = false Licence 1 - programmation fonctionnelle 14 15 #let x = "caml" ;; x : string = "caml" print_string #print_string x ;; caml- : unit = () print_newline () 16 Licence 1 - programmation fonctionnelle Déclaration locale Déclaration locale Une variable peut être définie par une expression 1 dans une expression Le type du résultat est celui de expression La variable doit avoir un type compatible avec celui de son occurrence dans expression ou ne pas apparaître #let x = 12 in x +. 5. ;; #let x = 12 in x + 5 ;; Toplevel input: - : int = 17 >let x = 12 in x +. 5. ;; > ^ #let x = 3 ;; #let x = "caml" ;; x : int = 3 x : string = "caml" #let x = 5 in x + 2 ;; #let x = 12 in x + 5 ;; - : int = 7 - : int = 17 #x;; #x ;; - : int = 3 - : string = "caml" This expression has type int, #let x = 12 in sqrt 12.6 ;; but is used with type float. - : float = 3.54964786986 Licence 1 - programmation fonctionnelle 3 17 Licence 1 - programmation fonctionnelle 18 27/02/1432 Déclaration d'une fonction simple La conditionnelle suit la structure de la forme algorithmique Ce n'est pas une instruction mais une expression qui peut prendre 2 valeurs possibles If condition then expression 1 else expression 2 Définition d'une fonction valeur absolue sur les réels #let f x = x * x + 2 * x + 5 ;; f : int -> int = <fun> #f 1 ;; #f 0 ;; #f (-1) ;; - : int = 8 - : int = 5 - : int = 4 #let x = 5 ;; #f 7 ;; x : int = 5 - : int = 8 #let f x = x + 1 ;; #f x ;; f : int -> int = <fun> - : int = 6 #let f a b = a + b ;; #f 3 7 ;; #f (2+4) (-8) ;; f : int -> int -> int = <fun> - : int = 10 - : int = -2 Licence 1 - programmation fonctionnelle 19 #let h x = if x < 0. then -. x else x ;; #abs 5. ;; h : float -> float = <fun> Toplevel input: #h 4. ;; >abs 5. ;; - : float = 4.0 > ^^ #h (-45.) ;; This expression has type float, but is used with type int. 20 = 45.0 fonctionnelle Licence-1:-float programmation Conditionnelle Les points importants Les deux expressions doivent avoir le même type • • • • #let x = 5 ;; x : int = 5 #let y = 2 ;; y : int = 2 #if x > y then print_int x else y ;; Toplevel input: >if x > y then print_int x else y ;; > ^ This expression has type int, but is used with type unit. Licence 1 - programmation fonctionnelle 4 21 Les types Les opérateurs La conditionnelle Les fonctions Licence 1 - programmation fonctionnelle 22