3 ms·
What I find curious about the error message is that it tries to tell you what is the cause of the error, but it does not actually show you source-code that caus
by galaxyLogic 5y ago
What I find curious about the error message is that it tries to tell you what is the cause of the error, but it does not actually show you source-code that causes it.
Wouldn't it be much easier to understand what is causing the error if I simply saw a short section of the source-code where I could FIX the error, by modifying something in the shown section?
What is the line-number where the error occurs? Show me the code, and where it is in relation to rest of the code.
- josephcsible 5y agoGHC does do those things when you're compiling code in files. The only reason you aren't seeing it in this article is that these errors came from entering one expression at a time into the REPL.
- galaxyLogic 5y agoI see. I work in JavaScript so I don't really use a REPL. Often I do "evaluate expressions" in my IDE, I guess that is somewhat similar. I wonder if a "real REPL" would be useful for JavaScript. Or is it something that especially helps when working with languages like Haskell?
- cole-k 5y agoYeah, that's definitely a confusing part of GHCi. If you see an error message like the one in the post Prelude> 1+[2,3] <interactive>:1:1: error: • Non type-variable argument in the constraint: Num [a] (Use FlexibleContexts to permit this) • When checking the inferred type it :: forall a. (Num a, Num [a]) => [a] The "line-number" is found on this line <interactive>:1:1: error: Since it's in a REPL, it is referring to the first line you entered. The more confusing part is that the important error is found here • When checking the inferred type it :: forall a. (Num a, Num [a]) => [a] where it is assumed that you know all REPL statements that don't bind to a variable are bound to a variable named "it". So the error is really saying that the whole expression 1+[2,3] has that inferred type, and that type is bad. If you load your example into a file and run GHC on it, it will produce a still confusing error message, but it will at least show you where the problems are error-message.hs:3:11: error: • No instance for (Num [a0]) arising from a use of ‘+’ • In the expression: 1 + [1, 2] In an equation for ‘badType’: badType = 1 + [1, 2] | 3 | badType = 1 + [1,2] | ^^^^^^^^^ (I've omitted another, much longer error message it spewed out which is similarly confusing)
- jmaker 5y agoIn this case the code is just above in the REPL. If you compile a source file, the exact spot is always pointed at by the compiler.
- Tarean 5y agoSome advanced Haskell features make this hard because constraints like 'a is a number' are solved separately. For instance data Tag a where IsInt :: Tag Int IsDouble :: Tag Double someNum :: Num a => a someNum = 3 * 4 + 5 pickNum :: Tag a -> a pickNum IsInt = someNum pickNum IsDouble = someNum * 2.5 Constraints can be delayed and changed by pattern matching, so the origin might not be obvious. There are ways around this, but they require tracking why values are set and to propagate this information around. Haskell has Turing complete type level functions so you have to figure out how they propagate this dependency information. GHC handles some easy cases, if you compiler in a file it does point to `+`, but it can get hairy. Also, currying can make it harder to say 'you swapped to arguments'