Article Directory
4.4 Encoding
To perform pair coding, we need three types of operations:
- Combine two values
- Operation to extract the first value
- Operation to extract the second value
In other words, we need mkpair, fst, snd to satisfy the following equation:
fst (mkpair MN) = n M snd (mkpair MN) = n N fst(mkpair\ M\ N) =_n M\\ snd(mkpair \ M\ N) =_n Nfst(mkpair M N)=nMsnd(mkpair M N)=nN
, we can also use the symbol ⟨M, N⟩ as an abbreviation of (first value M, the second value of N). One way to find the definition of mkpair, etc. is to consider what a value of ⟨M, N⟩ might look like.
Because all our values are functions, ⟨M, N⟩ must also be a function. This function needs to contain expressions of M and N, and it must return one of the values to the user (depending on the user’s needs) One or the second value). This prompts the user to call the pair as a function, and provide true to get the first value, and false to get the second value:
⟨M, N⟩ ≐ λ s. If s MN ⟨M,N⟩\doteq \lambda s.if\ s\ M\ N⟨M,N⟩≐λ s . i f s M N As
mentioned in the previous section, the if function is not necessary, so the above function can be simplified by discarding if.
Encoded in this way, the fst function receives the pair value and applies it as a true parameter.
fst ≐ λ p. p true fst\doteq\lambda pp\ truefst≐λ p . p t r u e
Similarly, snd applies the pair value as a parameter of false. Finally, in order to define mkpair, we abstractly abbreviate ⟨M, N⟩ to arbitrary M and N.
Exercise 4.5
Prove that the above mkpair, fst and snd satisfy the equation at the beginning of this section.
answer
-
fst (mkpair M N) = (λp.p true) ((λx.λy.λs.s x y) M N)
→nβ ((λx.λy.λs.s x y) M N) true
→nβ (λy.λs.s M y) N) true
→nβ (λs.s M N) true
→nβ true M N = (λx.λy.x) M N
→ n β (λ.M) N
→nβ M
-
snd (mkpair M N) = (λp.p false) ((λx.λy.λs.s x y) M N)
→nβ ((λx.λy.λs.s x y) M N) false
→nβ (λy.λs.s M y) N) false
→nβ (λs.s M N) false
→nβ false M N = (λx.λy.y) M N
→ n β (λy.y) N
→nβ N