Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

guard may evaluate to false, but _ case present #271

Closed
mabre opened this issue Apr 19, 2016 · 4 comments
Closed

guard may evaluate to false, but _ case present #271

mabre opened this issue Apr 19, 2016 · 4 comments
Assignees
Labels

Comments

@mabre
Copy link

mabre commented Apr 19, 2016

The following code runs fine with ghc (printing Nothing):

module WhereGuardBug where

readDriveShare :: [Char] -> Maybe ([Char], [Char])
readDriveShare (x:xs) | x == '/' = Just (x:a,b)
    where (a,b) = (['f'],['g'])
readDriveShare _ = Nothing

main = print $ readDriveShare ('f':'i':'l':'e':[])

When compiling with frege, I get

W whereGuardBug.hs:4: guard (== x '/') may evaluate to false.

and at runtime

Exception in thread "main" frege.runtime.NoMatch: readDriveShare at line 4 no match for value frege.prelude.PreludeBase$TTuple2@70177ecd
        at WhereGuardBug.readDriveShare(WhereGuardBug.java:90)
        at WhereGuardBug.lambda$null$2(WhereGuardBug.java:135)
        at frege.run8.Thunk.call(Thunk.java:231)
        at WhereGuardBug.lambda$null$0(WhereGuardBug.java:127)
        at frege.run8.Thunk.call(Thunk.java:231)
        at WhereGuardBug.lambda$static$3(WhereGuardBug.java:153)
        at frege.run8.Thunk.call(Thunk.java:231)
        at WhereGuardBug.main(WhereGuardBug.java:162)
@Ingo60 Ingo60 added the bug label Apr 19, 2016
@Ingo60 Ingo60 self-assigned this Apr 19, 2016
@Ingo60
Copy link
Member

Ingo60 commented Apr 19, 2016

This is yet another silly bug arising from the pattern binding in the where clause (a construct I never use, which may explain why I didn't recognize this before).

This gets translated into a something like

readDriveShare (x:xs) = case (['f'], ['g']) of (a,b) | x == '/' = Just (x:a,b)

but this wrong here, because the guard must not slip inside.

The workaround is to write

 where
      a = ['f']
      b = ['g']

Ingo60 added a commit that referenced this issue Apr 19, 2016
@mabre
Copy link
Author

mabre commented Apr 19, 2016

The workaround is to write

 where
     a = ['f']
     b = ['g']

That doesn’t work in my case, because (a,b) is actually the result of a function, so I used let (a,b) = (['f'],['g']) in … instead.

@Ingo60
Copy link
Member

Ingo60 commented Apr 19, 2016

In that case you can write

foo pattern | guard = case something of (a,b) -> .....

But since it is fixed anyway, this is no issue anymore.

@Ingo60
Copy link
Member

Ingo60 commented Apr 21, 2016

Fixed.

@Ingo60 Ingo60 closed this as completed Apr 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants