Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"No operation" haskell

If I remember correctly from school, there's a function or keyword that is used for "not yet implemented" but the code compiles. I've tried to search for it, but can't find. Any one know what I'm looking for?

is's something like

isDivisor :: Integer -> Integer -> Bool
isDivisor x y = None
--isDivisor x y = (rem x y) == 0
like image 337
Viktor Mellgren Avatar asked May 22 '13 13:05

Viktor Mellgren


1 Answers

What you're thinking of is called bottom

bottom isn't just for showing something isn't implemented, it's meant to represent a computation which causes our program to fail.

For example we can actually define undefined ourselves as an infinite loop

undefined = let x = x in x
undefined = undefined

So really what we're doing is just putting in a value undefined :: a which will cause or program to crash or loop forever, but never evaluating it.

Therefore if you have some big and complex function that you don't know how to implement you could just do this

foo :: Bar -> Baz -> Quux
foo bar baz = foo bar baz

Since this typechecks, it'll compile and we can test other parts of our program.

However since it's pretty unhelpful to have an infinite loop when you accidentally run that part of the program, GHC and others implement undefined as a differently. They have them crash the program and emit an error message, eg:

-- In GHC
error msg = throw (ErrorCall s)
undefined = error "Prelude.undefined"

So to leave a function undefined with better debugging capabilities

foo bar baz = undefined
foo bar baz = error ("Tried to evaluate foo with" ++ show bar ++ show baz)

If you're finding the concept of bottom confusing, hammar posted a great answer

like image 98
Daniel Gratzer Avatar answered Nov 16 '22 19:11

Daniel Gratzer