@@ -3,10 +3,17 @@ module Data.Bounded
3
3
, bottom
4
4
, top
5
5
, module Data.Ord
6
+ , class BoundedRecord , bottomRecord , topRecord
6
7
) where
7
8
8
- import Data.Ord (class Ord , Ordering (..), compare , (<), (<=), (>), (>=))
9
+ import Data.Ord (class Ord , class OrdRecord , Ordering (..), compare , (<), (<=), (>), (>=))
10
+ import Data.Symbol (class IsSymbol , SProxy (..), reflectSymbol )
9
11
import Data.Unit (Unit , unit )
12
+ import Prim.Row as Row
13
+ import Prim.RowList as RL
14
+ import Record.Unsafe (unsafeSet )
15
+ import Type.Data.Row (RProxy (..))
16
+ import Type.Data.RowList (RLProxy (..))
10
17
11
18
-- | The `Bounded` type class represents totally ordered types that have an
12
19
-- | upper and lower boundary.
@@ -54,3 +61,41 @@ foreign import bottomNumber :: Number
54
61
instance boundedNumber :: Bounded Number where
55
62
top = topNumber
56
63
bottom = bottomNumber
64
+
65
+ class OrdRecord rowlist row <= BoundedRecord rowlist row subrow | rowlist -> subrow where
66
+ topRecord :: RLProxy rowlist -> RProxy row -> Record subrow
67
+ bottomRecord :: RLProxy rowlist -> RProxy row -> Record subrow
68
+
69
+ instance boundedRecordNil :: BoundedRecord RL.Nil row () where
70
+ topRecord _ _ = {}
71
+ bottomRecord _ _ = {}
72
+
73
+ instance boundedRecordCons
74
+ :: ( IsSymbol key
75
+ , Bounded focus
76
+ , Row.Cons key focus rowTail row
77
+ , Row.Cons key focus subrowTail subrow
78
+ , BoundedRecord rowlistTail row subrowTail
79
+ )
80
+ => BoundedRecord (RL.Cons key focus rowlistTail ) row subrow where
81
+ topRecord _ rowProxy
82
+ = insert top tail
83
+ where
84
+ key = reflectSymbol (SProxy :: SProxy key )
85
+ insert = unsafeSet key :: focus -> Record subrowTail -> Record subrow
86
+ tail = topRecord (RLProxy :: RLProxy rowlistTail ) rowProxy
87
+
88
+ bottomRecord _ rowProxy
89
+ = insert bottom tail
90
+ where
91
+ key = reflectSymbol (SProxy :: SProxy key )
92
+ insert = unsafeSet key :: focus -> Record subrowTail -> Record subrow
93
+ tail = bottomRecord (RLProxy :: RLProxy rowlistTail ) rowProxy
94
+
95
+ instance boundedRecord
96
+ :: ( RL.RowToList row list
97
+ , BoundedRecord list row row
98
+ )
99
+ => Bounded (Record row ) where
100
+ top = topRecord (RLProxy :: RLProxy list ) (RProxy :: RProxy row )
101
+ bottom = bottomRecord (RLProxy :: RLProxy list ) (RProxy :: RProxy row )
0 commit comments