What kinds of type errors can Haskell catch at compile time that Java cannot? -
i'm starting learn haskell , keep seeing references powerful type system. see many instances in inference more powerful javas, implication can catch more errors @ compile time because of superior type system. so, i'm wondering if possible explain types of errors haskell can catch @ compile time java cannot.
saying haskell's type system can catch more errors java's little bit misleading. let's unpack little bit.
statically typed
java , haskell both statically typed languages. mean type of given expression in language known @ compile time. has number of advantages, both java , haskell, namely allows compiler check expressions "sane", reasonable definition of sane.
yes, java allows "mixed type" expressions, "abc" + 2
, may argue unsafe or bad, subjective choice. in end feature java language offers, better or worse.
immutability
to see how haskell code argued less error prone java (or c, c++, etc.) code, must consider type system respect immutability of language. in pure (normal) haskell code, there no side effects. say, no value in program, once created, may ever change. when compute creating new result old result, don't modify old value. this, turns out, has really convenient consequences safety perspective. when write code, can sure nothing else anywhere in program going effect our function. side effects, turns out, cause of many programming errors. example shared pointer in c freed in 1 function , accessed in another, causing crash. or variable set null in java,
string foo = "bar"; foo = null; char c = foo.charat(0); # error!
this not happen in normal haskell code, because foo
once defined, can not change. means can not set null
.
enter type system
now, wondering how type system plays of this, asked after all. well, nice immutability is, turns out there little interesting work can without mutation. reading file? mutation. writing disk? mutation. talking web server? mutation. do? in order solve issue, haskell uses type system encapsulate mutation in type, called io monad. instance read file, function may used,
readfile :: filepath -> io string
the io monad
notice type of result is not string
, io string
. means, in laymans terms, result introduces io (side effects) program. in formed program io take place inside io monad, allowing see clearly, side effects can occur. property enforced type system. further io a
types can produce results, side effects, inside main
function of program. have neatly , nicely isolated off dangerous side effects controlled part of program. when result of io string
, could happen, @ least can't happen anywhere, in main
function , result of io a
types.
now clear, can create io a
values anywhere in code. can manipulate them outside main
function, none of manipulation take place until result demanded in body of main
function. instance,
strreplicate :: io string strreplicate = readfile "somefile doesn't exist" >>= return . concat . replicate 2
this function reads input file, duplicates input , appends duplicated input onto end of original input. if file had characters abc
create string
contents abcabc
. can call function anywhere in code, haskell try read file when expression found in main
function, because instance of io
monad
. so,
main :: io () main = strreplicate >>= putstrln
this surely fail, file requested doesn't exist, fail here. have worry side effects, not everywhere in code, in many other languages.
there lot more both io , monads in general have covered here, beyond scope of question.
type inference
now there 1 more aspect this. type inference
haskell uses very advanced type inference system, allows write code that statically typed without having write type annotation, such string foo
in java. ghc can infer type of almost expression, very complex ones.
what means our safety discussion everywhere instance of io a
used in program, type system make sure can't used produce unexpected side effect. can't cast string
, , result out where/when ever want. you must explicitly introduce side effect in main
function.
the safety of static typing ease of dynamic typing
the type inference system has other nice properties well. people enjoy scripting languages because don't have write boilerplate types have in java or c. because scripting languages dynamically typed or type of expression computed expression being run interpreter. makes these languages arguably more prone errors, because won't know if have bad expression until run code. example, might in python.
def foo(x,y): return x + y
the problem x
, y
can anything. fine,
foo(1,2) -> 3
but cause error,
foo(1,[]) -> error
and have way of checking invalid, until run.
it important understand all statically type languages not have problem, java included. haskell not safer java in sense. haskell , java both keep safe type of error, in haskell don't have write types in order safe, type system can infer types. in general, considered practice annotate types functions in haskell, though don't have to. in body of function however, have specify types (there strange edge cases will).
conclusion
hopefully helps illuminate how haskell keeps safe. , in regard java, might in java have work against type system write code, in haskell type system works you.
Comments
Post a Comment