purescript / purescript-contravariant Goto Github PK
View Code? Open in Web Editor NEWContravariant functors
License: BSD 3-Clause "New" or "Revised" License
Contravariant functors
License: BSD 3-Clause "New" or "Revised" License
I think this could potentially be useful in some rare cases:
import Prelude
import Data.Functor.Contravariant (class Contravariant, cmap)
import Data.Divide (class Divide, divide)
import Data.Divisible (class Divisible, conquer)
import Data.Monoid (class Monoid)
import Data.Newtype (un)
import Data.Op (Op(..))
import Data.Profunctor.Strong (first)
import Data.Tuple (Tuple(..), snd)
import Unsafe.Coerce (unsafeCoerce)
data Divided_ f a b c = Divided_ (a -> Tuple b c) (f b) (Free f c)
data Divided (f :: Type -> Type) a
divided :: forall f a b c. Divided_ f a b c -> Divided f a
divided = unsafeCoerce
unDivided :: forall f a r. (forall b c. Divided_ f a b c -> r) -> Divided f a -> r
unDivided = unsafeCoerce
data Free f a
= Conquer
| Divide (Divided f a)
instance contravariantFree :: Contravariant (Free f) where
cmap _ Conquer = Conquer
cmap f (Divide d) = Divide (unDivided (\(Divided_ g x y) -> divided (Divided_ (g <<< f) x y)) d)
instance divideFree :: Contravariant f => Divide (Free f) where
divide f x y =
case x of
Conquer -> cmap (snd <<< f) y
Divide d -> unDivided (\(Divided_ g a b) ->
case divide id b y of
c -> Divide (divided (Divided_ (assoc <<< first g <<< f) a c))) d
where
assoc :: forall a b c. Tuple (Tuple a b) c -> Tuple a (Tuple b c)
assoc (Tuple (Tuple x y) z) = Tuple x (Tuple y z)
instance divisibleFree :: Contravariant f => Divisible (Free f) where
conquer = Conquer
interpretFree :: forall f g. Divisible g => (f ~> g) -> Free f ~> g
interpretFree _ Conquer = conquer
interpretFree f (Divide d) = unDivided (\(Divided_ g x y) -> divide g (f x) (interpretFree f y)) d
foldFree :: forall f m a. Monoid m => (forall b. f b -> b -> m) -> Free f a -> a -> m
foldFree f = un Op <<< interpretFree (Op <<< f)
Any reason why Predicate
doesn't have any instances except for Contravariant
?
Hello!
The new version of the compiler is having a bad time with the self import that has been fixed in #21 - would it be possible to do a new release with this fix in?
Thanks!
Dan
-- | An adaptor allowing `>$<` to map over the inputs of a comparison function.
newtype Comparison o a = Comparison (a -> a -> o)
derive instance newtypeComparison :: Newtype (Comparison o a) _
instance contravariantComparison :: Contravariant (Comparison o) where
cmap f (Comparison g) = Comparison (g `on` f)
instance semigroupComparison :: Semigroup o => Semigroup (Comparison o a) where
append (Comparison p) (Comparison q) = Comparison (p <> q)
instance monoidComparison :: Monoid o => Monoid (Comparison o a) where
mempty = Comparison (\_ _ -> mempty) -- mempty of `Ordering` is `EQ`
-- | The default comparison for any values with an `Ord` instance.
defaultComparison :: forall a. Ord a => Comparison Ordering a
defaultComparison = Comparison compare
Should be fairly simple to implement lawfully.
The following function could be defined
cflap >@<
cvoid
cvoidRight >$
cvoidLeft $<
Shall I write a PR?
Hi,
I am using purescript-contravariant with psc version 0.6.8 and it gives me:
Error at bower_components/purescript-contravariant/src/Data/Comparison.purs line 16, column 1 - line 19, column 1:
Error in declaration semigroupComparison
No instance found for Prelude.Semigroup (a6536 -> a6536 -> Prelude.Ordering)
Same as #32
For example, if >|<
, then we can write
choser = case _ of
x | x < 0 then in1 x
| x > 0 then in2 x
| otherwise then in3 x
f4 = choser >$< f1 >|< f2 >|< f3
This is not the reverse of <|>
so there may be a more appropriate alias.
In haskell contravariant we can find a new operator >*<
which is an alias for divided
. It allows us to nicely capture duality of Divide
and Apply
- we can write:
nonNegative = Predicate (_ >= 0)
q = (_.x &&& _.y &&& _.z) >$< nonNegative >*< nonNegative >*< nonNegative
Should I provide a PR with such addition to the library?
Not cmap
, I think.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.