Code Monkey home page Code Monkey logo

Comments (5)

paf31 avatar paf31 commented on July 30, 2024

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.

safareli avatar safareli commented on July 30, 2024

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.

hdgarrood avatar hdgarrood commented on July 30, 2024

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.

safareli avatar safareli commented on July 30, 2024

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.

hdgarrood avatar hdgarrood commented on July 30, 2024

Oh yeah of course, that certainly seems more plausible.

from purescript-control.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.