-
-
Save Risto-Stevcev/b5737355f58fee3e452397690b095fa4 to your computer and use it in GitHub Desktop.
records as association lists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module AssocList where | |
import Data.List (List(..)) | |
import Data.Record (delete, get) | |
import Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol) | |
import Data.Tuple (Tuple(..)) | |
import Type.Row (class RowLacks, class RowToList, Cons, Nil, RLProxy(RLProxy), kind RowList) | |
class RLToList | |
(rl :: RowList) | |
(row :: # Type) | |
(a :: Type) | |
| rl -> row a | |
where | |
rlToList :: RLProxy rl -> Record row -> List (Tuple String a) | |
instance basisRLToList :: RLToList Nil () a where | |
rlToList _ _ = Nil | |
instance ihRLToList :: | |
( IsSymbol k | |
, RowLacks k rowtail | |
, RowCons k a rowtail row | |
, RLToList tail rowtail a | |
) => RLToList (Cons k a tail) row a where | |
rlToList _ rec = Cons hd tl where | |
sym :: SProxy k | |
sym = SProxy | |
key :: String | |
key = reflectSymbol sym | |
val :: a | |
val = get sym rec | |
hd = Tuple key val | |
rectl :: Record rowtail | |
rectl = delete (SProxy :: SProxy k) rec | |
tl :: List (Tuple String a) | |
tl = rlToList (RLProxy :: RLProxy tail) rectl | |
class RecToList | |
(row :: # Type) | |
(a :: Type) | |
| row -> a where | |
recToList :: Record row -> List (Tuple String a) | |
instance recToListInstance :: | |
( RowToList row lst | |
, RLToList lst row a | |
) => RecToList row a where | |
recToList rec = rlToList (RLProxy :: RLProxy lst) rec | |
x = recToList {x: 15, y: 17} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment