From e841f1d783358eed9231126d237ac3292d67ad21 Mon Sep 17 00:00:00 2001 From: Liam Goodacre Date: Wed, 25 Apr 2018 23:24:39 +0100 Subject: [PATCH] Updates for 0.12 --- package.json | 2 +- src/Data/Record.purs | 36 ++++++++++----------- src/Data/Record/Builder.purs | 24 +++++++------- src/Data/Record/ST.js | 14 +++----- src/Data/Record/ST.purs | 62 +++++++++++++++--------------------- test/Main.purs | 19 +++++------ 6 files changed, 71 insertions(+), 86 deletions(-) diff --git a/package.json b/package.json index 3fcb467..34e6f24 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "scripts": { "clean": "rimraf output && rimraf .pulp-cache", "build": "eslint src && pulp build -- --censor-lib --strict", - "test": "pulp test" + "test": "pulp test --check-main-type Effect.Effect" }, "devDependencies": { "eslint": "^4.19.1", diff --git a/src/Data/Record.purs b/src/Data/Record.purs index 9048cbf..1104a37 100644 --- a/src/Data/Record.purs +++ b/src/Data/Record.purs @@ -14,7 +14,7 @@ import Data.Function.Uncurried (runFn2, runFn3) import Data.Record.Unsafe (unsafeGetFn, unsafeSetFn, unsafeDeleteFn) import Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol) import Prelude (class Eq, (&&), (==)) -import Type.Row (class RowLacks, class RowToList, Cons, Nil, RLProxy(RLProxy), kind RowList) +import Type.Row (class Lacks, class Cons, class RowToList, Cons, Nil, RLProxy(RLProxy), kind RowList) -- | Get a property for a label which is specified using a value-level proxy for -- | a type-level string. @@ -27,7 +27,7 @@ import Type.Row (class RowLacks, class RowToList, Cons, Nil, RLProxy(RLProxy), k get :: forall r r' l a . IsSymbol l - => RowCons l a r' r + => Cons l a r' r => SProxy l -> Record r -> a @@ -45,8 +45,8 @@ get l r = runFn2 unsafeGetFn (reflectSymbol l) r set :: forall r1 r2 r l a b . IsSymbol l - => RowCons l a r r1 - => RowCons l b r r2 + => Cons l a r r1 + => Cons l b r r2 => SProxy l -> b -> Record r1 @@ -65,8 +65,8 @@ set l b r = runFn3 unsafeSetFn (reflectSymbol l) b r modify :: forall r1 r2 r l a b . IsSymbol l - => RowCons l a r r1 - => RowCons l b r r2 + => Cons l a r r1 + => Cons l b r r2 => SProxy l -> (a -> b) -> Record r1 @@ -80,13 +80,13 @@ modify l f r = set l (f (get l r)) r -- | -- | ```purescript -- | insert (SProxy :: SProxy "x") --- | :: forall r a. RowLacks "x" r => a -> { | r } -> { x :: a | r } +-- | :: forall r a. Lacks "x" r => a -> { | r } -> { x :: a | r } -- | ``` insert :: forall r1 r2 l a . IsSymbol l - => RowLacks l r1 - => RowCons l a r1 r2 + => Lacks l r1 + => Cons l a r1 r2 => SProxy l -> a -> Record r1 @@ -103,13 +103,13 @@ insert l a r = runFn3 unsafeSetFn (reflectSymbol l) a r -- | -- | ```purescript -- | delete (SProxy :: SProxy "x") --- | :: forall r a. RowLacks "x" r => { x :: a | r } -> { | r } +-- | :: forall r a. Lacks "x" r => { x :: a | r } -> { | r } -- | ``` delete :: forall r1 r2 l a . IsSymbol l - => RowLacks l r1 - => RowCons l a r1 r2 + => Lacks l r1 + => Cons l a r1 r2 => SProxy l -> Record r2 -> Record r1 @@ -125,15 +125,15 @@ delete l r = runFn2 unsafeDeleteFn (reflectSymbol l) r -- | -- | ```purescript -- | rename (SProxy :: SProxy "x") (SProxy :: SProxy "y") --- | :: forall a r. RowLacks "x" r => RowLacks "y" r => { x :: a | r} -> { y :: a | r} +-- | :: forall a r. Lacks "x" r => Lacks "y" r => { x :: a | r} -> { y :: a | r} -- | ``` rename :: forall prev next ty input inter output . IsSymbol prev => IsSymbol next - => RowCons prev ty inter input - => RowLacks prev inter - => RowCons next ty inter output - => RowLacks next inter + => Cons prev ty inter input + => Lacks prev inter + => Cons next ty inter output + => Lacks next inter => SProxy prev -> SProxy next -> Record input @@ -158,7 +158,7 @@ instance equalFieldsCons :: ( IsSymbol name , Eq ty - , RowCons name ty tailRow row + , Cons name ty tailRow row , EqualFields tail row ) => EqualFields (Cons name ty tail) row where equalFields _ a b = get' a == get' b && equalRest a b diff --git a/src/Data/Record/Builder.purs b/src/Data/Record/Builder.purs index e7e0a85..004901b 100644 --- a/src/Data/Record/Builder.purs +++ b/src/Data/Record/Builder.purs @@ -11,7 +11,7 @@ module Data.Record.Builder import Prelude import Data.Symbol (class IsSymbol, SProxy, reflectSymbol) -import Type.Row (class RowLacks) +import Type.Row as Row foreign import copyRecord :: forall r1. Record r1 -> Record r1 foreign import unsafeInsert :: forall a r1 r2. String -> a -> Record r1 -> Record r2 @@ -43,8 +43,8 @@ derive newtype instance categoryBuilder :: Category Builder -- | Build by inserting a new field. insert :: forall l a r1 r2 - . RowCons l a r1 r2 - => RowLacks l r1 + . Row.Cons l a r1 r2 + => Row.Lacks l r1 => IsSymbol l => SProxy l -> a @@ -54,8 +54,8 @@ insert l a = Builder \r1 -> unsafeInsert (reflectSymbol l) a r1 -- | Build by modifying an existing field. modify :: forall l a b r r1 r2 - . RowCons l a r r1 - => RowCons l b r r2 + . Row.Cons l a r r1 + => Row.Cons l b r r2 => IsSymbol l => SProxy l -> (a -> b) @@ -66,8 +66,8 @@ modify l f = Builder \r1 -> unsafeModify (reflectSymbol l) f r1 delete :: forall l a r1 r2 . IsSymbol l - => RowLacks l r1 - => RowCons l a r1 r2 + => Row.Lacks l r1 + => Row.Cons l a r1 r2 => SProxy l -> Builder (Record r2) (Record r1) delete l = Builder \r2 -> unsafeDelete (reflectSymbol l) r2 @@ -76,10 +76,10 @@ delete l = Builder \r2 -> unsafeDelete (reflectSymbol l) r2 rename :: forall l1 l2 a r1 r2 r3 . IsSymbol l1 => IsSymbol l2 - => RowCons l1 a r2 r1 - => RowLacks l1 r2 - => RowCons l2 a r2 r3 - => RowLacks l2 r2 + => Row.Cons l1 a r2 r1 + => Row.Lacks l1 r2 + => Row.Cons l2 a r2 r3 + => Row.Lacks l2 r2 => SProxy l1 -> SProxy l2 -> Builder (Record r1) (Record r3) @@ -88,7 +88,7 @@ rename l1 l2 = Builder \r1 -> unsafeRename (reflectSymbol l1) (reflectSymbol l2) -- | Build by merging existing fields from another record. merge :: forall r1 r2 r3 - . Union r1 r2 r3 + . Row.Union r1 r2 r3 => Record r2 -> Builder (Record r1) (Record r3) merge r2 = Builder \r1 -> unsafeMerge r1 r2 diff --git a/src/Data/Record/ST.js b/src/Data/Record/ST.js index 12b97c4..aee1286 100644 --- a/src/Data/Record/ST.js +++ b/src/Data/Record/ST.js @@ -10,25 +10,19 @@ function copyRecord(rec) { return copy; } -exports.runSTRecord = function(rec) { - return function() { - return copyRecord(rec()); - }; -}; - -exports.freezeSTRecord = function(rec) { +exports.freeze = function(rec) { return function() { return copyRecord(rec); }; }; -exports.thawSTRecord = function(rec) { +exports.thaw = function(rec) { return function() { return copyRecord(rec); }; }; -exports.unsafePeekSTRecord = function(l) { +exports.unsafePeek = function(l) { return function(rec) { return function() { return rec[l]; @@ -36,7 +30,7 @@ exports.unsafePeekSTRecord = function(l) { }; }; -exports.unsafePokeSTRecord = function(l) { +exports.unsafePoke = function(l) { return function(a) { return function(rec) { return function() { diff --git a/src/Data/Record/ST.purs b/src/Data/Record/ST.purs index 671752f..942744f 100644 --- a/src/Data/Record/ST.purs +++ b/src/Data/Record/ST.purs @@ -1,71 +1,61 @@ module Data.Record.ST ( STRecord - , freezeSTRecord - , thawSTRecord - , peekSTRecord - , pokeSTRecord - , runSTRecord - , pureSTRecord + , freeze + , thaw + , peek + , poke ) where import Prelude -import Control.Monad.Eff (Eff, runPure) -import Control.Monad.ST (ST) +import Control.Monad.ST (ST, kind Region) import Data.Symbol (class IsSymbol, SProxy, reflectSymbol) +import Prim.Row as Row -- | A value of type `STRecord h r` represents a mutable record with fields `r`, -- | belonging to the state thread `h`. -- | --- | Create values of type `STRecord` using `thawSTRecord`. -foreign import data STRecord :: Type -> # Type -> Type +-- | Create values of type `STRecord` using `thaw`. +foreign import data STRecord :: Region -> # Type -> Type -- | Freeze a mutable record, creating a copy. -foreign import freezeSTRecord :: forall h r eff. STRecord h r -> Eff (st :: ST h | eff) (Record r) +foreign import freeze :: forall h r. STRecord h r -> ST h (Record r) -- | Thaw an immutable record, creating a copy. -foreign import thawSTRecord :: forall h r eff. Record r -> Eff (st :: ST h | eff) (STRecord h r) +foreign import thaw :: forall h r. Record r -> ST h (STRecord h r) --- | Run an ST computation safely, constructing a record. -foreign import runSTRecord :: forall r eff. (forall h. Eff (st :: ST h | eff) (STRecord h r)) -> Eff eff (Record r) - --- | Run an ST computation safely, constructing a record, assuming no other --- | types of effects. -pureSTRecord :: forall r. (forall h eff. Eff (st :: ST h | eff) (STRecord h r)) -> Record r -pureSTRecord st = runPure (runSTRecord st) - -foreign import unsafePeekSTRecord - :: forall a r h eff +foreign import unsafePeek + :: forall a r h . String -> STRecord h r - -> Eff (st :: ST h | eff) a + -> ST h a -- | Read the current value of a field in a mutable record, by providing a -- | type-level representative for the label which should be read. -peekSTRecord - :: forall l h a r r1 eff - . RowCons l a r1 r +peek + :: forall l h a r r1 + . Row.Cons l a r1 r => IsSymbol l => SProxy l -> STRecord h r - -> Eff (st :: ST h | eff) a -peekSTRecord l = unsafePeekSTRecord (reflectSymbol l) + -> ST h a +peek l = unsafePeek (reflectSymbol l) -foreign import unsafePokeSTRecord - :: forall a r h eff +foreign import unsafePoke + :: forall a r h . String -> a -> STRecord h r - -> Eff (st :: ST h | eff) Unit + -> ST h Unit -- | Modify a record in place, by providing a type-level representative for the label -- | which should be updated. -pokeSTRecord - :: forall l h a r r1 eff - . RowCons l a r1 r +poke + :: forall l h a r r1 + . Row.Cons l a r1 r => IsSymbol l => SProxy l -> a -> STRecord h r - -> Eff (st :: ST h | eff) Unit -pokeSTRecord l = unsafePokeSTRecord (reflectSymbol l) + -> ST h Unit +poke l = unsafePoke (reflectSymbol l) diff --git a/test/Main.purs b/test/Main.purs index 33b7174..e0867f7 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -2,15 +2,16 @@ module Test.Main where import Prelude -import Control.Monad.Eff (Eff) +import Effect (Effect) import Data.Record (delete, equal, get, insert, modify, rename, set) import Data.Record.Builder as Builder -import Data.Record.ST (pokeSTRecord, pureSTRecord, thawSTRecord) +import Control.Monad.ST (run) as ST +import Data.Record.ST (poke, thaw, freeze) as ST import Data.Record.Unsafe (unsafeHas) import Data.Symbol (SProxy(..)) -import Test.Assert (ASSERT, assert') +import Test.Assert (assert') -main :: Eff (assert :: ASSERT) Unit +main :: Effect Unit main = do let x = SProxy :: SProxy "x" y = SProxy :: SProxy "y" @@ -37,11 +38,11 @@ main = do assert' "unsafeHas2" $ not $ unsafeHas "b" { a: 42 } - let stTest1 = pureSTRecord do - rec <- thawSTRecord { x: 41, y: "" } - pokeSTRecord x 42 rec - pokeSTRecord y "testing" rec - pure rec + let stTest1 = ST.run do + rec <- ST.thaw { x: 41, y: "" } + ST.poke x 42 rec + ST.poke y "testing" rec + ST.freeze rec assert' "pokeSTRecord" $ stTest1.x == 42 && stTest1.y == "testing"