简体   繁体   中英

Figuring out types of function composition by hand or mentally

My attempt is currently like this: if I had (.) const const . First

(.) const :: 
(b→c)→((a→b)→(a→c)) (a→(b→a)) = 
(b→c)→((a→b)→(a→c)) (b→(d→b)) = -- (1) See below question
(a→b)→(a→(d→b)) = -- substitute (b→c) by (b→(d→b))

Next

(.) const const :: 
(a→b)→(a→(d→b)) (a→(b→a)) =
(a→b)→(a→(d→b)) (a→(e→a)) = -- (1) See below question
a→(d→(e→a)) -- substitute (a→b) by (a→(e→a)
  1. I knew some variables had to match, some were different but had same name as coincidence, and changed it. Is there a formal, correct way of doing it?
  2. I want to do it mentally, are there good exercises? Where should I start?

Knowing the result of function composition seems like hugely productive skill if someone is programming in FP language.

Here's how I do it mentally.

I have a function of type x->y and a function of type z->t . If I want to compose them, y and z must be the same type, and the result will be of type x->t .

For const . const const . const both functions have type a -> (b -> a) , but I need to rename variables in order to avoid clashes. So I have a->(b->a) and c->(d->c) . Now c is the same as b->a . I substitute the latter in the second type, and the final result is a->d->b->a .

Learning lambda calculus, simply typed lambda calculus seems like a good start for more vigorous approach.

(\f g x -> f (g x)) (\a b -> a) (\a b -> a) =
(\x -> (\a b -> a) ((\a b -> a) x)) =
(\x -> (\a b -> a) (\b -> x)) = 
(\x -> (\a b -> a) (\c -> x)) = 
(\x -> (\b -> (\c -> x)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM