Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.

GADTs Einführung

616 visualizaciones

Publicado el

Mein im Rahmen der Haskell User Group Hamburg gehaltener Vortrag.
Die Quellen sind ebenfalls öffentlich zugänglich:
https://github.com/smu4242/seminare

  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (2019 Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THIS can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THIS is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THIS Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THIS the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THIS Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Responder 
    ¿Estás seguro?    No
    Tu mensaje aparecerá aquí
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (2019 Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THIS can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THIS is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THIS Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THIS the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THIS Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Responder 
    ¿Estás seguro?    No
    Tu mensaje aparecerá aquí
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (Unlimited) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/qURD } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/qURD } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/qURD } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/qURD } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/qURD } ......................................................................................................................... Download doc Ebook here { https://soo.gd/qURD } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THIS can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THIS is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THIS Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THIS the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THIS Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Responder 
    ¿Estás seguro?    No
    Tu mensaje aparecerá aquí
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (2019 Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THIS can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THIS is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THIS Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THIS the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THIS Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Responder 
    ¿Estás seguro?    No
    Tu mensaje aparecerá aquí
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (2019 Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THIS can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THIS is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THIS Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THIS the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THIS Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Responder 
    ¿Estás seguro?    No
    Tu mensaje aparecerá aquí
  • Sé el primero en recomendar esto

GADTs Einführung

  1. 1. Einf¨hrung u Großraum GADTs Beispiele f¨r GADTs u Schlußbetrachtungen GADTs Generalisierte Algebraische DatentypenSven Urbanski (svenurbanski@gmail.com)Haskell User Group Hamburg 19.04.2012 1
  2. 2. Einf¨hrung u Großraum GADTs Beispiele f¨r GADTs u Schlußbetrachtungen1 Einf¨hrung u Motivation ADTs2 Großraum GADTs Phantomtypen Typeinschr¨nkungen a Erweiterung der R¨ckgabetypen u3 Beispiele f¨r GADTs u Sichere Listen Gemischte Listen Abstrakte Syntaxb¨ume a Vektoren4 Schlußbetrachtungen Fazit Verwandtes: Associated Types Verwandtes: Dependent Types 2
  3. 3. Einf¨hrung u Großraum GADTs Motivation Beispiele f¨r GADTs u ADTs SchlußbetrachtungenMotivation Generalisierte Algebraische Datentypen (GADT, [g2t]) bieten: ⇒ Formulierung von Invarianten im Typsystem ⇒ Zus¨tzliche Typchecks a ⇒ weniger Laufzeitfehler 3
  4. 4. Einf¨hrung u Großraum GADTs Motivation Beispiele f¨r GADTs u ADTs SchlußbetrachtungenADT ⇒ Algebraische Datentypen ([a:de:te:] auch Summen-Datentypen): Beispiel: 1 data X = X1 A | X2 B | ... ⇒ X ist der ADT X1, X2, ... sind Konstruktorfunktionen X ≈ die Menge aller Werte aus A, B, . . . X ≈ {a1, a2, a3, . . . , b1, b2, b3, . . .} |X | = |A| + |B| + . . . 4
  5. 5. Einf¨hrung u Großraum GADTs Motivation Beispiele f¨r GADTs u ADTs SchlußbetrachtungenAnwendungsf¨lle a ⇒ beliebig viele ⇒ uberall, wo im Datentyp verschiedene F¨lle unterschieden ¨ a werden Beispiel: 1 data Expr = IExpr Int 2 | BExpr Bool 3 | AndExpr Expr Expr 5
  6. 6. Einf¨hrung u Phantomtypen Großraum GADTs Typeinschr¨nkungen a Beispiele f¨r GADTs u Erweiterung der R¨ckgabetypen u SchlußbetrachtungenPhantomtypen Normale Typparameter: 1 data List a = Nil 2 | Cons a (List a) Typparameter, die scheinbar nicht genutzt werden: 1 data PList a b = PNil 2 | PCons a (PList a b) 6
  7. 7. Einf¨hrung u Phantomtypen Großraum GADTs Typeinschr¨nkungen a Beispiele f¨r GADTs u Erweiterung der R¨ckgabetypen u SchlußbetrachtungenPhantomtypen Zur Erinnerung: 1 data PList a b = PNil 2 | PCons a (PList a b) Vorteil: Listen mit unterschiedlicher Semantik k¨nnen nicht mehr o gemischt werden Beispiel: 1 data Euro = ... 2 data USD = ... 3 euroToUsd :: PList a Euro -> PList a USD 7
  8. 8. Einf¨hrung u Phantomtypen Großraum GADTs Typeinschr¨nkungen a Beispiele f¨r GADTs u Erweiterung der R¨ckgabetypen u SchlußbetrachtungenEinschr¨nkungen der Typparameter a In Java: 1 interface NumList<A> 2 class NCons<A extends Number> implements NumList<A> 3 class NNil ... In Haskell: 1 data NumList a = NNil 2 | Num a => NCons a (NumList a) 8
  9. 9. Einf¨hrung u Phantomtypen Großraum GADTs Typeinschr¨nkungen a Beispiele f¨r GADTs u Erweiterung der R¨ckgabetypen u SchlußbetrachtungenSyntax GADTs Alte Syntax (ADTs): 1 data List a = Nil 2 | Cons a (List a) Neue Syntax: 1 data List a where 2 Nil :: List a 3 Cons :: a -> (List a) -> List a Nil ist eine Funktion ohne Parameter. Cons ist eine Funktion mit zwei Parametern. Gemeinsamkeiten? 9
  10. 10. Einf¨hrung u Phantomtypen Großraum GADTs Typeinschr¨nkungen a Beispiele f¨r GADTs u Erweiterung der R¨ckgabetypen u SchlußbetrachtungenErweiterung durch GADTs Willk¨rliche Beispiele: u 1 Cons1 :: a -> (List a) -> List a 2 Cons2 :: a -> (List a) -> List (a,a) 3 Cons3 :: a -> (List a) -> List (List a) 4 Cons4 :: a -> (List a) -> a -- err ⇒ Welchen Vorteil bringt das? ⇒ Mehr Typchecks ⇒ Aber wie sinnvoll einsetzen? 10
  11. 11. Einf¨hrung u Sichere Listen Großraum GADTs Gemischte Listen Beispiele f¨r GADTs u Abstrakte Syntaxb¨ume a Schlußbetrachtungen VektorenSichere Listen ⇒ Was bedeutet “sicher”? ⇒ Typ-Unterscheidung zur Compilezeit: Leere / Nicht leere Listen (genauer: L¨nge der Liste) a ⇒ Sinnvoll f¨r partiell definierte Funktionen wie head und tail u Beispiel: 1 data Safe a = Safe a 2 data NotSafe = NotSafe 3 data SafeList a b where 4 Nil :: SafeList a NotSafe 5 Cons :: a -> SafeList a b -> SafeList a (Safe b) 11
  12. 12. Einf¨hrung u Sichere Listen Großraum GADTs Gemischte Listen Beispiele f¨r GADTs u Abstrakte Syntaxb¨ume a Schlußbetrachtungen VektorenTyp von sicheren Listen Zur Erinnerung: 1 data SafeList a b where 2 Nil :: SafeList a NotSafe 3 Cons :: a -> SafeList a b -> SafeList a (Safe b) Bedeutung der Typparameter: 1 SafeList a NotSafe -- leere Liste 2 SafeList a (Safe NotSafe) -- Laenge == 1 3 SafeList a (Safe b) -- Laenge >= 1 4 SafeList a (Safe (Safe NotSafe)) -- Laenge == 2 5 SafeList a (Safe (Safe b)) -- Laenge >= 2 6 SafeList a b -- Laenge beliebig 7 ... -- Laenge n ??? 8 12
  13. 13. Einf¨hrung u Sichere Listen Großraum GADTs Gemischte Listen Beispiele f¨r GADTs u Abstrakte Syntaxb¨ume a Schlußbetrachtungen VektorenFunktionen mit sicheren Listen: safeHead Zur Erinnerung: 1 data SafeList a b where 2 Nil :: SafeList a NotSafe 3 Cons :: a -> SafeList a b -> SafeList a (Safe b) Sicher: 1 safeHead :: SafeList a (Safe b) -> a 2 safeHead (Cons x _) = x 3 -- safeHead Nil = ??? 13
  14. 14. Einf¨hrung u Sichere Listen Großraum GADTs Gemischte Listen Beispiele f¨r GADTs u Abstrakte Syntaxb¨ume a Schlußbetrachtungen VektorenFunktionen mit sicheren Listen: unsafeHead Zur Erinnerung: 1 data SafeList a b where 2 Nil :: SafeList a NotSafe 3 Cons :: a -> SafeList a b -> SafeList a (Safe b) Unsicher: 1 unsafeHead :: SafeList a b -> a 2 unsafeHead (Cons x _) = x 3 unsafeHead Nil = error "error in head!" 14
  15. 15. Einf¨hrung u Sichere Listen Großraum GADTs Gemischte Listen Beispiele f¨r GADTs u Abstrakte Syntaxb¨ume a Schlußbetrachtungen VektorenFunktionen mit sicheren Listen: tail Zur Erinnerung: 1 data SafeList a b where 2 Nil :: SafeList a NotSafe 3 Cons :: a -> SafeList a b -> SafeList a (Safe b) Beispiel: 1 safeTail :: SafeList a (Safe b) -> SafeList a b 2 safeTail (Cons _ t) = t 15
  16. 16. Einf¨hrung u Sichere Listen Großraum GADTs Gemischte Listen Beispiele f¨r GADTs u Abstrakte Syntaxb¨ume a Schlußbetrachtungen VektorenFunktionen mit sicheren Listen: access Zur Erinnerung: 1 data SafeList a b where 2 Nil :: SafeList a NotSafe 3 Cons :: a -> SafeList a b -> SafeList a (Safe b) Sicherer Zugriff? 1 accessN :: Int 2 -> SafeList a (Safe (Safe (Safe ...))) 3 -> a 16
  17. 17. Einf¨hrung u Sichere Listen Großraum GADTs Gemischte Listen Beispiele f¨r GADTs u Abstrakte Syntaxb¨ume a Schlußbetrachtungen VektorenFunktionen mit sicheren Listen: map/fold ⇒ Was ist mit total definierten Funktionen wie map oder fold? ⇒ Kein Problem! map: 1 safeMap :: SafeList a c -> (a -> b) -> SafeList b c 2 safeMap (Cons e t) f = Cons (f e) (safeMap t f) 3 safeMap (Nil) _ = Nil fold: 1 fold :: (a -> b -> a) -> a -> SafeList b c -> a 2 fold f start (Nil) = start 3 fold f start (Cons e t) = fold f (f start e) t 17
  18. 18. Einf¨hrung u Sichere Listen Großraum GADTs Gemischte Listen Beispiele f¨r GADTs u Abstrakte Syntaxb¨ume a Schlußbetrachtungen VektorenFunktionen mit sicheren Listen: zip Zur Erinnerung: 1 data SafeList a b where 2 Nil :: SafeList a NotSafe 3 Cons :: a -> SafeList a b -> SafeList a (Safe b) zip: 1 safeZip :: SafeList a len -> SafeList b len 2 -> SafeList (a,b) len 3 safeZip Nil Nil = Nil 4 safeZip (Cons e1 l1) (Cons e2 l2) = 5 Cons (e1,e2) (safeZip l1 l2) 18
  19. 19. Einf¨hrung u Sichere Listen Großraum GADTs Gemischte Listen Beispiele f¨r GADTs u Abstrakte Syntaxb¨ume a Schlußbetrachtungen VektorenFunktionen mit sicheren Listen: Konvertierung 1 Zur Erinnerung: 1 data SafeList a b where 2 Nil :: SafeList a NotSafe 3 Cons :: a -> SafeList a b -> SafeList a (Safe b) Unsichere Liste → Sichere Liste 1 makeSafe1 :: SafeList a b 2 -> Maybe (SafeList a (Safe b)) 3 makeSafe1 Nil = Nothing 4 makeSafe1 (Cons e l) = Just (Cons e l) --- ??? 19
  20. 20. Einf¨hrung u Sichere Listen Großraum GADTs Gemischte Listen Beispiele f¨r GADTs u Abstrakte Syntaxb¨ume a Schlußbetrachtungen VektorenFunktionen mit sicheren Listen: Konvertierung 2 Zur Erinnerung: 1 data SafeList a b where 2 Nil :: SafeList a NotSafe 3 Cons :: a -> SafeList a b -> SafeList a (Safe b) Sichere Liste → Unsichere Liste 1 makeUnsafe :: SafeList a (Safe b) 2 -> SafeList a b 3 makeUnsafe (Cons e l) = Cons e l -- ??? Sinnvoll? 20
  21. 21. Einf¨hrung u Sichere Listen Großraum GADTs Gemischte Listen Beispiele f¨r GADTs u Abstrakte Syntaxb¨ume a Schlußbetrachtungen VektorenGemischte Listen Idee: Verschiedene Typen in einer Liste Beispiel: 1 data MixList a b where 2 MNil :: MixList a () 3 MCons :: (Show c, Show b) => 4 a -> MixList b c -> MixList a (MixList b c) ⇒ a ist der Typ des ersten Elements ⇒ b ist der Typ der Restliste 21
  22. 22. Einf¨hrung u Sichere Listen Großraum GADTs Gemischte Listen Beispiele f¨r GADTs u Abstrakte Syntaxb¨ume a Schlußbetrachtungen VektorenGemischte Listen Zur Erinnerung: 1 data MixList a b where 2 MNil :: MixList a () 3 MCons :: (Show c, Show b) => 4 a -> MixList b c -> MixList a (MixList b c) Beispiel: 1 > MCons 42 (MCons "hallo" MNil) 2 42 : "hallo" : [] 3 > :t MCons 42 (MCons "hallo" MNil) 4 MixList t (MixList [Char] (MixList b ())) 22
  23. 23. Einf¨hrung u Sichere Listen Großraum GADTs Gemischte Listen Beispiele f¨r GADTs u Abstrakte Syntaxb¨ume a Schlußbetrachtungen VektorenUnsichere Ausdrucksb¨ume (ADT) a ADT: 1 data Expr = IntLiteral Int 2 | BoolLiteral Bool 3 | AndExpr Expr Expr 4 inv :: Expr -> Bool 5 inv (BoolLiteral _) = True 6 inv (IntLiteral _) = True 7 inv (AndExpr l r) = isInt l && isInt r 8 ‘or‘ isBool l && isBool r 9 -- isBool, isInt ... ⇒ Umst¨ndliche Definition der Invariante (bei vielen a Konstruktorfunktionen) ⇒ Kann mit ADT nur zur Laufzeit uberpr¨ft werden ¨ u ⇒ Ziel: Invariante durch Compiler uberpr¨fen lassen ¨ u 23
  24. 24. Einf¨hrung u Sichere Listen Großraum GADTs Gemischte Listen Beispiele f¨r GADTs u Abstrakte Syntaxb¨ume a Schlußbetrachtungen VektorenSichere Ausdrucksb¨ume (GADT) a GADT: 1 data Expr a where 2 I :: Int -> Expr Int 3 B :: Bool -> Expr Bool 4 And :: Expr Bool -> Expr Bool -> Expr Bool ⇒ Invariante? ⇒ Langweilig: Ausdrucksb¨ume bestehen nur aus Ints oder nur a aus Bools ⇒ Erweiterung: Vergleich und Addition 24
  25. 25. Einf¨hrung u Sichere Listen Großraum GADTs Gemischte Listen Beispiele f¨r GADTs u Abstrakte Syntaxb¨ume a Schlußbetrachtungen VektorenSichere Ausdrucksb¨ume (GADT) a Erweiterung: 1 data Expr a where 2 I :: Int -> Expr Int 3 B :: Bool -> Expr Bool 4 And :: Expr Bool -> Expr Bool -> Expr Bool 5 Equal :: (Show a, Eq a) => 6 Expr a -> Expr a -> Expr Bool 7 Add :: Expr Int -> Expr Int -> Expr Int ⇒ Mischung von Ints und Bools und Invariante erf¨llt u 25
  26. 26. Einf¨hrung u Sichere Listen Großraum GADTs Gemischte Listen Beispiele f¨r GADTs u Abstrakte Syntaxb¨ume a Schlußbetrachtungen VektorenSichere Ausdrucksb¨ume: eval a Zur Erinnerung: 1 data Expr a where 2 I :: Int -> Expr Int 3 B :: Bool -> Expr Bool 4 And :: Expr Bool -> Expr Bool -> Expr Bool 5 Equal :: Eq a => Expr a -> Expr a -> Expr Bool 6 Add :: Expr Int -> Expr Int -> Expr Int Auswertung: 1 eval :: Expr a -> a 2 eval (I i) = i 3 eval (B b) = b 4 eval (And l r) = eval l && eval r 5 eval (Equal l r) = eval l == eval r 6 eval (Add l r) = eval l + eval r 26
  27. 27. Einf¨hrung u Sichere Listen Großraum GADTs Gemischte Listen Beispiele f¨r GADTs u Abstrakte Syntaxb¨ume a Schlußbetrachtungen VektorenSichere Ausdrucksb¨ume: optimize a Zur Erinnerung: 1 data Expr a where 2 I :: Int -> Expr Int 3 B :: Bool -> Expr Bool 4 And :: Expr Bool -> Expr Bool -> Expr Bool 5 Equal :: Eq a => Expr a -> Expr a -> Expr Bool 6 Add :: Expr Int -> Expr Int -> Expr Int optimize: 1 optimize :: Expr a -> Expr a 2 optimize (And (B True) r) = optimize r 3 optimize (And l r) = And (optimize l) (optimize r) 4 optimize x = x 27
  28. 28. Einf¨hrung u Sichere Listen Großraum GADTs Gemischte Listen Beispiele f¨r GADTs u Abstrakte Syntaxb¨ume a Schlußbetrachtungen VektorenVektoren Beispiel: 1 data Zero 2 data Next a 3 data Vec e n where 4 NullVec :: Vec e Zero 5 NextVec :: e -> Vec e n -> Vec e (Next n) Konvertierung: 1 v2l :: Vec e n -> [e] 2 v2l NullVec = [] 3 v2l (NextVec e l) = e : v2l l 4 5 l2v :: [e] -> Vec e n 6 l2v [] = NullVec 7 l2v (e:l) = NextVec e (l2v l) -- ??? 28
  29. 29. Einf¨hrung u Sichere Listen Großraum GADTs Gemischte Listen Beispiele f¨r GADTs u Abstrakte Syntaxb¨ume a Schlußbetrachtungen VektorenVektorfunktionen: Skalarprodukt Zur Erinnerung: 1 data Zero 2 data Next a 3 data Vec e n where 4 NullVec :: Vec e Zero 5 NextVec :: e -> Vec e n -> Vec e (Next n) Skalarprodukt: 1 dot :: Vec Int n -> Vec Int n -> Int 2 dot NullVec NullVec = 0 3 dot (NextVec e1 l1) (NextVec e2 l2) = e1 * e2 + dot l1 l2 29
  30. 30. Einf¨hrung u Sichere Listen Großraum GADTs Gemischte Listen Beispiele f¨r GADTs u Abstrakte Syntaxb¨ume a Schlußbetrachtungen VektorenVektorfunktionen: Konkatenation Zur Erinnerung: 1 data Zero 2 data Next a 3 data Vec e n where 4 NullVec :: Vec e Zero 5 NextVec :: e -> Vec e n -> Vec e (Next n) Konkatenation: 1 concatV :: Vec e n1 -> Vec e n2 -> Vec e n3 -- ??? 2 concatV NullVec x = x 3 concatV (NextVec e1 l1) x = NextVec e1 (concatV l1 x) 30
  31. 31. Einf¨hrung u Sichere Listen Großraum GADTs Gemischte Listen Beispiele f¨r GADTs u Abstrakte Syntaxb¨ume a Schlußbetrachtungen VektorenVektoren – zweiter Versuch: Beispiel: 1 data Wec e n where 2 Wec :: [e] -> Wec e n Konvertierung: 1 w2l :: Wec e n -> [e] 2 w2l (Wec l) = l 3 4 l2w :: [e] -> Wec e n 5 l2w l = Wec l 31
  32. 32. Einf¨hrung u Sichere Listen Großraum GADTs Gemischte Listen Beispiele f¨r GADTs u Abstrakte Syntaxb¨ume a Schlußbetrachtungen VektorenVektoren – zweiter Versuch: Invariante Zur Erinnerung: 1 data Wec e n where 2 Wec :: [e] -> Wec e n Eigene Konstruktorfunktion: 1 w3 :: [e] -> Wec e (Next (Next (Next Zero))) 2 w3 e = assert (length e == 3) (Wec e) 32
  33. 33. Einf¨hrung u Sichere Listen Großraum GADTs Gemischte Listen Beispiele f¨r GADTs u Abstrakte Syntaxb¨ume a Schlußbetrachtungen VektorenVektoren – dritter Versuch: Beispiel: 1 data WecN e n where 2 Wec1 :: e -> WecN e (Next Zero) 3 Wec2 :: e -> e -> WecN e (Next (Next Zero)) 4 Wec3 :: e -> e -> e -> WecN e (Next (Next (Next Zero))) 5 -- ... ≈ Praktikabel? 33
  34. 34. Einf¨hrung u Fazit Großraum GADTs Verwandtes: Associated Types Beispiele f¨r GADTs u Verwandtes: Dependent Types SchlußbetrachtungenFazit + Mehr Typsicherheit + Weniger Laufzeitfehler + Geschicktere Invarianten + Partielle Funktion ⇒ totale Funktion ≈ . . . aber nicht alle ≈ Nur bei bestimmten Datenstrukturen sinnvoll einsetzbar 34
  35. 35. Einf¨hrung u Fazit Großraum GADTs Verwandtes: Associated Types Beispiele f¨r GADTs u Verwandtes: Dependent Types SchlußbetrachtungenAssociated Types (1) ⇒ Funktionen uber Typen ¨ ⇒ Erweiterung von TypklassenHerk¨mmliche Num-Typklasse: o Neue Num-Typklasse: 1 class Num a where 1 class MyNum a b where 2 (+++) :: a -> a -> a 2 type Sum a b :: * 3 3 (+++) :: a -> b -> Sum a b 4 4 5 instance Num Int where 5 instance MyNum Int Int where 6 (+++) x y = x + y 6 type Sum Int Int = Int 7 7 (+++) x y = x + y ⇒ Sum ist eine Funktion mit 2 Typen als Parameter und einem Typ als R¨ckgabewert u 35
  36. 36. Einf¨hrung u Fazit Großraum GADTs Verwandtes: Associated Types Beispiele f¨r GADTs u Verwandtes: Dependent Types SchlußbetrachtungenAssociated Types (2) Zur Erinnerung: 1 class MyNum a b where 2 type Sum a b :: * 3 (+++) :: a -> b -> Sum a b MyNum Float Int: 1 instance Integral a => MyNum Float a where 2 type Sum Float a = Float 3 (+++) x y = x + intToFloat y ⇒ Floats und Ints addieren! ≈ Ints und Floats? 36
  37. 37. Einf¨hrung u Fazit Großraum GADTs Verwandtes: Associated Types Beispiele f¨r GADTs u Verwandtes: Dependent Types SchlußbetrachtungenAssociated Types (3) Zur Erinnerung: 1 class MyNum a b where 2 type Sum a b :: * 3 (+++) :: a -> b -> Sum a b MyNum Int Float: 1 instance Integral a => MyNum a Float where 2 type Sum a Float = Float 3 (+++) x y = y +++ x ⇒ nun ist +++ kommutativ! 37
  38. 38. Einf¨hrung u Fazit Großraum GADTs Verwandtes: Associated Types Beispiele f¨r GADTs u Verwandtes: Dependent Types SchlußbetrachtungenDependent Types (1) ⇒ Typen, die von Werten abh¨ngen a ⇒ Agda 38
  39. 39. Einf¨hrung u Fazit Großraum GADTs Verwandtes: Associated Types Beispiele f¨r GADTs u Verwandtes: Dependent Types SchlußbetrachtungenDependent Types (2) Willk¨rliches Beispiel: u q : ΣN (λx → if x = 1 then N else Bool) Werte f¨r q: u q = 4, true q = 1, 42 39
  40. 40. Einf¨hrung u Fazit Großraum GADTs Verwandtes: Associated Types Beispiele f¨r GADTs u Verwandtes: Dependent Types SchlußbetrachtungenDependent Types (3) ⇒ noch genauere Typ¨berp¨fungen / Invarianten u u z.B.: Vektoren (auch mit GADTs) Matrizen (m × n → n × o → m × o) Bin¨re B¨ume (links < mitte < rechts) a a ≈ Vermischung Typ/Wert ≈ Endlosschleifen im Typsystem ≈ Typgleichheit 40

×