[Haskell-cafe] Implicit Multi Threading in Switch Case

Jonas Scholl anselm.scholl at tu-harburg.de
Tue Jun 20 10:07:42 UTC 2017

You have to keep in mind that you can only pattern match against a
single thing at once. If you match at multiple things like you are
doing, this is just syntactic sugar for the following:

test = case (infi, stop) of
    (a, b) -> case a of
        Nothing -> case b of
            Just _ -> 1
            _ -> 2
        _ -> 2

The Haskell report states this in section 3.17.2 ("Pattern matching
proceeds from left to right, and outside to inside") resp. section 3.17.3.

To match with stop first, you either need to put it left of infi, or use
multiple case statements. However, if you do not know, which of stop and
infi will run forever (or throw an exception or behave like _|_ in any
other way), you are stuck, because now you need to pick one to evaluate
and if it is the wrong one, there is nothing you can do.

If you want to leave the sane world, you could try to see if one of the
arguments is already evaluated. However, your code would then be neither
portable nor pure anymore. GHC tags pointers to evaluated values, so you
can actually see if a value is evaluated without entering (evaluating)
it - but there can be false negatives, not every evaluated closure is
guaranteed to have the tag bits set and if neither of infi and stop has
them set, you are back at square one.

On 06/20/2017 11:28 AM, Yotam Ohad wrote:
> Hi,
> After reading "Push-Pull Functional Reactive Programming
> <https://pdfs.semanticscholar.org/fb7a/879d639641341e025197b40afad9e21f0ce5.pdf>"
> I had an idea about deciding which, of two events, comes first. Instead
> of using forkIO, I tried something like the following:
> infi :: Maybe Int
> infi = infi
> stop :: Maybe Int
> stop = Nothing
> test :: Int
> test = case (infi, stop) of
>     (Nothing, Just _) -> 1
>     (_, _) -> 2
> Here, infi is an action that never ends, and stop a function that ends
> immediately. I thought that the compiler would see that stop evaluates
> immediately to Nothing and thus will return 2, but it tries to evaluate
> infi and get stuck.
> I think it happens because I am using a tuple to hold both values (but
> not really sure about it). Do you know a way to make this arrangement work?
> Yotam
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20170620/d2e3ab03/attachment.sig>

More information about the Haskell-Cafe mailing list