Skip to content

Commit c285815

Browse files
LiamGoodacrepaf31
authored andcommitted
Add RowToList class (#11)
* Add RowToList class * Add RowToList and ListToRow
1 parent 42a59d1 commit c285815

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/Type/Prelude.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ import Type.Data.Ordering (kind Ordering, LT, EQ, GT, OProxy(..), class IsOrderi
1212
import Type.Proxy (Proxy(..))
1313
import Type.Data.Symbol (SProxy(..), class IsSymbol, reflectSymbol, reifySymbol, class CompareSymbol, compareSymbol, class AppendSymbol, appendSymbol)
1414
import Type.Equality (class TypeEquals, from, to)
15-
import Type.Row (class RowLacks)
15+
import Type.Row (class RowLacks, class RowToList, class ListToRow)
1616

src/Type/Row.purs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
module Type.Row
22
( class RowLacks
33
, class RowLacking
4+
, kind RowList
5+
, Nil
6+
, Cons
7+
, class RowToList
8+
, class ListToRow
49
) where
510

611
-- Must not be exported
@@ -32,3 +37,30 @@ instance rowLacks
3237
, RowLacking Entry key typ row )
3338
=> RowLacks key row
3439

40+
41+
-- | A type-level list representation of a row
42+
foreign import kind RowList
43+
foreign import data Nil :: RowList
44+
foreign import data Cons :: Symbol -> Type -> RowList -> RowList
45+
46+
-- | Extract the collection of entries in a closed row of types.
47+
-- | The list of entries is sorted by label and preserves duplicates.
48+
-- | The inverse of this operation is `ListToRow`.
49+
-- | Solved by the compiler.
50+
class RowToList (row :: # Type)
51+
(list :: RowList) |
52+
row -> list
53+
54+
-- | Convert a RowList to a row of types.
55+
-- | The inverse of this operation is `RowToList`.
56+
class ListToRow (list :: RowList)
57+
(row :: # Type) |
58+
list -> row
59+
60+
instance listToRowNil
61+
:: ListToRow Nil ()
62+
63+
instance listToRowCons
64+
:: ( ListToRow tail tailRow
65+
, RowCons label ty tailRow row )
66+
=> ListToRow (Cons label ty tail) row

0 commit comments

Comments
 (0)