Literatur
|
LYAH:
Tuples
|
| |
Paare
|
kartesisches Produkt: A×B
|
| |
Definition
|
data Pair a b = MkPair a b
|
| |
|
polymorpher Datentyp
Datentyp mit Typparameter
|
| |
|
Pair besitzt genau einen Konstruktor
|
| |
|
Der Konstruktor ist eine Funktion mit 2 Parametern |
|
MkPair :: a -> b -> Pair a b
|
| |
Konvention
|
Konstruktoren beginnen immer mit einem Großbuchstaben
|
|
Konstruktoren dürfen in Mustern auftreten
|
| |
Notation
|
vertrauter: Paar-Schreibweise (x, y)
|
|
|
| |
elementare Operationen
|
fst :: (a, b) -> a
fst (x, y) = x
snd :: (a, b) -> b
snd (x, y) = y
|
| |
Werte
|
⊥
(⊥, ⊥)
(⊥, y)
(x, ⊥)
(x, y)
|
| |
Anzahl Werte
|
data A = A1 | A2 | ... | An
data B = B1 | B2 | ... | Bm
type P = (A, B)
|
| |
?
|
Wieviele Werte besitzt P?
|
|
(n + 1) * (m + 1) + 1
|
| |
? |
CPO für (Bool, Bool) ?
|
| |
Beispiele
|
für Funktionen mit Paaren
|
|
pair :: (a -> b, a -> c) -> a -> (b, c)
pair (f, g) x = (f x, g x)
cross :: (a -> b, c -> d) -> (a, c) -> (b, d)
cross (f, g) = pair (f . fst, g . snd)
|
| |
cross
|
ist eine nichtstrikte Funktion
|
|
cross (f, g) ⊥ = (f ⊥, g ⊥)
|
| |
Beweis |
|
cross (f,g) ⊥
|
= | { definition cross } |
|
pair (f . fst, g . snd) ⊥
|
= | { Definition pair } |
|
((f .fst) ⊥, (g . snd) ⊥)
|
= | { Definition . } |
|
(f (fst ⊥), g (snd ⊥))
|
= | { Striktheit von fst, snd } |
|
(f ⊥, g ⊥)
|
|
| |
Eigenschaften
Gesetzte
|
für cross und pair
|
|
fst . pair (f, g) = f
snd . pair (f, g) = g
pair (f, g) . h = pair (f . h, g . h)
cross (f, g) . pair (h, k) = pair (f . h, g . k)
|
| |
Beweis der letzten Gleichung
|
|
cross (f, g) . pair (h, k)
|
= | { definition cross } |
|
pair (f . fst, g . snd) . pair (h, k)
|
= | { 3. Gleichung } |
|
pair (f . fst . pair (h, k), g . snd . pair (h, k))
|
= | { 1. und 2. Gleichung } |
|
pair (f . h, g . k)
|
|
| |
Vergleiche von Paaren
|
instance (Eq a, Eq b) => Eq (a, b) where
(x, y) == (u, v) = (x == u) && (y == v)
instance (Ord a, Ord b) => Ord (a, b) where
(x, y) < (u, v) = (x < u) || (x == u && y < v)
|
| |
gegeben |
(1, undefined) < (2, undefined) = ?
|
?
|
Resultat
|
|
True
|
gegeben |
(undefined, 1) < (undefined, 2) = ?
(undefined, 1) < (3, 2) = ?
(3, 1) < (undefined, 2) = ?
|
?
|
Resultat
|
|
⊥
|
Operatoren |
für pair und cross: mit Currying
|
|
(&&&) :: (a -> b) -> (a -> c) -> a -> (b, c)
(&&&) = curry pair
(***) :: (a -> b) -> (c -> d) -> (a, c) -> (b, d)
(***) = curry cross
|
| |
?
|
Wie definiert man einen Datentyp für Tripel und Quadrupel?
|
|
data Triple a b c = MkTriple a b c
type Triple a b c = (a, b, c)
|
|