Skip to content

Instantly share code, notes, and snippets.

@natefaubion
Last active March 1, 2020 22:01
Show Gist options
  • Save natefaubion/b3b43fbc10bc1a50dc19de7e837cfa2c to your computer and use it in GitHub Desktop.
Save natefaubion/b3b43fbc10bc1a50dc19de7e837cfa2c to your computer and use it in GitHub Desktop.
module Deref where
import Prelude hiding (map, apply, pure)
import Record as Record
import Prelude as Prelude
import Prim.Row as Row
import Data.Symbol (SProxy(..), class IsSymbol)
data Deref r1 (r2 :: # Type) a = Deref ({ | r1 } -> a)
data DerefOf a (sym :: Symbol) = DerefOf
deref :: forall a b. DerefOf a b
deref = DerefOf
map ::
forall r1 r2 rx sym a b.
IsSymbol sym =>
Row.Cons sym a rx r1 =>
Row.Cons sym a () r2 =>
(a -> b) -> DerefOf a sym -> Deref r1 r2 b
map f _ = Deref (Prelude.map f (Record.get (SProxy :: _ sym)))
apply ::
forall sproxy r1 r2 r3 rx sym a b.
IsSymbol sym =>
Row.Cons sym a rx r1 =>
Row.Cons sym a r2 r3 =>
Deref r1 r2 (a -> b) -> DerefOf a sym -> Deref r1 r3 b
apply (Deref a) _ = Deref (Prelude.apply a (Record.get (SProxy :: _ sym)))
test = ado
a <- deref :: _ "a"
b <- deref :: _ "b"
in a + b
runDeref :: forall r a. Deref r r a -> { | r } -> a
runDeref (Deref k) = k
result = runDeref test { a: 1, b: 2 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment