Haskell Read (no instance) -


i haskell beginner , have weird question. until has been going great, , have been able use prelude read function normally. have declare it's type in order use it.

i have declare or similar in order use it.

let r = read::string-> int 

i have tried restarting ghci thinking have accidentally overloaded read whenever try using such

read "456" 

i following error

no instance (read a0) arising use of `read' type variable `a0' ambiguous possible fix: add type signature fixes these type variable(s) note: there several potential instances:   instance read () -- defined in `ghc.read'   instance (read a, read b) => read (a, b) -- defined in `ghc.read'   instance (read a, read b, read c) => read (a, b, c)     -- defined in `ghc.read'   ...plus 25 others in expression: read "456" in equation `it': = read "456" 

does have ideas causing , how fix it?

first, solve problem, specify result expect read in 1 of few ways:

first, specifying type of result of entire computation:

read "456" :: int 

or specifying type of function read:

(read :: string -> int) "456" 

to explain why happens, issue read polymorphic in result type. is, there many different read functions defined, , defined part of typeclass read. error gives explanation why:

possible fix: add type signature fixes these type variable(s) note: there several potential instances:   instance read () -- defined in `ghc.read'   instance (read a, read b) => read (a, b) -- defined in `ghc.read'   instance (read a, read b, read c) => read (a, b, c)     -- defined in `ghc.read'   ...plus 25 others 

the read typeclass defined many, many types, says there 28 instances defined in error. it's defined int, , integer, , double, , string, , countless others. , in order compiler know instance of typeclass use, needs able infer type of read specify. if give :: string -> int, compiler can figure out.

to understand how works, let's @ part of type class read:

class read   read :: string -> 

so given type a, instance of read a must defined in order function read return value of type a. int, there definition instance of read int, , indeed there in ghc-read. instance defines how read works. unfortunately, instance rather obtuse , relies on other functions , other such things, sake of completeness, here is:

instance read int   readprec     = readnumber convertint   readlistprec = readlistprecdefault   readlist     = readlistdefault 

but, important thing realize instance means read work int. shortly after, there's instance:

instance read double   readprec     = readnumber convertfrac   readlistprec = readlistprecdefault   readlist     = readlistdefault 

oh dear. read works int , double!

the haskell language ensures compiler will, usually, try infer unique type value or fail. there odd exceptions extensions language in ghc, have remember here haskell trying prevent shooting in foot. if don't specify somehow type you're expecting read, compiler infer any type, might not valid. wouldn't want code start reading in floating point doubles or arbitrary precision integers expected read in int, right?

in case, not specifying type merely caused compiler give up, couldn't definitively answer question asked to, is, "which instance of read use?" , haskell compilers do not guessing. 1 way prevent guessing use value in way unambiguous later, or define function uses read unambiguously. example, function can used in code avoid having type :: string -> int:

readint :: string -> int readint = read 

the compiler can figure out instance of read needs use there, , in code if do:

readint "456" 

the compiler know readint has type string -> int, , readint "456" has type int. unambiguous, haskell likes it.

addendum

the syntax in ghci bit different defining things, if using ghci exclusively play around haskell, i'd recommend switch loading .hs files , using :r command reload them. 1 problem ghci have define "let" start feel strange when start writing programs, top level definitions don't need that. problem monomorphism restriction which, frankly, kind of weird , quirky , it's not worth going detail here.

anyhow, syntax defining readint above in ghci simple, though there 2 ways (equivalent). know one, this:

let readint = read :: string -> int 

the other way define function , type separately, more akin how done in regular .hs file:

let readint :: string -> int; readint = read 

neither idiomatic haskell, that's because ghci imposes quirky limitations on writing code , multi-line entry strange. this:

:{ let readint :: string -> int;     readint = read :} 

and close idiomatic haskell definition. ghci correctly compile initial example if put in .hs file , use :load or specify file ghci ./somefile.hs.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -