Identifier "Otherwise" in guarded equation can crash a program
In GHC.Base the identifier otherwise
is defined as the value "True" and it helps to make guards more readable.\\
The identifier otherwise
is not secure.\\
We can change his property easily and trick the compiler.\\
As written in GHC Base, otherwise = True
is perfectible.\\
The problem here is to protect the "otherwise" identifier from uncontrolled access.\\
See the code below and the result of the compilation as well as the output.\\
import qualified Prelude as P
otherwise :: P.Bool
otherwise = P.False
fun :: (P.Num a, P.Ord a) => a -> [P.Char]
fun x | x P.< 0 = "down"
| otherwise = "up"
main :: P.IO ()
main = P.print (fun 7)
c:\Sourcehs>ghc -Wall otherwise.hs
[1 of 1] Compiling Main ( otherwise.hs, otherwise.o )
otherwise.hs:7:1: warning: [-Wincomplete-patterns]
Pattern match(es) are non-exhaustive
In an equation for `fun': Patterns not matched: _
otherwise.hs:11:17: warning: [-Wtype-defaults]
* Defaulting the following constraints to type `P.Integer'
(P.Num a0) arising from a use of `fun' at otherwise.hs:11:17-21
(P.Ord a0) arising from a use of `fun' at otherwise.hs:11:17-21
* In the first argument of `P.print', namely `(fun 7)'
In the expression: P.print (fun 7)
In an equation for `main': main = P.print (fun 7)
Linking otherwise.exe ...
c:\Sourcehs>otherwise
otherwise: otherwise.hs:(7,1)-(8,27): Non-exhaustive patterns in function fun
I propose three solutions:
1- Delete otherwise
from GHC.Base and use only TRUE.\\
2- Add otherwise
as a reserved word or keyword.\\
3- Create protected objects in Haskell.
Trac metadata
Trac field | Value |
---|---|
Version | 8.0.2 |
Type | FeatureRequest |
TypeOfFailure | OtherFailure |
Priority | normal |
Resolution | Unresolved |
Component | Compiler |
Test case | |
Differential revisions | |
BlockedBy | |
Related | |
Blocking | |
CC | |
Operating system | |
Architecture |