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

0.19 Overrestrictive Type Inference? #1858

Open
rue opened this issue Nov 23, 2018 · 1 comment
Open

0.19 Overrestrictive Type Inference? #1858

rue opened this issue Nov 23, 2018 · 1 comment
Labels

Comments

@rue
Copy link

rue commented Nov 23, 2018

The following code (from An Outsider's Guide to Statically Typed Functional Programming, @marick) produces a type mismatch error in 0.19:

module SumTypes.WholeStorySolution exposing (..)

type Silly a b
  = AsA a
  | AsB b
  | AlsoAsA a

  | AsInt Int

  | AsFloatString Float String
  | AsBoth a b
  | AsIntB Int b

  | AsMaybe (Maybe b)
  | AsMaybeInt (Maybe Int)
  | DeeplySilly (Silly a a) (Silly Int b)
  | NoArg

bVals : Silly a b -> Maybe b
bVals silly =
  case silly of 
    AsB b -> Just b
    AsBoth _ b -> Just b
    AsIntB _ b -> Just b
    AsMaybe (Just b) -> Just b  -- could just be AsMaybe maybe -> maybe
    DeeplySilly _ nested -> bVals nested            {- <--- Problem here -}
_ -> Nothing

Error:

Detected errors in 1 module.                                         
-- TYPE MISMATCH --------------------------------------------- src/SumTypes/WholeStorySolution.elm

The 1st argument to `bVals` is not what I expect:

171|             bVals nested
                   ^^^^^^^^^^
This `nested` value is a:

    Silly Int b

But `bVals` needs the 1st argument to be:

    Silly a b
[]

Additional possibly interesting info:

> bVals
<function> : Silly Int a -> Maybe a      -- The inferred type (without annotation)

> AsFloatString 4.5 "hi" |> bVals
Nothing : Maybe b                                -- …Why is this not an error, it doesn't conform to the inferred type?

It seems like Silly a b should allow Silly Int b. Is this intended behavior?

@marick
Copy link

marick commented Nov 23, 2018

The book was written against 0.18.

I translated the code into PureScript. It works there as it did in 0.18.

@evancz evancz added the types label Jul 28, 2019
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

3 participants