Jan kowalski
Jan kowalski

Reputation: 75

Calculating type Haskell

How to calculate type of (.)(.) in Haskell? I know that it should be

(.)(.) :: (a -> b -> c) -> a -> (a1 -> b) -> a1 -> c

But how to calculate it without computer?

Upvotes: 4

Views: 104

Answers (3)

hasufell
hasufell

Reputation: 573

Since I wasn't particularly satisfied with the missing explanations in the accepted answer, I give my POV as well:

-- this is the original type signature
(.) :: (b -> c) -> (a -> b) -> a -> c

-- now because of haskell polymorphism,
-- even 'b' and 'c' and so on could be functions
--
-- (.)(.) means we shove the second function composition
-- into the first as an argument.
-- Let's give the second function a distinct type signature, so we
-- don't mix up the types:
(.) :: (e -> f) -> (d -> e) -> d -> f

-- Since the first argument of the initial (.) is of type (b -> c)
-- we could say the following if we apply the second (.) to it:
(b -> c) == (e -> f) -> (d -> e) -> d -> f

-- further, because of how currying works, as in
(e -> f) -> (d -> e) -> d -> f == (e -> f) -> ((d -> e) -> d -> f)
-- we can conclude
b == (e -> f)
c == (d -> e) -> d -> f

-- since we passed one argument in, the function arity changes,
-- so we'd actually only have (a -> b) -> a -> c left, but that
-- doesn't represent the types we have now, so we have to substitute
-- for b and c, so
(a -> b) -> a -> c
-- becomes
(.)(.) :: (a -> (e -> f)) -> a -> (d -> e) -> d -> f
-- and again because of currying we can also write
(.)(.) :: (a -> e -> f) -> a -> (d -> e) -> d -> f

Upvotes: 0

felix-eku
felix-eku

Reputation: 2213

(.)    :: (b        -> c                     ) -> ((a -> b)        -> (a -> c))
   (.) :: ((e -> f) -> ((d -> e) -> (d -> f)))
(.)(.) ::                                         ((a -> (e -> f)) -> (a -> ((d -> e) -> (d -> f))))
(.)(.) :: (a -> (e -> f)) -> (a -> ((d -> e) -> (d -> f)))
(.)(.) :: (a -> e -> f) -> a -> ((d -> e) -> (d -> f))
(.)(.) :: (a -> e -> f) -> a -> (d -> e) -> (d -> f)
(.)(.) :: (a -> e -> f) -> a -> (d -> e) -> d -> f

Upvotes: 8

Random Dev
Random Dev

Reputation: 52280

by (manual) pattern-matching and rewriting types-variables

(.) has type (b -> c) -> ((a -> b) -> a -> c) so the first argument should have type b -> c. Now if we use it again we have to substitute b with b' -> c' and c with (a' -> b') -> a' -> c') (the second (.) should have type (b' -> c') -> ((a' -> b') -> a' -> c')) and we get

(a -> b' -> c') -> a -> (a' -> b') -> a' -> c'

which is (after renaming) the same as above.

Note that I used a -> b -> c = a -> (b -> c) here

using GHCi

yeah I know - you want it by hand - but GHCi is such a valuable tool that you really should use it to confirm your manual labor.

Here from a terminal:

$ ghci
GHCi, version 7.10.1: http://www.haskell.org/ghc/  :? for help
Prelude> :t (.)(.)
(.)(.) :: (a -> b -> c) -> a -> (a1 -> b) -> a1 -> c
Prelude> 

as you can see the type is (a -> b -> c) -> a -> (a1 -> b) -> a1 -> c

btw: :t is short for :type and you can see all commands with :help from inside a GHCi session.

Upvotes: 5

Related Questions