Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 483
Earth Engine Avatar asked Jul 04 '14 04:07

Earth Engine


1 Answers

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.

like image 66
John L Avatar answered Nov 16 '22 09:11

John L