can Haskell compiler give warning on functions that used "undefined"
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)
If You are using hlint
, You can add this rule to your .hlint.yaml
:
- warning: {lhs: "undefined", rhs: undefined}
for warnings like:
Use alternative. Replace with: undefined
or You can add your own description with name
attribute, for example:
- warning: {lhs: "undefined", rhs: undefined, name: Realisation is required}
Realisation is required. Replace with: undefined
You can use automatic refactoring safely, because of it just replace undefined
to undefined
and does not broke anything.
Also You can ignore this rule (with name
attribute) for specific function:
{-# ANN functionName "HLint: ignore Realisation is required" #-}
functionName :: Int -> Int
functionName x = undefined
or for module: {-# ANN module "HLint: ignore Realisation is required" #-}
Some alternative preludes export their own version of undefined
with warning added so you will see warning during compilation if you left undefined
.
- http://hackage.haskell.org/package/relude-0.2.0/docs/Relude-Debug.html#v:undefined