[Haskell-cafe] Specify compile error

Adam Smith unfactorable.polynomial at gmail.com
Thu May 3 18:21:57 CEST 2012


Nope - because at compile time, there's no way to know whether
createB's argument is a Safe or an Unsafe. That information only
exists at run time. Consider the following functions.

f :: Int -> A
f x = if x < 0 then Unsafe x else Safe x

g :: IO B
g = do x <- getLine
       return $ createB $ f (read x)

Here, read x will convert the input (entered at runtime) to an Int
(assuming it can - failure leads to a runtime exception), and then f
will convert the resulting Int to an A, which is then passed to
createB. But there's no way of the compiler knowing whether that A
will be a Safe or an Unsafe A, since that depends on the value entered
at runtime.

If you want the compiler to typecheck two things differently, they
need to be of different types. If you give a bit more context about
what you're trying to do, someone might be able to suggest a safer way
of doing what it.

Cheers,
Adam

On 3 May 2012 11:36, Ismael Figueroa Palet <ifigueroap at gmail.com> wrote:
>
> Hi, I'm writing a program like this:
>
> data B = B Int
> data A = Safe Int | Unsafe Int
>
> createB :: A -> B
> createB (Safe i) = B i
> createB (Unsafe i) = error "This is not allowed"
>
> Unfortunately, the situation when createB is called with an Unsafe value is only checked at runtime.
> If I omit the second case, it is not an error to not be exhaustive :-(
>
> Is there a way to make it a compile time error??
>
> Thanks!
> --
> Ismael
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>



More information about the Haskell-Cafe mailing list