Generic grouping and sorting RALF HINZE Institut f ur Informatik - - PowerPoint PPT Presentation

generic grouping and sorting
SMART_READER_LITE
LIVE PREVIEW

Generic grouping and sorting RALF HINZE Institut f ur Informatik - - PowerPoint PPT Presentation

Generic grouping and sorting RALF HINZE Institut f ur Informatik III, Universit at Bonn R omerstrae 164, 53117 Bonn, Germany Email: ralf@informatik.uni-bonn.de Homepage: http://www.informatik.uni-bonn.de/~ralf October, 2005 (Pick up


slide-1
SLIDE 1

1 ◭ ◭ ◭ ◮ ◮ ◮ ✷

Generic grouping and sorting

RALF HINZE Institut f¨ ur Informatik III, Universit¨ at Bonn R¨

  • merstraße 164, 53117 Bonn, Germany

Email: ralf@informatik.uni-bonn.de Homepage: http://www.informatik.uni-bonn.de/~ralf October, 2005 (Pick up the slides at .../~ralf/talks.html#T45.)

slide-2
SLIDE 2

2 ◭ ◭ ◭ ◮ ◮ ◮ ✷

Equivalence relation over a type

Capture equivalence relations using a generalized algebraic data type (GADT): data Equiv :: ⋆ → ⋆ where Char :: Equiv Char IgnoreCase :: Equiv Char Unit :: Equiv () Sum :: Equiv τ1 → Equiv τ2 → Equiv (τ1 + τ2) Pair :: Equiv τ1 → Equiv τ2 → Equiv (τ1 × τ2) List :: Equiv τ → Equiv [τ ] Bag :: Equiv τ → Equiv [τ ]

☞ Set omitted.

slide-3
SLIDE 3

3 ◭ ◭ ◭ ◮ ◮ ◮ ✷

Overview

related :: (Equiv τ) → τ → τ → Bool sort :: (Equiv τ) → [τ ] → [τ ] group :: (Equiv τ) → [(τ, ν)] → [(τ, [ν])]

slide-4
SLIDE 4

4 ◭ ◭ ◭ ◮ ◮ ◮ ✷

Are two elements related?

related :: (Equiv τ) → τ → τ → Bool related (Char) x y = x y related (IgnoreCase) x y = toUpper x toUpper y related (Unit) x y = True related (Sum r1 r2) (Inl x1) (Inl y1) = related (r1) x1 y1 related (Sum r1 r2) (Inl x1) (Inr y2) = False related (Sum r1 r2) (Inr x2) (Inl y1) = False related (Sum r1 r2) (Inr x2) (Inr y2) = related (r2) x2 y2 related (Pair r1 r2) (x1, x2) (y1, y2) = related (r1) x1 y1 ∧ related (r2) x2 y2 related (Bag r) xs ys = related (List r) (sort (r) xs) (sort (r) ys)

☞ The List case can be done generically (not shown). The IgnoreCase and

the Bag case are done via normalization.

slide-5
SLIDE 5

5 ◭ ◭ ◭ ◮ ◮ ◮ ✷

Generic sorting

sort :: (Equiv τ) → [τ ] → [τ ] sort (Char) xs = sortChar xs sort (IgnoreCase) xs = sort (Char) [toUpper x | x ← xs ] sort (Unit) xs = xs sort (Sum r1 r2) xs = [Inl y1 | y1 ← sort (r1) [x1 | Inl x1 ← xs ]] + + [Inr y2 | y2 ← sort (r2) [x2 | Inr x2 ← xs ]] sort (Pair r1 r2) xs = [(x1, y2) | (x1, ys2) ← group (r1) xs , y2 ← sort (r2) ys2] sort (Bag r) xs = sort (List r) [sort (r) x | x ← xs ]

slide-6
SLIDE 6

6 ◭ ◭ ◭ ◮ ◮ ◮ ✷

Generic grouping

group :: (Equiv τ) → [(τ, ν)] → [(τ, [ν])] group (Char) xs = groupChar xs group (IgnoreCase) xs = group (Char) [(toUpper x, v) | (x, v) ← xs ] group (Unit) xs = make ((), [v | ((), v) ← xs ]) group (Sum r1 r2) xs = [(Inl y1, vs) | (y1, vs) ← group (r1) [(x1, v) | (Inl x1, v) ← xs ]] + + [(Inr y2, vs) | (y2, vs) ← group (r2) [(x2, v) | (Inr x2, v) ← xs ]] group (Pair r1 r2) xs = [ ((a1, a2), vs) | (a1, ys) ← group (r1) [(a1, (a2, v)) | ((a1, a2), v) ← xs ] , (a2, vs) ← group (r2) ys ] group (Bag r) xs = group (List r) [(sort (r) x, v) | (x, v) ← xs ]

slide-7
SLIDE 7

7 ◭ ◭ ◭ ◮ ◮ ◮ ✷

Generic grouping — continued

make :: (τ, [ν]) → [(τ, [ν])] make (a, [ ]) = [ ] make (a, xs) = [(a, xs)]

slide-8
SLIDE 8

8 ◭ ◭ ◭ ◮ ◮ ◮ ✷

Dealing with arbitrary data types

The top-level structure of a list: fromList :: [τ ] → () + τ × [τ ] fromList [ ] = Inl () fromList (x : xs) = Inr (x, xs) toList :: () + τ × [τ ] → [τ ] toList (Inl ()) = [ ] toList (Inr (x, xs)) = x : xs An equivalence relation for the top-level structure: list :: Equiv τ → Equiv (() + τ × [τ ]) list r = Sum Unit (Pair r (List r)) The missing piece for related: related (List r) xs ys = related (list r) (fromList xs) (fromList ys)