Earth Engine
Earth Engine

Reputation: 10466

How to implement "symmetric non-strict or" in Haskell

I want to define a function that have the following properties

symmetricLazyOr :: Bool -> Bool -> Bool
symmetricLazyOr True _|_ === True
symmetricLazyOr _|_ True === True

And otherwise it works like the normal or.

Is it even possible in Haskell?

UPDATE

This question is focus on semantic rather than implementation detail. Intuitively, or shall be symmetric, which means or a b === or b a for all given a and b. However, this is not true in Haskell since or _|_ True === _|_ whilst or True _|_ === True.

Upvotes: 7

Views: 200

Answers (1)

John L
John L

Reputation: 28097

In other words, you're looking for a function that, given two arguments, attempts to evaluate them both and is true if either argument is true? And in particular, a True result will be returned so long as at least one argument is True and not bottom?

Assuming that's correct, this is possible, but not purely. In order to implement it, you need to race two threads to evaluate each of the branches. The unamb package has some functions for dealing with cases like this (including the parallel-or function por). Another option is lvish, which should also work in this case as I understand it.

Upvotes: 14

Related Questions