Re: [Haskell-cafe] What's the problem with iota's type signature?

Yeah, I went back and tried double again, though I'd swear I got the dang thing to compile (and run) w/o errors.
I guess I meant Num. So Num is a class and Int and Integer are types? What are the other classes? Docs?
Unification, for me, is pattern matching ala Prolog. What's the meaning in Haskell? Docs?
I've been digging into this stuff for months and it's still tripping me up. Very frustrating.
Thanks.
Michael
--- On Thu, 5/28/09, wren ng thornton
Still exploring monads. I don't understand why the type signature for double is OK,
It isn't. The |a| and |b| variables must unify: Prelude> :t \x -> Just (x+x) \x -> Just (x+x) :: (Num a) => a -> Maybe a Prelude> :t (\x -> Just (x+x)) :: Num a => a -> Maybe b <interactive>:1:13: Couldn't match expected type `b' against inferred type `a' `b' is a rigid type variable bound by the polymorphic type `forall a b. (Num a) => a -> Maybe b' at <interactive>:1:0 `a' is a rigid type variable bound by the polymorphic type `forall a b. (Num a) => a -> Maybe b' at <interactive>:1:0 In the first argument of `Just', namely `(x + x)' In the expression: Just (x + x)
but not the one for iota.
Again the |a| and |b| must unify. Also, Int isn't a type class (did you mean Num?) Also, the use of (..) requires the Enum class. Prelude> :t \n -> [1..n] \n -> [1..n] :: (Enum t, Num t) => t -> [t] Prelude> [3,4,5] >>= (\n -> [1..n]) [1,2,3,1,2,3,4,1,2,3,4,5] -- Live well, ~wren _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

michael rice
I've been digging into this stuff for months and it's still tripping me up.
For exploration use GHCi. It can tell you the type of thing you have written. It has command to tell you type of thing, the ":t". See here: Prelude> let double x = Just (x + x) Prelude> :t double double :: (Num a) => a -> Maybe a Prelude> let iota n = [1..n] Prelude> :t iota iota :: (Num t, Enum t) => t -> [t] Prelude> [3,4,5] >>= iota [1,2,3,1,2,3,4,1,2,3,4,5] You don't have to guess then, Haskell compiler can do the guessing for you. It is called type inference. -- Gracjan

On Thu, May 28, 2009 at 5:19 PM, Gracjan Polak
You don't have to guess then, Haskell compiler can do the guessing for you.
It isn't guess, Haskell compiler (like GHC) gets these types by (type) inference, as you said :-) lee
It is called type inference.

michael rice wrote:
Yeah, I went back and tried double again, though I'd swear I got the dang thing to compile (and run) w/o errors.
I guess I meant Num. So Num is a class and Int and Integer are types? What are the other classes? Docs?
Unification, for me, is pattern matching ala Prolog. What's the meaning in Haskell? Docs?
I've been digging into this stuff for months and it's still tripping me up. Very frustrating.
As others have mentioned, using GHCi can give you a lot of information. GHCi commands begin with a colon followed by the command (kinda like vi), whereas anything not beginning with a colon is treated as a Haskell expression to be evaluated. The :? command will list all the GHCi commands, which is a good place to start. The :type command takes a Haskell expression and prints the type inferred for it. You can also use the command ":set +t" which will make GHCi print the type after evaluating each expression; you may want to use this until you get the hang of things. The :info command takes a whitespace separated list of identifiers and will print their type and other information (e.g. what module it comes from, what type class a function belongs to, all the functions that are members of a type class, fixity of operators,...) As far as type classes and the like, there are a bunch. Hoogle[1] is an excellent API search engine for looking up this kind of stuff. Just enter the name of a function, type class, data type, or enter a type signature. The results give links to the online Haddock documentation (either GHC official docs, or Hackage docs). Unification is used when doing type inference. It's the same unification as in Prolog, only at the type level. Because of the way inference works, it happens that the |a| and |b| type variables must be unified for both of the functions you gave. A whole lot more can be said here, though I'm not sure how much you care :) The Wikipedia page on Hindley--Milner[2] gives a good starting point, though Haskell's type inference has more bells and whistles than the basic HM algorithm. [1] http://haskell.org/hoogle/ [2] http://en.wikipedia.org/wiki/Hindley-Milner -- Live well, ~wren
participants (4)
-
Gracjan Polak
-
Lee Duhem
-
michael rice
-
wren ng thornton