-
Notifications
You must be signed in to change notification settings - Fork 1
/
RowPolymorphism.hs
57 lines (47 loc) · 1.11 KB
/
RowPolymorphism.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
{-# LANGUAGE AllowAmbiguousTypes #-}
module Wakame.Examples.RowPolymorphism where
import Prelude
import Data.Kind (Type)
import GHC.TypeLits (KnownSymbol, Symbol)
import Wakame
-- * Row polymorphism
--
-- Theoretically, all row-polymorphic operations can be described as a
-- composition of the 3 basic functions:
--
-- - select_l :: {l} -> {l :: a | r} -> a
-- - add_l :: {l} -> a -> {absent(l) :: a | r} -> {l :: a | r}
-- - remove_l :: {l} -> {l :: a | r} -> {absent(l) :: a | r}
--
-- This module shows how to implement these functions.
select ::
forall (l :: Symbol) (a :: Type) r.
( IsRow r
, KnownSymbol l
, Nub (Of r) '[ '(l, a) ]
) =>
r -> a
select x =
unV (fromRow $ nub $ toRow x :: V '(l, a))
add ::
forall (l :: Symbol) (a :: Type) r r'.
( IsRow r
, IsRow r'
, KnownSymbol l
, Lacks l (Of r)
, Merge '[ '(l, a) ] (Of r) (Of r')
) =>
a -> r -> r'
add e x =
fromRow $ merge (toRow $ keyed @l e) (toRow x)
remove ::
forall (l :: Symbol) r r'.
( IsRow r
, IsRow r'
, KnownSymbol l
, Lacks l (Of r')
, Nub (Of r) (Of r')
) =>
r -> r'
remove x =
fromRow $ nub $ toRow x