Skip to content

Ports purescript-proxy into this repo #230

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 16 commits into from
Oct 10, 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
4 changes: 4 additions & 0 deletions src/Control/Applicative.purs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Control.Apply (class Apply, apply, (*>), (<*), (<*>))

import Data.Functor (class Functor, map, void, ($>), (<#>), (<$), (<$>))
import Data.Unit (Unit, unit)
import Type.Proxy (Proxy(..))

-- | The `Applicative` type class extends the [`Apply`](#apply) type class
-- | with a `pure` function, which can be used to create values of type `f a`
Expand Down Expand Up @@ -38,6 +39,9 @@ instance applicativeFn :: Applicative ((->) r) where
instance applicativeArray :: Applicative Array where
pure x = [x]

instance applicativeProxy :: Applicative Proxy where
pure _ = Proxy

-- | `liftA1` provides a default implementation of `(<$>)` for any
-- | [`Applicative`](#applicative) functor, without using `(<$>)` as provided
-- | by the [`Functor`](#functor)-[`Applicative`](#applicative) superclass
Expand Down
4 changes: 4 additions & 0 deletions src/Control/Apply.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Control.Apply
import Data.Functor (class Functor, map, void, ($>), (<#>), (<$), (<$>))
import Data.Function (const)
import Control.Category (identity)
import Type.Proxy (Proxy(..))

-- | The `Apply` class provides the `(<*>)` which is used to apply a function
-- | to an argument under a type constructor.
Expand Down Expand Up @@ -54,6 +55,9 @@ instance applyArray :: Apply Array where

foreign import arrayApply :: forall a b. Array (a -> b) -> Array a -> Array b

instance applyProxy :: Apply Proxy where
apply _ _ = Proxy

-- | Combine two effectful actions, keeping only the result of the first.
applyFirst :: forall a b f. Apply f => f a -> f b -> f a
applyFirst a b = const <$> a <*> b
Expand Down
13 changes: 13 additions & 0 deletions src/Control/Bind.purs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Control.Category (identity)
import Data.Function (flip)
import Data.Functor (class Functor, map, void, ($>), (<#>), (<$), (<$>))
import Data.Unit (Unit)
import Type.Proxy (Proxy(..), Proxy2, Proxy3)

-- | The `Bind` type class extends the [`Apply`](#apply) type class with a
-- | "bind" operation `(>>=)` which composes computations in sequence, using
Expand Down Expand Up @@ -90,6 +91,9 @@ instance bindArray :: Bind Array where

foreign import arrayBind :: forall a b. Array a -> (a -> Array b) -> Array b

instance bindProxy :: Bind Proxy where
bind _ _ = Proxy

-- | A class for types whose values can safely be discarded
-- | in a `do` notation block.
-- |
Expand All @@ -101,6 +105,15 @@ class Discard a where
instance discardUnit :: Discard Unit where
discard = bind

instance discardProxy :: Discard (Proxy a) where
discard = bind

instance discardProxy2 :: Discard (Proxy2 a) where
discard = bind

instance discardProxy3 :: Discard (Proxy3 a) where
discard = bind

-- | Collapse two applications of a monadic type constructor into one.
join :: forall a m. Bind m => m (m a) -> m a
join m = m >>= identity
Expand Down
3 changes: 3 additions & 0 deletions src/Control/Monad.purs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Control.Bind (class Bind, bind, ap, ifM, join, (<=<), (=<<), (>=>), (>>=)

import Data.Functor (class Functor, map, void, ($>), (<#>), (<$), (<$>))
import Data.Unit (Unit)
import Type.Proxy (Proxy)

-- | The `Monad` type class combines the operations of the `Bind` and
-- | `Applicative` type classes. Therefore, `Monad` instances represent type
Expand All @@ -32,6 +33,8 @@ instance monadFn :: Monad ((->) r)

instance monadArray :: Monad Array

instance monadProxy :: Monad Proxy

-- | `liftM1` provides a default implementation of `(<$>)` for any
-- | [`Monad`](#monad), without using `(<$>)` as provided by the
-- | [`Functor`](#functor)-[`Monad`](#monad) superclass relationship.
Expand Down
5 changes: 5 additions & 0 deletions src/Data/BooleanAlgebra.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Data.Symbol (class IsSymbol)
import Data.Unit (Unit)
import Prim.Row as Row
import Prim.RowList as RL
import Type.Proxy (Proxy, Proxy2, Proxy3)

-- | The `BooleanAlgebra` type class represents types that behave like boolean
-- | values.
Expand All @@ -24,9 +25,13 @@ instance booleanAlgebraBoolean :: BooleanAlgebra Boolean
instance booleanAlgebraUnit :: BooleanAlgebra Unit
instance booleanAlgebraFn :: BooleanAlgebra b => BooleanAlgebra (a -> b)
instance booleanAlgebraRecord :: (RL.RowToList row list, BooleanAlgebraRecord list row row) => BooleanAlgebra (Record row)
instance booleanAlgebraProxy :: BooleanAlgebra (Proxy a)
instance booleanAlgebraProxy2 :: BooleanAlgebra (Proxy2 a)
instance booleanAlgebraProxy3 :: BooleanAlgebra (Proxy3 a)

-- | A class for records where all fields have `BooleanAlgebra` instances, used
-- | to implement the `BooleanAlgebra` instance for records.
class BooleanAlgebraRecord :: RL.RowList Type -> Row Type -> Row Type -> Constraint
class HeytingAlgebraRecord rowlist row subrow <= BooleanAlgebraRecord rowlist row subrow | rowlist -> subrow

instance booleanAlgebraRecordNil :: BooleanAlgebraRecord RL.Nil row ()
Expand Down
34 changes: 23 additions & 11 deletions src/Data/Bounded.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ module Data.Bounded
) where

import Data.Ord (class Ord, class OrdRecord, Ordering(..), compare, (<), (<=), (>), (>=))
import Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol)
import Data.Symbol (class IsSymbol, reflectSymbol)
import Data.Unit (Unit, unit)
import Prim.Row as Row
import Prim.RowList as RL
import Record.Unsafe (unsafeSet)
import Type.Data.Row (RProxy(..))
import Type.Data.RowList (RLProxy(..))
import Type.Proxy (Proxy(..), Proxy2(..), Proxy3(..))

-- | The `Bounded` type class represents totally ordered types that have an
-- | upper and lower boundary.
Expand Down Expand Up @@ -62,9 +61,22 @@ instance boundedNumber :: Bounded Number where
top = topNumber
bottom = bottomNumber

instance boundedProxy :: Bounded (Proxy a) where
bottom = Proxy
top = Proxy

instance boundedProxy2 :: Bounded (Proxy2 a) where
bottom = Proxy2
top = Proxy2

instance boundedProxy3 :: Bounded (Proxy3 a) where
bottom = Proxy3
top = Proxy3

class BoundedRecord :: RL.RowList Type -> Row Type -> Row Type -> Constraint
class OrdRecord rowlist row <= BoundedRecord rowlist row subrow | rowlist -> subrow where
topRecord :: RLProxy rowlist -> RProxy row -> Record subrow
bottomRecord :: RLProxy rowlist -> RProxy row -> Record subrow
topRecord :: forall rlproxy rproxy. rlproxy rowlist -> rproxy row -> Record subrow
bottomRecord :: forall rlproxy rproxy. rlproxy rowlist -> rproxy row -> Record subrow

instance boundedRecordNil :: BoundedRecord RL.Nil row () where
topRecord _ _ = {}
Expand All @@ -81,21 +93,21 @@ instance boundedRecordCons
topRecord _ rowProxy
= insert top tail
where
key = reflectSymbol (SProxy :: SProxy key)
key = reflectSymbol (Proxy :: Proxy key)
insert = unsafeSet key :: focus -> Record subrowTail -> Record subrow
tail = topRecord (RLProxy :: RLProxy rowlistTail) rowProxy
tail = topRecord (Proxy :: Proxy rowlistTail) rowProxy

bottomRecord _ rowProxy
= insert bottom tail
where
key = reflectSymbol (SProxy :: SProxy key)
key = reflectSymbol (Proxy :: Proxy key)
insert = unsafeSet key :: focus -> Record subrowTail -> Record subrow
tail = bottomRecord (RLProxy :: RLProxy rowlistTail) rowProxy
tail = bottomRecord (Proxy :: Proxy rowlistTail) rowProxy

instance boundedRecord
:: ( RL.RowToList row list
, BoundedRecord list row row
)
=> Bounded (Record row) where
top = topRecord (RLProxy :: RLProxy list) (RProxy :: RProxy row)
bottom = bottomRecord (RLProxy :: RLProxy list) (RProxy :: RProxy row)
top = topRecord (Proxy :: Proxy list) (Proxy :: Proxy row)
bottom = bottomRecord (Proxy :: Proxy list) (Proxy :: Proxy row)
4 changes: 4 additions & 0 deletions src/Data/CommutativeRing.purs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Data.Symbol (class IsSymbol)
import Data.Unit (Unit)
import Prim.Row as Row
import Prim.RowList as RL
import Type.Proxy (Proxy, Proxy2, Proxy3)

-- | The `CommutativeRing` class is for rings where multiplication is
-- | commutative.
Expand All @@ -26,6 +27,9 @@ instance commutativeRingNumber :: CommutativeRing Number
instance commutativeRingUnit :: CommutativeRing Unit
instance commutativeRingFn :: CommutativeRing b => CommutativeRing (a -> b)
instance commutativeRingRecord :: (RL.RowToList row list, CommutativeRingRecord list row row) => CommutativeRing (Record row)
instance commutativeRingProxy :: CommutativeRing (Proxy a)
instance commutativeRingProxy2 :: CommutativeRing (Proxy2 a)
instance commutativeRingProxy3 :: CommutativeRing (Proxy3 a)

-- | A class for records where all fields have `CommutativeRing` instances, used
-- | to implement the `CommutativeRing` instance for records.
Expand Down
22 changes: 16 additions & 6 deletions src/Data/Eq.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ module Data.Eq
) where

import Data.HeytingAlgebra ((&&))
import Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol)
import Data.Symbol (class IsSymbol, reflectSymbol)
import Data.Unit (Unit)
import Data.Void (Void)
import Prim.Row as Row
import Prim.RowList as RL
import Record.Unsafe (unsafeGet)
import Type.Data.RowList (RLProxy(..))
import Type.Proxy (Proxy(..), Proxy2, Proxy3)

-- | The `Eq` type class represents types which support decidable equality.
-- |
Expand Down Expand Up @@ -62,7 +62,16 @@ instance eqArray :: Eq a => Eq (Array a) where
eq = eqArrayImpl eq

instance eqRec :: (RL.RowToList row list, EqRecord list row) => Eq (Record row) where
eq = eqRecord (RLProxy :: RLProxy list)
eq = eqRecord (Proxy :: Proxy list)

instance eqProxy :: Eq (Proxy a) where
eq _ _ = true

instance eqProxy2 :: Eq (Proxy2 a) where
eq _ _ = true

instance eqProxy3 :: Eq (Proxy3 a) where
eq _ _ = true

foreign import eqBooleanImpl :: Boolean -> Boolean -> Boolean
foreign import eqIntImpl :: Int -> Int -> Boolean
Expand All @@ -84,8 +93,9 @@ notEq1 x y = (x `eq1` y) == false

-- | A class for records where all fields have `Eq` instances, used to implement
-- | the `Eq` instance for records.
class EqRecord :: RL.RowList Type -> Row Type -> Constraint
class EqRecord rowlist row where
eqRecord :: RLProxy rowlist -> Record row -> Record row -> Boolean
eqRecord :: forall rlproxy. rlproxy rowlist -> Record row -> Record row -> Boolean

instance eqRowNil :: EqRecord RL.Nil row where
eqRecord _ _ _ = true
Expand All @@ -99,6 +109,6 @@ instance eqRowCons
=> EqRecord (RL.Cons key focus rowlistTail) row where
eqRecord _ ra rb = (get ra == get rb) && tail
where
key = reflectSymbol (SProxy :: SProxy key)
key = reflectSymbol (Proxy :: Proxy key)
get = unsafeGet key :: Record row -> focus
tail = eqRecord (RLProxy :: RLProxy rowlistTail) ra rb
tail = eqRecord (Proxy :: Proxy rowlistTail) ra rb
4 changes: 4 additions & 0 deletions src/Data/Functor.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Data.Functor

import Data.Function (const, compose)
import Data.Unit (Unit, unit)
import Type.Proxy (Proxy(..))

-- | A `Functor` is a type constructor which supports a mapping operation
-- | `map`.
Expand Down Expand Up @@ -42,6 +43,9 @@ instance functorFn :: Functor ((->) r) where
instance functorArray :: Functor Array where
map = arrayMap

instance functorProxy :: Functor Proxy where
map _ _ = Proxy

foreign import arrayMap :: forall a b. (a -> b) -> Array a -> Array b

-- | The `void` function is used to ignore the type wrapped by a
Expand Down
Loading