Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can Haskell compiler give warning on functions that used "undefined"

Tags:

haskell

If I define a function using "undefined" like this, it type-checks.

add2 :: Int -> Int -> Int
add2 = undefined

Is it possible to detect if any function is using "undefined" in the function definition, and turn it into a warning?

It would be useful that in development-phase, I can use "undefined" to check if the type signature is correct before I implement the function. And then on production I can have some way to catch the mistakes that I forgot to give implementation for functions that had "undefined".

like image 525
Leo Zhang Avatar asked Aug 28 '18 17:08

Leo Zhang


1 Answers

A good solution is to use typed holes instead of undefined along with the -fdefer-typed-holes compiler flag to make them warnings instead of errors (which is generally more useful, anyway). With this flag enabled, you would write your example like this, instead:

add2 :: Int -> Int -> Int
add2 = _

…which produces the following warning:

warning: [-Wtyped-holes]
    • Found hole: _ :: Int -> Int -> Int
    • In the expression: _
      In an equation for ‘add2’: add2 = _
    • Relevant bindings include
        add2 :: Int -> Int -> Int

Modern GHCs will even include a list of possible substitutions for the hole in the warning:

      Valid substitutions include
        add2 :: Int -> Int -> Int
        (+) :: forall a. Num a => a -> a -> a
          (imported from ‘Prelude’ (and originally defined in ‘GHC.Num’))
        (*) :: forall a. Num a => a -> a -> a
          (imported from ‘Prelude’ (and originally defined in ‘GHC.Num’))
        (^) :: forall a b. (Num a, Integral b) => a -> b -> a
          (imported from ‘Prelude’ (and originally defined in ‘GHC.Real’))
        (-) :: forall a. Num a => a -> a -> a
          (imported from ‘Prelude’ (and originally defined in ‘GHC.Num’))
        seq :: forall a b. a -> b -> b
          (imported from ‘Prelude’ (and originally defined in ‘GHC.Prim’))
        (Some substitutions suppressed; use -fmax-valid-substitutions=N or -fno-max-valid-substitutions)
like image 50
Alexis King Avatar answered Sep 20 '22 16:09

Alexis King