elm / error-message-catalog

A catalog of broken Elm programs / data to improve error messages
BSD 3-Clause "New" or "Revised" License
174 stars 17 forks source link

Comparability of composite structures and an example in tuples #73

Open s-marashi opened 8 years ago

s-marashi commented 8 years ago

As I was playing around of comparability an equability concept, I found this weird example:

import Graphics.Element exposing (..)

f1 a = a
f2 a = a

main : Element
main = show <| (1,f1) > (1,f2)

which fails by this error:

(>) is expecting the left argument to be a:

( number, a -> a )

But the left argument is:

( number, a -> a )

Hint: Only ints, floats, chars, strings, lists, and tuples are comparable.

So I think here is a challenge for comparability concept, i.e. although in the compiler's hint is said tuples are comparable but this one is an exception so maybe comparability should be defined as "any composite data structure is comparable if and only if all of its members are comparable" then the mentioned tuple is not comparable at all and then compiler error should be:

(>) is expecting the left argument to be a:

comparable

Hint: Only ints, floats, chars, strings and composite structures whose members are comparable are counted as comparable.

or maybe something more declarative.

ckoster22 commented 8 years ago

I just stumbled across this same error in a less weird example. I began exploring a strategy for shuffling Lists (incomplete example) and goofed.

import Html exposing (text)
import Random exposing (generate, initialSeed, float, list, Seed)

shuffleList : List Int -> List Int
shuffleList list =
  let
    randomVals = List.map createFloatAndItemTuple list
    sortedListWithTuples = List.sortBy (\itemTuple -> fst itemTuple) randomVals
  in
    List.map (\itemTuple -> snd itemTuple) sortedListWithTuples

createFloatAndItemTuple num =
  let
    seed = initialSeed 12345
  in
    -- Comment out line 17 and uncomment line 18 to see error
    (,) 1.2 num
    --(,) (generate (float 0 100) seed) num

main =
  text "I compile!"

And the error I got:

TYPE MISMATCH The 2nd argument to function sortBy is causing a mismatch.

8| sortedListWithTuples = List.sortBy (\itemTuple -> fst itemTuple) randomVals Function sortBy is expecting the 2nd argument to be:

List ( ( Float, Seed ), a )

But it is:

List ( ( Float, Seed ), a )

Hint: Only ints, floats, chars, strings, lists, and tuples are comparable.

I assumed generate returned a Float, but it returned a (Float, Seed). The expected vs. actual in the error message being the same made it a little confusing to troubleshoot.