Cours de Haskell en pdf

Problème à signaler:

Télécharger



★★★★★★★★★★3.5 étoiles sur 5 basé sur 1 votes.
Votez ce document:

Haskell — Paresse, ordre strict et typage fort

IFT359 — cinquième thème

Benoît Fraikin

Département d’informatique

UNIVERSITÉ DE SHERBROOKE

28 juin 2012

UNIVERSITÉ DE SHERBROOKE

1 / 42

Plan de la séance

Présentation d’Haskell Historique

Caractéristiques

Introduction

La base

Les types

Les fonctions

Les classes de types

Les itérateurs

Structure de données

Conclusion

Haskell vs Racket

         Finalement                                                                        UNIVERSITÉ DE

SHERBROOKE

2 / 42


Chronologie

 

Quelques acteurs principaux

                                    Présentation d’Haskell              Historique

de 1987 à 1998 (Paul Hudak et al., 2007) Hudak et Peyton Jones en 87

inspiré par Miranda (propriétaire) Haskell v1.0 en 1990 (v1.1 en 91 et v1.2 en 92)

Haskell v1.3 en 1996 (v1.4 en 97)

Haskell 98 (99 et 02)

Haskell HP 2011

UNIVERSITÉ DE SHERBROOKE

4 / 42

                             Présentation d’Haskell              Historique

Paul Hudak (Yale U.)

Simon Peyton Jones (Microsoft Research, Glasgow U.)

Richard Bird (Oxford U.)

Simon Marlow (Microsoft Research)

Philip Wadler (Edinburgh U.)

Graham Hutton (Notthingham U.)

Eric Meijer (Microsoft Research)

John Hugues (Chalmers U.)

Ralph Lämmer (U. Koblenz-Landau, ex. Microsoft Research) Don Stewart (Galois Inc., Standard Chartered Bank)

UNIVERSITÉ DE SHERBROOKE

5 / 42


 

7 / 42                                                                                                                                                                                                                                                                                                                                                                                   8 / 42


                                    Présentation d’Haskell              Caractéristiques                                                                                                                                                                                                                                                                                    Présentation d’Haskell              Caractéristiques

Liste

 

Les opérateurs infixes

                                    Présentation d’Haskell              Caractéristiques                                                                                                                                                                                                                                                                                    Présentation d’Haskell              Caractéristiques

La curryfication

 

Le polymorphisme ad-hoc

 

UNIVERSITÉ DE         UNIVERSITÉ DE SHERBROOKE         SHERBROOKE

                                                                                                               9 / 42                                                                                                                                                                                                                                                                                                                                                          10 / 42

                                    Présentation d’Haskell              Caractéristiques                                                                                                                                                                                                                                                                                    Présentation d’Haskell              Caractéristiques

La pureté

 

La paresse

Pas de forme impérative                                                                                             La paresse est

        pas d’affectation                                                                                                        une implémentation de l’évaluation en ordre strict (normal)

           pas de boucle itérative (for, while)                                                                             évite le problème de multiplication des calculs (boxed value)

          « forme » plus mathématique =? récursivité                                                              augmente la mémoire utilisée

        nécessite un mécanisme pour les entrées/sorties                                                          est un choix pour obtenir la pureté d’un point de vue pratique

apparition des monades comme solution                                                                    impose de repenser les itérateurs (foldl et foldr) simplifie les choix cependant

UNIVERSITÉ DE UNIVERSITÉ DE SHERBROOKESHERBROOKE

                                                                                                              11 / 42                                                                                                                                                                                                                                                                                                                                                          12 / 42

                                    Présentation d’Haskell              Caractéristiques                                                                                                                                                                                                                                                                                                             Introduction             La base

Le typage statique fort

 

Développement

Une notion fondamentale                                                                                           Outils

        nécessaire pour comprendre comment développer                                                  1          le compilateur : GHC

        fiabilité accrue                                                                                                                  YHC

        efficacité en mémoire accrue à l’exécution                                                           l’interpréteur : GHCihttp:///

2 impose de penser et comprendre les types avant tout           Hugs

pas d’indication explicite (la plupart du temps)     http:/// gestion efficace par inférence de type           3                                             la plateforme — piles inclus : Haskell Platform

                                                                                                              13 / 42                                                                                                                                                                                                                                                                                                                                                          15 / 42

                                                           Les types                                                                                                                                                                                                                                                                                                                                                                          Les types

Comprendre les types pour compiler

 

Types et constructeurs de bases

 

UNIVERSITÉ DE SHERBROOKE

17 / 42

UNIVERSITÉ DE SHERBROOKE

18 / 42

                                             Introduction             Les types                                                                                                                                                                                                                                                                                                                               Introduction             Les types

Le type d’une fonction

 

La généricité

 

                                             Introduction             Les types                                                                                                                                                                                                                                                                                                                               Introduction               Les fonctions

La généricité

 

Écriture d’une fonction

 

                                                                                                              21 / 42                                                                                                                                                                                                                                                                                                                                                          23 / 42


                                                           Les fonctions                                                                                                                                                                                                                                                                                                                                                                Les classes de types

Le pattern matching

 

Limite de la généricité

 

UNIVERSITÉ DE         UNIVERSITÉ DE SHERBROOKE         SHERBROOKE

                                                                                                              24 / 42                                                                                                                                                                                                                                                                                                                                                          26 / 42

                                             Introduction              Les classes de types                                                                                                                                                                                                                                                                                                    Introduction              Les classes de types

Quelques classes de types

 

Organigramme des types classes courants

Les classiques

Eq définit les types comparables

Ord définit les types ordonnés 3 Show définit les types affichables

Read définit les types lisibles

Num définit les nombres

Fractionnal définit les nombres divisibles

UNIVERSITÉ DE UNIVERSITÉ DE  SHERBROOKE                SHERBROOKE

                                                                                                              27 / 42                                                                                                                                                                                                                                                                                                                                                          28 / 42

                                             Introduction              Les itérateurs                                                                                                                                                                                                                                                                                                                   Introduction              Les itérateurs

Les opérateurs basiques

 

Comparaison foldr vs. foldl

 

                                                                                                              30 / 42                                                                                                                                                                                                                                                                                                                                                          31 / 42

                                                           Les itérateurs                                                                                                                                                                                                                                                                                                                                                               Les itérateurs

Comparaison foldr vs. foldl

 

La version strict foldl

 

UNIVERSITÉ DE SHERBROOKE

32 / 42

UNIVERSITÉ DE SHERBROOKE

33 / 42

                                             Introduction              Structure de données                                                                                                                                                                                                                                                                                                Introduction               Structure de données

Structures existantes

 

Nouveaux types

 

                                              Conclusion             Haskell vs Racket                                                                                                                                                                                                                                                                                                              Conclusion             Finalement

Comparaison

 

Les entrées/sorties ?

 

                                                                                                              38 / 42                                                                                                                                                                                                                                                                                                                                                          40 / 42


                                              Conclusion             Finalement

Exemple type Histogramme = [(Char,String)]

afficheH :: Histogramme ? IO () afficheH h = mapM_ print_ligne h

where print_ligne (l,f) = putStrLn $ l : ’ ’ : f

main = do texte ? getLine

Monade IO a

 

Conclusions

UNIVERSITÉ DE SHERBROOKE

41 / 42

UNIVERSITÉ DE SHERBROOKE

42 / 42

afficheH (calcul_histogramme texte) return -- n’est pas obligatoire

                                              Conclusion             Finalement

Haskell est un langage fonctionnel et efficace à la syntaxe concise (trop?) encourageant un style déclaratif typée fort, pur et paresseux avec du polymorphisme paramétrique et ad-hoc disposant de monade !


39