Comments (5)
Sounds useful, but we should consider using MonadZero
or MonadPlus
. What can you prove about this function given just Alternative
and Monad
?
from purescript-control.
Technically we need Plus
for empty
, Chain
for >>=
and Applicative
for pure
:: ∀ m a. Plus m => Chain m => Applicative m => (a -> Boolean) -> m a -> m a
But if we should use one from MonadZero
or MonadPlus
I guess MonadZero
is just enough.
About proving something don't know, there are cople filter and filterMap related laws in Filterable. (btw filterMap could be implemented this way too)
I think these are correct assumptions:
(filter g) <<< (filter f) = filter (\x -> f x || g x) -- composition?
(filter f) <<< (filter f) = filter f -- ???
filter (const true) = id -- identity?
from purescript-control.
You can prove things with just Monad
and Alternative
; see for example #51.
filter (const true) = identity
is fairly easy to establish: we have:
filter (const true) m
= m >>= \x -> if const true x then pure x else empty
= m >>= \x -> pure x
= m >>= pure
= m
I don't think filter g <<< filter f = filter (\x -> f x || g x)
holds though. Consider:
filter isOdd (filter isEven (pure 1))
= filter isOdd (pure 1 >>= \x -> if isEven x then pure x else empty)
= filter isOdd (if isEven 1 then pure 1 else empty)
= filter isOdd empty
= empty >>= if isOdd x then pure x else empty
= empty
filter (\x -> isOdd x || isEven x) (pure 1)
= pure 1 >>= \x -> if (isOdd x || isEven x) then pure x else empty
= if (isOdd 1 || isEven 1) then pure 1 else empty
= pure `
However, I'm struggling to think of a concrete type for which this would be useful. For sequence types like List or Array which have a concat-mappy Bind instance, this will do the same as your standard filterA
, but with terrible performance. For effect types like Effect
or Aff
, it's essentially an assertion that the result satisfies the predicate supplied, except that you'll get a useless error message if it doesn't. If that's the kind of thing you want, it would be better to use MonadThrow
, I think.
I'm going to close this for now for this reason plus the fact that it hasn't been touched in a long time, but please feel free to comment if I'm missing something.
from purescript-control.
This might be correct:
-(filter g) <<< (filter f) = filter (\x -> f x || g x)
+(filter g) <<< (filter f) = filter (\x -> f x && g x)
(not posting with intent to reopen this issue tho)
from purescript-control.
Oh yeah of course, that certainly seems more plausible.
from purescript-control.
Related Issues (20)
- Add (<$$>) HOT 2
- $> breaks operator symmetry HOT 28
- Include a class for `hoist`? HOT 10
- Unknown module Data.Functor Control.Category, Data.Unit, Prelude HOT 4
- Annihilation law of alternative has incorrect order? HOT 1
- Add MonadOr? HOT 2
- laws for Lazy HOT 2
- Use fundeps for an intermediate representation in `defer` HOT 8
- Definition of `fix` lacks sharing HOT 1
- MonadZero and MonadPlus seem redundant? HOT 10
- purescript-control#^3.0.0 vs purescript-control#^4.0.0 HOT 1
- class Lazy1 f where defer1 :: forall a. (Unit -> f a) -> f a HOT 10
- Lazy instance for `Effect`
- Deprecate MonadZero HOT 21
- The distributivity law for Alternative rules out effectful instances HOT 28
- Remove deprecated MonadZero instance (v0.15.0 PS)
- Update class hierarchy to remove mention of MonadZero
- Consider updating misleading Alt documentation HOT 2
- Proposal: Make <|> right associative HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from purescript-control.