Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating type Haskell

Tags:

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?

like image 390
Jan kowalski Avatar asked Dec 11 '22 23:12

Jan kowalski


2 Answers

(.)    :: (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
like image 87
felix-eku Avatar answered Dec 30 '22 07:12

felix-eku


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.

like image 24
Random Dev Avatar answered Dec 30 '22 06:12

Random Dev