Skip to content

Instantly share code, notes, and snippets.

@shamansir
Last active June 14, 2023 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shamansir/6b545aeb27b7d90032aed9bf26c7a775 to your computer and use it in GitHub Desktop.
Save shamansir/6b545aeb27b7d90032aed9bf26c7a775 to your computer and use it in GitHub Desktop.
PureScript Records Pairing
module Data.Record.Pairs where
import Prelude
import Data.Symbol (class IsSymbol)
import Prim.RowList as RL
class Pairs (xsA :: RL.RowList Type) (xsB :: RL.RowList Type)
class Converts a b where
convert :: a -> b
instance pairsNilNil :: Pairs RL.Nil RL.Nil
else instance pairsConsNil ::
( IsSymbol nameA
) => Pairs (RL.Cons nameA tA tailA) RL.Nil
else instance pairsNilCons ::
( IsSymbol nameB
) => Pairs RL.Nil (RL.Cons nameB tB tailB)
else instance pairsConsCons ::
( IsSymbol nameA
, IsSymbol nameB
, Pairs (RL.Cons nameA tA tailA) tailB
, Pairs tailA (RL.Cons nameB tB tailB)
, Pairs tailA tailB
, Converts tA tB
) => Pairs (RL.Cons nameA tA tailA) (RL.Cons nameB tB tailB)
testPairs :: forall ra rlA rb rlB. Pairs rlA rlB => RL.RowToList ra rlA => RL.RowToList rb rlB => Record ra -> Record rb -> Unit
testPairs _ _ = unit
foo1 :: Unit
foo1 = testPairs { a : "foo", b : 1 } { a : true, b : "x" }
-- foo2 :: Unit
-- foo2 = testPairs { a : "foo", b : 1 } { foo : true, bar : "x" }
instance Converts Int String where
convert _ = "42"
instance Converts String Boolean where
convert _ = false
instance Converts String String where
convert = identity
instance Converts Int Boolean where
convert _ = false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment