Haskell polymorphic calls without complete type knowledge -


i'm studying haskell since little while, i'm newbie.

the following code understandable:

purstrln $ show [1] 

here can infer types (with defaults), , works well. following code works, too:

putstrln $ show [] 

even if can't infer list type.

if execute code ghci obtain following:

prelude> [] [] prelude> :t it :: [a] prelude>  

so type seems polymorphic. in case show called partially applied type.

the same behavior common other types, example data.map.empty, isn't list feature (or @ least seems it).

why , how works?

first of all, works in ghci. if try compile program ghc you'll type error:

test.hs:3:19:     ambiguous type variable `a0' in constraint:       (show a0) arising use of `show'     probable fix: add type signature fixes these type variable(s)     in second argument of `($)', namely `show []'     in expression: putstrln $ show []     in equation `main': main = putstrln $ show [] 

adding type signature makes error go away:

module main  main = putstrln $ show ([]::[int]) 

but why did work in ghci? answer extended type defaulting in ghci: type of a defaulted () (the unit type).

the motivation behaviour is bit tiresome user specify types when working in interpreter. vitus notes in comments, running ghci -wall (or adding :set -wall ~/.ghci) makes easier spot defaulting:

<interactive>:2:12:     warning: defaulting following constraint(s) type `()'                (show a0) arising use of `show'     in second argument of `($)', namely `show []'     in stmt of interactive ghci command: <- putstrln $ show [] 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -