简体   繁体   中英

Can someone explain how to unify types(Haskell)?

Can someone explain type unification in Haskell? For example: snd. snd:: (a1, (a2, c)) -> c snd. snd:: (a1, (a2, c)) -> c

Example

How do we get to, (a1, (a2, c)) -> c , from snd. snd snd. snd ?

Thanks in advance for the help.

Start with

snd :: (x1, y1) -> y1
snd :: (x2, y2) -> y2

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

Applying (.) to snd , with the following pairings

b ~ (x1, y1)
c ~ y1

yields

-- (.) :: (   b    -> c ) -> (a ->     b   ) -> a -> c
-- snd :: (x1, y1) -> y1
(.) snd ::                   (a -> (x1, y1)) -> a -> y1

Now applying this to snd again with the following pairings

a        ~ (x2, y2)
(x1, y1) ~ y2

yields

-- (.) snd :: (   a    -> (x1, y1)) -> a -> y1
-- snd ::     (x2, y2) ->     y2
(.) snd snd ::                      (x2, y2) -> y1

This obscures where y1 comes from. But since ~ is symmetric , we can replace y2 with (x1, y1) to yield

(.) snd snd  :: (x2, (x1, y1)) -> y1

which is equivalent to (a1, (a2, c)) -> c up to alpha renaming.

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