93
Langage de Programmation 2 (LP2) Langage de Programmation 2 (LP2) RICM3 Cours 1 : Introduction (Base de Caml) Pascal Lafourcade Polytech 2010 - 2011 1 / 62

Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

  • Upload
    dinhnhi

  • View
    222

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Langage de Programmation 2 (LP2)RICM3

Cours 1 : Introduction (Base de Caml)

Pascal Lafourcade

Polytech

2010 - 2011

1 / 62

Page 2: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Démo, DOOM

2 / 62

Page 3: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Presentation du cours

Qui ? Où ? et Quand ?

COURS : Lundi 8h00 - 9h30 Amphi 005

[email protected]

TD :

I Mardi 15h45 - 17h45 : [email protected] Mardi 13h30- 15h30 [email protected]

TP 3h : Jeudi matin UFR IMA (15 jours)

[email protected] [email protected]@komite.net

Règles de savoir vivre

3 / 62

Page 4: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Presentation du cours

Matériel

I Slides online le vendredi avant le coursI Feuille de TD une semaine avantI Feuille de DM avantI Feuille de TP avantI Page Web LP2

www-verimag.imag.fr/∼plafourc/teaching/LP2_RICM3_2010_2011.php

4 / 62

Page 5: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Presentation du cours

Note LP2

Note finale = 60% Examen + 40% max (Examen,CC)CC = 40 % DM + 60% TP

5 / 62

Page 6: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Presentation du cours

Note LP2

Note finale = 60% Examen + 40% max (Examen,CC)CC = 40 % DM + 60% TP

1 RV + 1 exo fait.

CC

I 5 DMsI 4 TPsI 1 ProjetI Note DM = Moyenne des 4 meilleures notes des 5 DMSI TP = 60 % TP-Projet + 40 % TPsI Note TPs = Moyenne des 3 meilleurs notes des 4 TPs

Possibilité de rendre d’autres travaux pour correction détaillée.5 / 62

Page 7: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Presentation du cours

Note LP2

TP-Projet à rendre pour le 5 Mai 2011 minuit par email.Tout travail non rendu dans les délais aura une note de ZERO.

Règles

I Si TP pas rendu alors 0.I Si TOUS les TPs rendus +1 note TP.I Si absence justifiée pas de correction (prévenir à l’avance).

Possibilité de demander une correction ciblée de parties de TP.

6 / 62

Page 8: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Presentation du cours

Triche

Ce n’est pas tricher que de

I Regarder le manuel ou le site de Ocaml !I http://caml.inria.fr/

I Demander aux enseignants !I Envoyer un email aux enseignants !

Tricher c’est :

I Ne pas respecter les deadlines.I Confondre Travail personnel 6= Travail en groupe.I Copier, recopier, copier-coller

Politique claire : triche = ZERO pour TOUS. 7 / 62

Page 9: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Presentation du cours

Votre Travail Personnel

Rappel : Volume de travail personel (6 ETCS = 6*24h)

8 / 62

Page 10: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Presentation du cours

Votre Travail Personnel

Rappel : Volume de travail personel (6 ETCS = 6*24h)

“J’entends, J’oublie

Je vois, Je me rappelle

Je fais, Je comprends

Confucius

8 / 62

Page 11: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Presentation du cours

Votre Travail Personnel

Rappel : Volume de travail personel (6 ETCS = 6*24h)

“J’entends, J’oublie

Je vois, Je me rappelle

Je fais, Je comprends

Confucius

I Amphi sert à entendre et à apprendreI TD sert à se rappeler et à comprendreI Projet sert à expérimenter età comprendre

8 / 62

Page 12: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Presentation du cours

Travail Personnel pour cette UE

I Préparation des TDs suite au Cours (pro-actif)I Exercices chaque semaine + DMI TP par groupeI TP-Projet

9 / 62

Page 13: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Presentation du cours

Programme du Cours

1. Bases de OCaml (Objective Categorical Abstract MachineLanguage) ...

2. Récurrence structurelle

3. Evaluation et compléments en Ocaml.

4. Modules, Foncteurs, Input Output

5. Exception, flots

6. Inférence de type (Typage)

7. Polymorphisme, référence, mutable.

8. Lambda Calcul

9. Lambda Calcul

10. Objet, Mémoire et point fixe

10 / 62

Page 14: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Presentation du cours

Programme TD

I listesI arbresI tasI trisI filesI flotsI foncteursI modulesI graphesI automatesI typageI ....

11 / 62

Page 15: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Presentation du cours

Programme TP

1. Introduction à OCAML

2. Code de Huffmann

3. Modules, foncteurs, graphes

4. Analyse lexicale par flots

5. Rami des lettres, des chiffres ...

12 / 62

Page 16: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Presentation du cours

Objectif du cours

Savoir faire

I Changer de paradigme.I Programmer dans un langage FONCTIONNEL.I Conception d’algorithmes efficaces et corrects.I Initiation aux calculs de complexité.

Outils

I Programmation fonctionnelle en Objective Caml.(Caml = Categorical Abstract Machine Language)

I TypageI Raisonnement par récurrence structurelle.I Preuve de programmes. 13 / 62

Page 17: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Presentation du cours

Plan

Presentation du cours

MotivationsProgrammation impérativeProgrammation fonctionnelleMythes et réalités sur la programmation fonctionnelle

Notions de Base

Variables

Fonctions

Type Sommes

Filtrage

Conclusion

14 / 62

Page 18: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Motivations

Plan

Presentation du cours

MotivationsProgrammation impérativeProgrammation fonctionnelleMythes et réalités sur la programmation fonctionnelle

Notions de Base

Variables

Fonctions

Type Sommes

Filtrage

Conclusion

15 / 62

Page 19: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Motivations

Programmation ?

16 / 62

Page 20: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Motivations

Programmation ?

Ensemble de techniques qui permettent d’écrire un programmeinformatique pour “discuter” avec la machine.

Ordinateur de Von Neumann 1948.

16 / 62

Page 21: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Motivations

Historique (I)

I Boulier, arithmétique ...I 1640 : La Pascaline : calculatrice mécanique de Blaise PascalI 1703 : L’arithmétique binaire par LeibnizI 1801 : Le métier à tisser programmable de J-M JacquardI 1820 : La machine à différences de Charles BabbageI 1843 : Calcul par itération (Ada Lovelace)I 1847 : L’algèbre de Boole par George Boole

17 / 62

Page 22: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Motivations

Historique (II)

I 1931 : Incomplétude de Kurt GoedelI 1936 : Machine de Turing (Alan-Turing)I 1935 : λ-Calcul (Stephen Kleene, Alonzo Church)I 1943 : ColossusI 1948 : Architecture de Von NeumannI 1948 : Invention du transistor (John Bardeen, William

Shockley et Walter Brattain)I 1956 : Automate de Noam ChomskyI 1958 : Correspondance de Curry Howard :

preuve/programme ou formule/type,I 1963 : Ordinateurs à circuit intégréI 1971 : Invention du processeur (IBM 4004 Marcian Hoff).I 2005 : Ordinateur multi-coeurs.

18 / 62

Page 23: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Motivations

Différents paradigmes de programmation (styles)

1. Impératif

2. Fonctionnel

3. Objet

4. Logique

19 / 62

Page 24: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Motivations

Différents paradigmes de programmation (styles)

1. ImpératifFortran IBM (1954), ALGOL (1958), COBOL (1960), BASIC(1963), PASCAL (1970), C (1970), ADA (1974) ...

2. FonctionnelMIRANDA (1989), HASKELL (1990), MERCURY (1995),OBJECTIVE CAML (1996), Scheme (1970), Lisp (1959),LOGO (1968) ...

3. ObjetJAVA (1995), C# (2001), VALA (2006), Objective C, EIFFEL(1986), PYTHON (1990), C++ (1985), PHP (1994),SMALLTALK (1972)...

4. Logique Prolog (1972).

ETC : pearl (1987), html (1989), Ruby (1995), csh ...

19 / 62

Page 25: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Motivations

Programmation impérative

Programmation impérative

Centrée sur la notion d’assignation

x := E

I x est un emplacement mémoire

I E est une expression :sa valeur v est temporairement associée à x

x est aussi traditionnellement appelée une variable

20 / 62

Page 26: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Motivations

Programmation impérative

Modèle de calcul

État

Défini par un n-uplet de données en mémoireDonnées décrites in fine en termes concrets

Transformations d’état

I État du programme = 〈mémoire, compteur ordinal〉I Mémoire = n-uplet d’associations 〈x , v〉

Objectif : placer le système dans un état final souhaitable⇒ connaître tous les chemins d’exécution qui mènent à un étatfinal

21 / 62

Page 27: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Motivations

Programmation fonctionnelle

Programmation fonctionnelle

Centrée sur la notion d’expression

Toute expression a une valeur et un type

I Déterminés une fois pour toutesI La valeur ne dépend pas de l’ordre d’évaluation

Pour le raisonnement :

I on ne se préoccupe que de la valeur et du type

I raisonnement par cas sur les différentes valeurs possibles+ raisonnement par récurrence

I raisonnement équationnel

toute expression peut-être remplacée par une expression égale

22 / 62

Page 28: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Motivations

Mythes et réalités sur la programmation fonctionnelle

Ce que l’on évite

Perturbations

I Dues au contrôle : chemins d’exécution indifférentsI Dues aux données : pas de pointeurs ⇒ valeurs

mathématiques

Conséquence :

I Meilleure maîtrise d’algorithmes complexesI Estimation de la complexité (temps de calcul)

23 / 62

Page 29: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Motivations

Mythes et réalités sur la programmation fonctionnelle

Ce que l’on ne perd pas

Efficacité (en Ocaml)

I Interpréteur, compilateur → code-octetI compilateur → code natif

efficacité typiquement 1/2 par rapport à C (selon applications)

Possibilité d’utiliser des traits impératifs (à bon escient)

I contrôle séquentielI données mutables, pointeurs

24 / 62

Page 30: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Motivations

Mythes et réalités sur la programmation fonctionnelle

Ce que l’on gagne

Efficacité en programmation

I Taille du code typiquement 1/10 du code C équivalentI Certains problèmes se codent naturellement en fonctionnelI Gestion mémoire automatique

I allocation/libération

I Pas de gadget : on se concentre sur l’essentiel

25 / 62

Page 31: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Motivations

Mythes et réalités sur la programmation fonctionnelle

Prix à payer

Effort

I Changement de perspectiveI Moins adapté à l’embarqué ou à la programmation système de

bas niveau (un peu comme Java)

Support

I Non adossé à une société de grande envergure (ex. Sun)(mais communauté très active dans le « libre »)

I Bibliothèques en quantité moyenneI Trop souvent enseigné à un niveau limité⇒ moins de développeurs pointus

26 / 62

Page 32: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Motivations

Mythes et réalités sur la programmation fonctionnelle

Problème *

Écrire une fonction “affine” prenant deux entiers a et b, etrenvoyant la fonction x → ax + b ?

27 / 62

Page 33: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Motivations

Mythes et réalités sur la programmation fonctionnelle

Problème *

Écrire une fonction “affine” prenant deux entiers a et b, etrenvoyant la fonction x → ax + b ?

En C : Exercice difficile

Pas de valeur de type : “fonction int vers int”

27 / 62

Page 34: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Motivations

Mythes et réalités sur la programmation fonctionnelle

Problème *

Écrire une fonction “affine” prenant deux entiers a et b, etrenvoyant la fonction x → ax + b ?

En C : Exercice difficile

Pas de valeur de type : “fonction int vers int”

En Java : Exercice difficile

Idem, mais un peu plus facile car Java est flexible / structuré

27 / 62

Page 35: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Motivations

Mythes et réalités sur la programmation fonctionnelle

Problème *

Écrire une fonction “affine” prenant deux entiers a et b, etrenvoyant la fonction x → ax + b ?

En C : Exercice difficile

Pas de valeur de type : “fonction int vers int”

En Java : Exercice difficile

Idem, mais un peu plus facile car Java est flexible / structuré

En OCaml : Facile

let affine a b = (fun x− > a ∗ x + b)let f = affine 3 2; ;let x = f 2; ; (∗ x = 8 ∗)let g = affine 4 5; ;let y = g 1; ; (∗ y = 9∗)

27 / 62

Page 36: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Motivations

Mythes et réalités sur la programmation fonctionnelle

Applications de la programmation fonctionnelle

I Programmation système ou réseau (serveur web)I Domaine bancaire : analyse financièreI Domaine de prédilection = traitement symbolique :

I analyse de programmesI aide à la démonstrationI compilation⇒ outils de production en milieu industriel

I Calculs numériques avec structures de données complexesI Milieux scientifiques : mathématiques, physique, biologie . . .

28 / 62

Page 37: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Motivations

Mythes et réalités sur la programmation fonctionnelle

Dans la vraie vie

I MLDonkeyI LexiFiI EnsembleI Unison

I L’analyseur statique ASTREEI SLAM (Microsoft)I Coq

29 / 62

Page 38: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Motivations

Mythes et réalités sur la programmation fonctionnelle

Caractéristiques de Ocaml

I Typage statique avec inférence de typesI PolymorphismeI Types sommes et pattern matching (filtrage)I Gestions des exceptionsI Gestion de la mémoire (Garbage Collector)I Modules paramétrables (fonctor)I Compilateur natif et bytecodeI Système de classe évolué (objets)I Préprocesseur expressif et sûr (Camlp4)

30 / 62

Page 39: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Motivations

Mythes et réalités sur la programmation fonctionnelle

Historique

I 1973 ML Milner (tactique de preuves pour le prouveur LCF)I 1980 Projet Formel à l’INRIA (Gèrard Huet) Catergorical

Absrtract Machine (Pierre-Louis Curien)I 1984-1990 Définition de SML (Milner)I 1987 Caml (implémenté en Lisp) Guy =Cousineau, Ascander

Suarez (avec Pierre Weis et Michel Mauny)I 1990-1991 Caml Light par Xavier Leroy (et Damien Doligez

pour la gestion de la mémoire)I 1995 Caml Special LightI 1996 Ocaml (Xavier Leroy, Jêrome Vouillon, Didier Rémy,

Michel Mauny)

31 / 62

Page 40: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Notions de Base

Plan

Presentation du cours

MotivationsProgrammation impérativeProgrammation fonctionnelleMythes et réalités sur la programmation fonctionnelle

Notions de Base

Variables

Fonctions

Type Sommes

Filtrage

Conclusion

32 / 62

Page 41: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Notions de Base

Types de base

I int, constantes : 42 -12 opérateurs : + - * / modI float, constantes : -3.14 0. 1.3e-11 opérateurs : +. -. *. /.I char, constantes : ’a’ ’B’ char_of_int, int_of_charI string, constantes : “abc” opérateur de concaténation : ˆI bool, constantes : true false

opérateur de concatenation : &, &&, or, ||, notI α Type polymorpheI unit Type spécial avec un seul élement ()

33 / 62

Page 42: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Notions de Base

Expression conditionnelle

I “if b then e1 else e2”I évaluation en fonction de b

Exemple : (if 5 < 9 then 21 else 3) *2 ; ;

34 / 62

Page 43: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Notions de Base

Expression conditionnelle

I “if b then e1 else e2”I évaluation en fonction de b

Exemple : (if 5 < 9 then 21 else 3) *2 ; ;

I (if 5 < 9 then 21 else 3) *2 ; ;

34 / 62

Page 44: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Notions de Base

Expression conditionnelle

I “if b then e1 else e2”I évaluation en fonction de b

Exemple : (if 5 < 9 then 21 else 3) *2 ; ;

I (if 5 < 9 then 21 else 3) *2 ; ;I (if true then 21 else 3) *2

34 / 62

Page 45: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Notions de Base

Expression conditionnelle

I “if b then e1 else e2”I évaluation en fonction de b

Exemple : (if 5 < 9 then 21 else 3) *2 ; ;

I (if 5 < 9 then 21 else 3) *2 ; ;I (if true then 21 else 3) *2I 21*2

34 / 62

Page 46: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Notions de Base

Expression conditionnelle

I “if b then e1 else e2”I évaluation en fonction de b

Exemple : (if 5 < 9 then 21 else 3) *2 ; ;

I (if 5 < 9 then 21 else 3) *2 ; ;I (if true then 21 else 3) *2I 21*2I 42

34 / 62

Page 47: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Notions de Base

Expression conditionnelle

I “if b then e1 else e2”I évaluation en fonction de b

Exemple : (if 5 < 9 then 21 else 3) *2 ; ;

I (if 5 < 9 then 21 else 3) *2 ; ;I (if true then 21 else 3) *2I 21*2I 42

Remarque :e1 && e2 : e2 n’est pas évaluée si e vaut falsee1 || e2 : e2 n’est pas évaluée si e1 vaut vrai

34 / 62

Page 48: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Notions de Base

Listes

I Liste vide []I ajout en tête : :

Exemple

# ’O’ : :(’c’ : :(’a’ : :(’m’ : :(’l’ : :[])))) ; ;- : char list = [’O’ ;’c’ ;’a’ ;’m’ ;’l’]

comparaison : ordre lexicographique# [’a’ ;’b’] < [’a’ ;’a’] ; ;- : bool = false

35 / 62

Page 49: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Variables

Plan

Presentation du cours

MotivationsProgrammation impérativeProgrammation fonctionnelleMythes et réalités sur la programmation fonctionnelle

Notions de Base

Variables

Fonctions

Type Sommes

Filtrage

Conclusion

36 / 62

Page 50: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Variables

Précision : Deux notions de variable

Mathématiques A déf== 2 sin x + 3x Informatique

37 / 62

Page 51: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Variables

Précision : Deux notions de variable

Mathématiques A déf== 2 sin x + 3x Informatique

Variable en mathématiques

La valeur de A dépend de la valeur de x :

I la valeur de x est fixée (même si elle peut être inconnue)I si x varie, en fonction du temps t cela est explicité : x(t)

Ex. : si x(t) = 5t2 − 1 alors A(t) = 2 sin(5t2 − 1) + 15t2 − 3

37 / 62

Page 52: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Variables

Précision : Deux notions de variable

Mathématiques A déf== 2 sin x + 3x Informatique

Variable en mathématiques

La valeur de A dépend de la valeur de x :

I la valeur de x est fixée (même si elle peut être inconnue)I si x varie, en fonction du temps t cela est explicité : x(t)

Ex. : si x(t) = 5t2 − 1 alors A(t) = 2 sin(5t2 − 1) + 15t2 − 3

Variable en informatique

I le temps t reste implicite (cadencement par l’horloge)I t est discretI x(t) dépend des valeurs en mémoire à l’instant précédent :

x(t−1), y(t−1), etc.

En fait t est pratiquement inutilisable pour le raisonnement.

37 / 62

Page 53: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Variables

Variables *

Définition de variable : let

Variable = association nom/valeur

# let x = 53 ; ;val x : int = 53

# let y = x - 11 ; ;val y : int = ?

38 / 62

Page 54: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Variables

Variables *

Définition de variable : let

Variable = association nom/valeur

# let x = 53 ; ;val x : int = 53

# let y = x - 11 ; ;val y : int = 42

38 / 62

Page 55: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Variables

Variables *

Définition de variable : let

Variable = association nom/valeur

# let x = 53 ; ;val x : int = 53

# let y = x - 11 ; ;val y : int = 42

ExplicationsI let y = x - 11 ; ;I let y = 42 - 11 ; ;I let y = 42 ; ;I val y : int = 42

38 / 62

Page 56: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Variables

Redéfinition de variables *

Variable = nom donné à une valeur

Exemple

# let x = 53 ; ;val x : int = 53# let y = x - 11 ; ;val y : int = 42# let x = 2 ; ;val x : int = 2# y ; ;- : int = ?

39 / 62

Page 57: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Variables

Redéfinition de variables *

Variable = nom donné à une valeur

Exemple

# let x = 53 ; ;val x : int = 53# let y = x - 11 ; ;val y : int = 42# let x = 2 ; ;val x : int = 2# y ; ;- : int = ? -9 ou 42

39 / 62

Page 58: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Variables

Redéfinition de variables *

Variable = nom donné à une valeur

Exemple

# let x = 53 ; ;val x : int = 53# let y = x - 11 ; ;val y : int = 42# let x = 2 ; ;val x : int = 2# y ; ;- : int = ? -9 ou 42

39 / 62

Page 59: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Variables

Important *

Casse

# let eta = 0 ; ;val eta : int = 0# let eTa =1 ; ;val eTa : int = 1# eta = eTa ; ;

40 / 62

Page 60: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Variables

Important *

Casse

# let eta = 0 ; ;val eta : int = 0# let eTa =1 ; ;val eTa : int = 1# eta = eTa ; ;- : bool = false

Ocaml est case-sensitive

40 / 62

Page 61: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Fonctions

Plan

Presentation du cours

MotivationsProgrammation impérativeProgrammation fonctionnelleMythes et réalités sur la programmation fonctionnelle

Notions de Base

Variables

Fonctions

Type Sommes

Filtrage

Conclusion

41 / 62

Page 62: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Fonctions

Fonctions *

Notationmathématique : λx .f (x) Ocaml : fun x → f (x)

Exemple

# fun x -> x <> 0 ; ;- : int -> bool = <fun># (fun x -> x <> 0) 42 ; ;- : bool = true

# let estnonnul = (fun x -> x <> 0) ; ;val estnonnul : int -> bool = <fun># estnonnul 0 ; ;- : bool = false

42 / 62

Page 63: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Fonctions

Fonctions à plusieurs arguments

Point de vue au premier ordre (usuel en maths)

Fonction à n arguments = fonction à 1 argument : n-uplet

Exemples :

I ds =√

dx2 + dy2 + dz2 − 3.14dt2

ds : float× float× float× float → float# let ds (dx , dy , dz, dt) =

sqrt (dx ** 2. +. dy ** 2. +. dz ** 2. −. (3.14 *. dt) **2.) ; ;1 argument : quadruplet

43 / 62

Page 64: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Fonctions

Fonctions à plusieurs arguments “Curryfication”

Point de vue à l’ordre supérieur (préféré en prog. fonct.)

I Fonction à 0 argument = constanteI Fonction à n + 1 arguments =

fonction à 1 argument qui rend une fonction à n arguments

Exemplesplus 3 2 se lit (plus 3) 2plus : int → int → int qui se lit int → (int → int)

ds : (float → (float → (float → (float → float))))# let ds dx dy dz dt =

sqrt (dx ** 2. +. dy ** 2. +. dz ** 2. −. (c *. dt) ** 2.) ; ;

44 / 62

Page 65: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Type Sommes

Plan

Presentation du cours

MotivationsProgrammation impérativeProgrammation fonctionnelleMythes et réalités sur la programmation fonctionnelle

Notions de Base

Variables

Fonctions

Type Sommes

Filtrage

Conclusion

45 / 62

Page 66: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Type Sommes

Besoins contradictoires

Essence du typage

Les arguments fournis à une fonction doivent avoir un sens : ne pasmélanger entiers, chaînes, booléens, n-uplets, fonctions. . .

Mais on a souvent besoin de mélanger

I protocolesI lexèmes, structures grammaticales

Fausse solution : union (casse le typage)Solution compliquée : types avec discriminants

Bonne solution : union disjointe = somme

46 / 62

Page 67: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Type Sommes

Types somme *

Exemple

type ecb =Ent of int | Ch of string | Boofl of bool × float

Ent, Ch, Boofl = constructeurs du type

Valeurs : Ent (3), Ch (”azerty”), Boofl (true, 3.14)Étant donnée une valeur, l’examen du constructeur permet dedéterminer le type d’origine (ex. int, string, bool × float)

47 / 62

Page 68: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Type Sommes

Sommes : exemples dégénérés

Énumération # type couleur = Rouge | Vert | Bleu | Jaune

48 / 62

Page 69: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Type Sommes

Sommes : exemples dégénérés

Énumération # type couleur = Rouge | Vert | Bleu | Jaune# type boule = top | bottom ; ;Syntax error# type boule = Top| Bottom ; ;

48 / 62

Page 70: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Type Sommes

Sommes : exemples dégénérés

Énumération # type couleur = Rouge | Vert | Bleu | Jaune# type boule = top | bottom ; ;Syntax error# type boule = Top| Bottom ; ;Booléen type bool = true | falseUnité type unit = ()

48 / 62

Page 71: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Type Sommes

Application : Jeu de carte

Définir une type somme afin de manipuler un jeu de 32 cartes pourpouvoir jouer en Ocaml ?

49 / 62

Page 72: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Type Sommes

Application : Jeu de carte

Définir une type somme afin de manipuler un jeu de 32 cartes pourpouvoir jouer en Ocaml ?

Solution

type couleur = Pique | Coeur | Carreau | Trefle ; ;

49 / 62

Page 73: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Type Sommes

Application : Jeu de carte

Définir une type somme afin de manipuler un jeu de 32 cartes pourpouvoir jouer en Ocaml ?

Solution

type couleur = Pique | Coeur | Carreau | Trefle ; ;

type carte = As of couleur | Roi of couleur | Dame of couleur |Valet of couleur | Petite of int * couleur ; ;

49 / 62

Page 74: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Type Sommes

Application : Jeu de carte

Définir une type somme afin de manipuler un jeu de 32 cartes pourpouvoir jouer en Ocaml ?

Solution

type couleur = Pique | Coeur | Carreau | Trefle ; ;

type carte = As of couleur | Roi of couleur | Dame of couleur |Valet of couleur | Petite of int * couleur ; ;

let dixtrefle =

49 / 62

Page 75: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Type Sommes

Application : Jeu de carte

Définir une type somme afin de manipuler un jeu de 32 cartes pourpouvoir jouer en Ocaml ?

Solution

type couleur = Pique | Coeur | Carreau | Trefle ; ;

type carte = As of couleur | Roi of couleur | Dame of couleur |Valet of couleur | Petite of int * couleur ; ;

let dixtrefle = Petite (10, Trefle) ; ;

49 / 62

Page 76: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Type Sommes

Application : Jeu de carte

Définir une type somme afin de manipuler un jeu de 32 cartes pourpouvoir jouer en Ocaml ?

Solution

type couleur = Pique | Coeur | Carreau | Trefle ; ;

type carte = As of couleur | Roi of couleur | Dame of couleur |Valet of couleur | Petite of int * couleur ; ;

let dixtrefle = Petite (10, Trefle) ; ;let ascoeur =

49 / 62

Page 77: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Type Sommes

Application : Jeu de carte

Définir une type somme afin de manipuler un jeu de 32 cartes pourpouvoir jouer en Ocaml ?

Solution

type couleur = Pique | Coeur | Carreau | Trefle ; ;

type carte = As of couleur | Roi of couleur | Dame of couleur |Valet of couleur | Petite of int * couleur ; ;

let dixtrefle = Petite (10, Trefle) ; ;let ascoeur = As Coeur ; ;

49 / 62

Page 78: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Type Sommes

Application : Jeu de carte

Définir une type somme afin de manipuler un jeu de 32 cartes pourpouvoir jouer en Ocaml ?

Solution

type couleur = Pique | Coeur | Carreau | Trefle ; ;

type carte = As of couleur | Roi of couleur | Dame of couleur |Valet of couleur | Petite of int * couleur ; ;

let dixtrefle = Petite (10, Trefle) ; ;let ascoeur = As Coeur ; ;

! Impossible de définir un type entier entre [2..10]! cela rendrait le typage indécidable.

49 / 62

Page 79: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Type Sommes

Type somme récursif : Listes *

type liste_ent =| Nil| Cons of int * liste_ent

Exemple : [5 ; 2 ; 4 ]

50 / 62

Page 80: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Type Sommes

Type somme récursif : Listes *

type liste_ent =| Nil| Cons of int * liste_ent

Exemple : [5 ; 2 ; 4 ]

Cons (5, Cons (2, Cons (4, Nil)))

Cons

Cons

Cons

Nil4

2

5

50 / 62

Page 81: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Type Sommes

Type somme récursif : Listes *

type liste_ent =| Nil| Cons of int * liste_ent

Exemple : [5 ; 2 ; 4 ]

Cons (5, Cons (2, Cons (4, Nil)))

Cons

Cons

Cons

Nil4

2

5

���

���

���

[ ]4

2

5

50 / 62

Page 82: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Filtrage

Plan

Presentation du cours

MotivationsProgrammation impérativeProgrammation fonctionnelleMythes et réalités sur la programmation fonctionnelle

Notions de Base

Variables

Fonctions

Type Sommes

Filtrage

Conclusion

51 / 62

Page 83: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Filtrage

Analyse par cas *

type ecb = Ent of int | Ch of string | Boofl of bool × float

Filtrage

Soit v une valeur de type ecbmatch v with| Ent (n) →e1

| Ch (s) →e2

| Boofl (b, x) →e3

Les expressions ei à droite de « → » doivent avoir le même type

52 / 62

Page 84: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Filtrage

Filtrage = analyse par cas + décomposition

type ecb = Ent of int | Ch of string | Boofl of bool × float

Décompositionmatch V with| Ent (n) →. . . n . . .| Ch (s) →. . . s . . .| Boofl (b, x) →. . . x . . . b . . .

Les expressions de même type à droite de « → », sont toujoursbien définies :

I la première dans un environnement augmenté de n : intI la seconde dans un environnement augmenté de s : stringI la troisième dans un environnement

augmenté de b : bool et de x : float

53 / 62

Page 85: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Filtrage

Exemple : listes

type liste_ent =| Nil| Cons of int * liste_ent

match l with| Nil → true| Cons (x , l) → false

match l with| [ ] → true| x :: l → false

54 / 62

Page 86: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Filtrage

Exemple : booléens

match b with true → e1 | false → e2

est juste une autre syntaxe pour

if b then e1 else e2

55 / 62

Page 87: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Filtrage

Enregistrements = produits avec champs nommés *

type r = {c1 : T1 ;. . . cn : Tn}

En Ocaml, il faut nommer le type pour définir un enregistrement.

Exemple

# type produit = Poire|Banane ; ;# type marque = Opel|Nissan ; ;# type article = prod : produit ; marq : marque ; prix : int ; nb :int

Application :

# let car = prod = Poire ; marq = Opel ; prix = 42 ; nb =1 ; ;

Autres langages : record en ADA, struct en C.56 / 62

Page 88: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Filtrage

Enregistrements = produits avec champs nommés

Accès aux champs (projections) en notation pointée

# car.prod ; ;- : produit = Poire

Exemple

# type fa = { fct : int -> int ; arg : int }type fa = { fct : int -> int ; arg : int ; }# let moitie = fun x -> x/2 ; ;val moitie : int -> int = <fun>

# let div2 = { fct = moitie ; arg = 84 } ; ;let div2 = { fct = <fun> ; arg = 84 } ; ;# div2.fct div2.arg ; ;- : int = 42

57 / 62

Page 89: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Conclusion

Plan

Presentation du cours

MotivationsProgrammation impérativeProgrammation fonctionnelleMythes et réalités sur la programmation fonctionnelle

Notions de Base

Variables

Fonctions

Type Sommes

Filtrage

Conclusion

58 / 62

Page 90: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Conclusion

Aujourd’hui

I Programmation fonctionnelleI Types et opérateurs de base (int, char ...)I Variables (let)I Fonction (fun)I Types somme (type = ... of ...)I Filtrage (match)

59 / 62

Page 91: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Conclusion

Prochaine fois

I RécurrenceI FiltrageI PreuvesI Fonctions d’ordre supérieur

60 / 62

Page 92: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Conclusion

Merci de votre attention.

Questions ?

61 / 62

Page 93: Langage de Programmation 2 (LP2) - [Verimag]plafourc/teaching/LP2_RICM3... · Pascal Lafourcade Polytech 2010 - 2011 1 / 62. Langage de Programmation 2 (LP2) DØmo, DOOM 2 / 62. Langage

Langage de Programmation 2 (LP2)

Conclusion

Bibliographie

I Xavier Leroy et al. Objective Caml manualhttp://caml.inria.fr/pub/docs/manual-ocaml/index.html

I The book Developing Applications With Objective Caml (byEmmanuel Chailloux, Pascal Manoury and Bruno Pagano)

I Approche fonctionnelle de la programmation, Michel Mauny ,Guy Cousineau

I Pierre Weis and Xavier Leroy. Le langage Caml. Dunod, 1999.I Apprentissage de la programmation avec OCaml Catherine

Dubois & Valérie Ménissier-MorainI Philippe Nardel. Programmation fonctionnelle, générique et

objet : Une introduction avec le langage OCaml. Vuibert, 2005I Louis Gacogne. Programmation par l’exemple en Caml.

Ellipse, 2004

62 / 62