Re: [Haskell-cafe] Function Types

f x = x x :: a f x :: b therefore f :: a -> b x = a and x = b therefore a = b therefore f :: a -> a Simple mappings are easy to work out. It's the more detailed stuff I'm not sure about. f g x y = g x (y x) Cheers, Paul At 03:15 23/10/2007, you wrote:
On 10/22/07, PR Stanley <mailto:prstanley@ntlworld.comprstanley@ntlworld.com> wrote: Hi What are the rules for calculating function types? Is there a set procedure ? Thanks, Paul
There must be a set procedure, since otherwise the compiler could not function! =)
Seriously, though, I'm not exactly sure what you're asking for. Could you maybe provide a few examples of the kind of thing you're asking about?
-Brent

On 10/22/07, PR Stanley
f x = x x :: a f x :: b therefore f :: a -> b x = a and x = b therefore a = b therefore f :: a -> a Simple mappings are easy to work out. It's the more detailed stuff I'm not sure about. f g x y = g x (y x)
Benjamin Pierce's _Types and Programming Languages_ has some good chapters on type inference. Cheers, Tim -- Tim Chevalier * catamorphism.org * Often in error, never in doubt "Ever wonder why 'bus error core dump' is the standard C program crap out? Because C freely hands out random pointers to anyone that asks. Slut." -- Olin Shivers

On Oct 22, 2007, at 22:41 , PR Stanley wrote:
f x = x x :: a f x :: b therefore f :: a -> b x = a and x = b therefore a = b therefore f :: a -> a Simple mappings are easy to work out. It's the more detailed stuff I'm not sure about. f g x y = g x (y x)
I think you're looking for Hindley-Milner type inference. (GHC actually uses a more complex system known as System Fc, but probably want to start with H-M.) _Types and Programming Languages_ is a good place to start if you want to learn about the H-M family. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

PR Stanley:
f x = x x :: a f x :: b therefore f :: a -> b x = a and x = b therefore a = b therefore f :: a -> a Simple mappings are easy to work out. It's the more detailed stuff I'm not sure about.
You've got the right idea. Type inference involves a process called unification. This is essentially what you are doing when you say "x = a and x = b therefore a = b". (But note that it might be more precise to write x :: a and x :: b therefore a ~ b). Generalising to more complex definitions is just a matter of applying this technique systematically. Just be careful to use a fresh name whenever you introduce a type variable.
f g x y = g x (y x)
On the LHS, f takes 3 parameters, so f :: a -> b -> c -> d, with g :: a, x :: b, y :: c. On the RHS, g takes 2 parameters, the first x :: b, the second y x :: e, and returns the same type as f, so g :: b -> e -> d. Looking at y, it takes one parameter, x :: b, and returns y x :: e, so y :: b -> e. Putting it all together, a ~ (b -> e -> d), c ~ (b -> e), and f :: (b -> e -> d) -> b -> (b -> e) -> d. An automated type checker would need to be more systematic than me, but that should get you started. It gets a little more interesting when you end up with a constraint like this: a Int ~ [b]. In this case, a ~ [] and b ~ Int.
participants (4)
-
Brandon S. Allbery KF8NH
-
Matthew Brecknell
-
PR Stanley
-
Tim Chevalier