Skip to content

Replace monomorphic proxies by Type.Proxy.Proxy and polymorphic variables #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions src/Record.purs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ module Record
import Prelude

import Data.Function.Uncurried (runFn2)
import Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol)
import Data.Symbol (class IsSymbol, reflectSymbol)
import Prim.Row (class Lacks, class Cons, class Nub, class Union)
import Prim.RowList (class RowToList, RowList, Cons, Nil)
import Record.Unsafe (unsafeGet, unsafeSet, unsafeDelete)
import Record.Unsafe.Union (unsafeUnionFn)
import Type.Data.RowList (RLProxy(..))
import Type.Proxy (Proxy(..))
import Unsafe.Coerce (unsafeCoerce)

-- | Get a property for a label which is specified using a value-level proxy for
Expand All @@ -31,13 +31,13 @@ import Unsafe.Coerce (unsafeCoerce)
-- | For example:
-- |
-- | ```purescript
-- | get (SProxy :: SProxy "x") :: forall r a. { x :: a | r } -> a
-- | get (Proxy :: Proxy "x") :: forall r a. { x :: a | r } -> a
-- | ```
get
:: forall r r' l a
:: forall proxy r r' l a
. IsSymbol l
=> Cons l a r' r
=> SProxy l
=> proxy l
-> Record r
-> a
get l r = unsafeGet (reflectSymbol l) r
Expand All @@ -48,15 +48,15 @@ get l r = unsafeGet (reflectSymbol l) r
-- | For example:
-- |
-- | ```purescript
-- | set (SProxy :: SProxy "x")
-- | set (Proxy :: Proxy "x")
-- | :: forall r a b. a -> { x :: b | r } -> { x :: a | r }
-- | ```
set
:: forall r1 r2 r l a b
:: forall proxy r1 r2 r l a b
. IsSymbol l
=> Cons l a r r1
=> Cons l b r r2
=> SProxy l
=> proxy l
-> b
-> Record r1
-> Record r2
Expand All @@ -68,15 +68,15 @@ set l b r = unsafeSet (reflectSymbol l) b r
-- | For example:
-- |
-- | ```purescript
-- | modify (SProxy :: SProxy "x")
-- | modify (Proxy :: Proxy "x")
-- | :: forall r a b. (a -> b) -> { x :: a | r } -> { x :: b | r }
-- | ```
modify
:: forall r1 r2 r l a b
:: forall proxy r1 r2 r l a b
. IsSymbol l
=> Cons l a r r1
=> Cons l b r r2
=> SProxy l
=> proxy l
-> (a -> b)
-> Record r1
-> Record r2
Expand All @@ -88,15 +88,15 @@ modify l f r = set l (f (get l r)) r
-- | For example:
-- |
-- | ```purescript
-- | insert (SProxy :: SProxy "x")
-- | insert (Proxy :: Proxy "x")
-- | :: forall r a. Lacks "x" r => a -> { | r } -> { x :: a | r }
-- | ```
insert
:: forall r1 r2 l a
:: forall proxy r1 r2 l a
. IsSymbol l
=> Lacks l r1
=> Cons l a r1 r2
=> SProxy l
=> proxy l
-> a
-> Record r1
-> Record r2
Expand All @@ -111,15 +111,15 @@ insert l a r = unsafeSet (reflectSymbol l) a r
-- | For example:
-- |
-- | ```purescript
-- | delete (SProxy :: SProxy "x")
-- | delete (Proxy :: Proxy "x")
-- | :: forall r a. Lacks "x" r => { x :: a | r } -> { | r }
-- | ```
delete
:: forall r1 r2 l a
:: forall proxy r1 r2 l a
. IsSymbol l
=> Lacks l r1
=> Cons l a r1 r2
=> SProxy l
=> proxy l
-> Record r2
-> Record r1
delete l r = unsafeDelete (reflectSymbol l) r
Expand All @@ -133,18 +133,18 @@ delete l r = unsafeDelete (reflectSymbol l) r
-- | For example:
-- |
-- | ```purescript
-- | rename (SProxy :: SProxy "x") (SProxy :: SProxy "y")
-- | rename (Proxy :: Proxy "x") (Proxy :: Proxy "y")
-- | :: forall a r. Lacks "x" r => Lacks "y" r => { x :: a | r} -> { y :: a | r}
-- | ```
rename :: forall prev next ty input inter output
rename :: forall proxy prev next ty input inter output
. IsSymbol prev
=> IsSymbol next
=> Cons prev ty inter input
=> Lacks prev inter
=> Cons next ty inter output
=> Lacks next inter
=> SProxy prev
-> SProxy next
=> proxy prev
-> proxy next
-> Record input
-> Record output
rename prev next record =
Expand Down Expand Up @@ -221,10 +221,10 @@ equal
=> Record r
-> Record r
-> Boolean
equal a b = equalFields (RLProxy :: RLProxy rs) a b
equal a b = equalFields (Proxy :: Proxy rs) a b

class EqualFields (rs :: RowList Type) (row :: Row Type) | rs -> row where
equalFields :: RLProxy rs -> Record row -> Record row -> Boolean
equalFields :: forall rlproxy. rlproxy rs -> Record row -> Record row -> Boolean

instance equalFieldsCons
::
Expand All @@ -235,8 +235,8 @@ instance equalFieldsCons
) => EqualFields (Cons name ty tail) row where
equalFields _ a b = get' a == get' b && equalRest a b
where
get' = get (SProxy :: SProxy name)
equalRest = equalFields (RLProxy :: RLProxy tail)
get' = get (Proxy :: Proxy name)
equalRest = equalFields (Proxy :: Proxy tail)

instance equalFieldsNil :: EqualFields Nil row where
equalFields _ _ _ = true
20 changes: 10 additions & 10 deletions src/Record/Builder.purs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module Record.Builder
import Prelude

import Data.Function.Uncurried (runFn2)
import Data.Symbol (class IsSymbol, SProxy, reflectSymbol)
import Data.Symbol (class IsSymbol, reflectSymbol)
import Prim.Row as Row
import Record.Unsafe.Union (unsafeUnionFn)
import Unsafe.Coerce (unsafeCoerce)
Expand Down Expand Up @@ -55,46 +55,46 @@ derive newtype instance categoryBuilder :: Category Builder

-- | Build by inserting a new field.
insert
:: forall l a r1 r2
:: forall proxy l a r1 r2
. Row.Cons l a r1 r2
=> Row.Lacks l r1
=> IsSymbol l
=> SProxy l
=> proxy l
-> a
-> Builder (Record r1) (Record r2)
insert l a = Builder \r1 -> unsafeInsert (reflectSymbol l) a r1

-- | Build by modifying an existing field.
modify
:: forall l a b r r1 r2
:: forall proxy l a b r r1 r2
. Row.Cons l a r r1
=> Row.Cons l b r r2
=> IsSymbol l
=> SProxy l
=> proxy l
-> (a -> b)
-> Builder (Record r1) (Record r2)
modify l f = Builder \r1 -> unsafeModify (reflectSymbol l) f r1

-- | Build by deleting an existing field.
delete
:: forall l a r1 r2
:: forall proxy l a r1 r2
. IsSymbol l
=> Row.Lacks l r1
=> Row.Cons l a r1 r2
=> SProxy l
=> proxy l
-> Builder (Record r2) (Record r1)
delete l = Builder \r2 -> unsafeDelete (reflectSymbol l) r2

-- | Build by renaming an existing field.
rename :: forall l1 l2 a r1 r2 r3
rename :: forall proxy l1 l2 a r1 r2 r3
. IsSymbol l1
=> IsSymbol l2
=> Row.Cons l1 a r2 r1
=> Row.Lacks l1 r2
=> Row.Cons l2 a r2 r3
=> Row.Lacks l2 r2
=> SProxy l1
-> SProxy l2
=> proxy l1
-> proxy l2
-> Builder (Record r1) (Record r3)
rename l1 l2 = Builder \r1 -> unsafeRename (reflectSymbol l1) (reflectSymbol l2) r1

Expand Down
14 changes: 7 additions & 7 deletions src/Record/ST.purs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Record.ST
import Prelude

import Control.Monad.ST (ST, Region)
import Data.Symbol (class IsSymbol, SProxy, reflectSymbol)
import Data.Symbol (class IsSymbol, reflectSymbol)
import Prim.Row as Row

-- | A value of type `STRecord h r` represents a mutable record with fields `r`,
Expand All @@ -36,10 +36,10 @@ foreign import unsafePeek
-- | Read the current value of a field in a mutable record, by providing a
-- | type-level representative for the label which should be read.
peek
:: forall l h a r r1
:: forall proxy l h a r r1
. Row.Cons l a r1 r
=> IsSymbol l
=> SProxy l
=> proxy l
-> STRecord h r
-> ST h a
peek l = unsafePeek (reflectSymbol l)
Expand All @@ -54,10 +54,10 @@ foreign import unsafePoke
-- | Modify a record in place, by providing a type-level representative for the label
-- | which should be updated.
poke
:: forall l h a r r1
:: forall proxy l h a r r1
. Row.Cons l a r1 r
=> IsSymbol l
=> SProxy l
=> proxy l
-> a
-> STRecord h r
-> ST h Unit
Expand All @@ -74,10 +74,10 @@ foreign import unsafeModify
-- | by providing a type-level representative for the label to update
-- | and a function to update it.
modify
:: forall l h a r r1
:: forall proxy l h a r r1
. Row.Cons l a r1 r
=> IsSymbol l
=> SProxy l
=> proxy l
-> (a -> a)
-> STRecord h r
-> ST h Unit
Expand Down
8 changes: 4 additions & 4 deletions test/Examples.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ module Examples where
import Prelude

import Record as Record
import Data.Symbol (SProxy(..))
import Type.Proxy (Proxy(..))

x_ = SProxy :: SProxy "x"
y_ = SProxy :: SProxy "y"
z_ = SProxy :: SProxy "z"
x_ = Proxy :: Proxy "x"
y_ = Proxy :: Proxy "y"
z_ = Proxy :: Proxy "z"

gotX :: Int
gotX = Record.get x_ { x: 1 }
Expand Down
8 changes: 4 additions & 4 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import Record.Builder as Builder
import Control.Monad.ST (run) as ST
import Record.ST (poke, thaw, freeze, modify) as ST
import Record.Unsafe (unsafeHas)
import Data.Symbol (SProxy(..))
import Test.Assert (assert')
import Type.Proxy (Proxy(..))

main :: Effect Unit
main = do
let x = SProxy :: SProxy "x"
y = SProxy :: SProxy "y"
z = SProxy :: SProxy "z"
let x = Proxy :: Proxy "x"
y = Proxy :: Proxy "y"
z = Proxy :: Proxy "z"

assert' "insert, get" $
get x (insert x 42 {}) == 42
Expand Down