Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
pp:2025:l07 [2025/04/14 22:00]
cata_chiru [7.3. Booleans]
pp:2025:l07 [2025/04/14 22:36] (current)
ldaniel
Line 82: Line 82:
 <hidden Solutions 7.1.1> <hidden Solutions 7.1.1>
  
-Solutions:+Solutions: ​\\ \\ 
 +1. $ \lambda y.(\lambda x.x_1 \ (x_2 \ y_1)) $ \\ 
 +Bounded occurrences:​ $ x_1 $ (to $ \lambda x $), $ y_1 $ (to $ \lambda y $). \\ 
 +Free occurrences:​ $ x_2 $ (Not bounded by $ \lambda x.x_1 $) \\ 
 +In the sub-context of $ \lambda x.x_1 \ (x_2 \ y_1) $, $ y_1 $ is free. \\
 \\ \\
- +2. $ (\lambda x.(x_1 \ \lambda y_1.((x_2 \ y_2) \ z_1)) \ (x_3 \ \lambda y_3.x_4)) $ \\ 
-1. $ \lambda y.(\lambda x.x_1 \ (x_2 \ y_1)) $ +Bounded occurrences:​ $ x_1, x_2 $ (to $ \lambda x $), $ y_2$ (to $ \lambda ​y_1 $). \\ 
- +Free occurrences:​ $ z_1, x_3, x_4 $. \\ 
-Bounded occurrences:​ $ x_1 $ (to $ \lambda x $), $ y_1 $ (to $ \lambda ​$). +In the sub-context of $ \lambda ​y_1.((x_2 \ y_2) \ z_1) $, $ x_2 $ is free. \\
- +
-Free occurrences:​ $ x_2 (Not bounded by $ \lambda x.x_1 $) +
- +
-In the sub-context of $ \lambda ​x.x_1 \ (x_2 \ y_1) $, $ y_1 $ is free.  +
 \\ \\
-2. $ (\lambda x.(x_1 \ \lambda y_1.((x_2 \ y_2) \ z_1)) \ (x_3 \ \lambda y_3.x_4)) $ +3. $ (\lambda f.(\lambda x_1.f_1 \ (x_2 \ x_3)) \ (\lambda x_4.f_2 \ (x_5 \ x_6))) $ \\ 
- +Bounded occurrences:​ $ f_1 $ (to $ \lambda f $). \\ 
-Bounded occurrences:​ $ x_1, x_2 $ (to $ \lambda x $), $ y_2$ (to $ \lambda y_1 $), +Free occurrences:​ $ x_2, x_3, f_2, x_5, x_6 $. \\
- +
-Free occurrences:​ $ z_1, x_3, x_4 $ +
- +
-In the sub-context of $ \lambda y_1.((x_2 \ y_2) \ z_1) $, $ x_2 $ is free. +
- +
-\\ +
-3. $ (\lambda f.(\lambda x_1.f_1 \ (x_2 \ x_3)) \ (\lambda x_4.f_2 \ (x_5 \ x_6))) $  +
- +
-Bounded occurrences:​ $ f_1 $ (to $ \lambda f $),  +
- +
-Free occurrences:​ $ x_2, x_3, f_2, x_5, x_6 $+
  
 </​hidden>​ </​hidden>​
- 
- 
  
 ===== 7.2. Reduction rules ===== ===== 7.2. Reduction rules =====
Line 177: Line 163:
 $ OR = \lambda x.\lambda y.((x \ x) \ y) $ \\ $ OR = \lambda x.\lambda y.((x \ x) \ y) $ \\
 $ NOT = \lambda x.((x \ FALSE) \ TRUE) $ \\ $ NOT = \lambda x.((x \ FALSE) \ TRUE) $ \\
 +$ IF = \lambda c.\lambda t.\lambda e.((c t) e) $ \\
  
 <​hidden>​ <​hidden>​
Line 198: Line 185:
 **7.3.3.** Define the $ NOR $ operations over booleans. **7.3.3.** Define the $ NOR $ operations over booleans.
  
-<hidden Solutions 7.3.1>+<hidden Solutions 7.3>
  
 Solutions: Solutions:
  
 https://​aartaka.me/​lambda-3.html https://​aartaka.me/​lambda-3.html
 +
 +\\
  
 7.3.1: ​ 7.3.1: ​
Line 210: Line 199:
 V2: XOR = $ \lambda a.\lambda b.((OR (AND (NOT a)) b)) (AND (NOT b)) a))$ V2: XOR = $ \lambda a.\lambda b.((OR (AND (NOT a)) b)) (AND (NOT b)) a))$
  
-\\ 
 \\ \\
 7.3.2: 7.3.2:
Line 218: Line 206:
 V2: NAND = $ \lambda a.\lambda b.(NOT (AND a b)) $ V2: NAND = $ \lambda a.\lambda b.(NOT (AND a b)) $
  
-\\ 
 \\ \\
  
Line 272: Line 259:
 **7.4.3. (*)** Define the predecessor operator, that takes a number and returns the number prior to it. What's the predecessor of 0? Evaluate $ (PRED \ N0) $. **7.4.3. (*)** Define the predecessor operator, that takes a number and returns the number prior to it. What's the predecessor of 0? Evaluate $ (PRED \ N0) $.
  
-**Solution:** \\ +**7.4.4.** Define substraction under church numerals$ SUB = \lambda n.\lambda m. \ ... $ (**Hint**: use $ PRED $). What happens if you try to substract a bigger number from a smaller one? Evaluate $ (SUB N1 N2 )$. 
-<hidden 7.4.>+ 
 +**7.4.5.** Define $ LEQ $ (less or equal). $ LEQ \ n \ m $ should return **TRUE** if $ n \leq m $ and **FALSE** if $ n > m $. 
 + 
 +**7.4.6.** Define $ EQ $ (equality). $ EQ \ n \ m $ should return **TRUE** if $ n = m $ and **FALSE** otherwise. 
 + 
 +<​hidden ​Solution ​7.4.> 
 + 
 +7.4.3. 
 Let's start with defining a // shift-and-increment // operator: \\ Let's start with defining a // shift-and-increment // operator: \\
 $ \phi' = \lambda x.(PAIR \ x \ (SUCC \ x)) $ \\ $ \phi' = \lambda x.(PAIR \ x \ (SUCC \ x)) $ \\
Line 291: Line 286:
 \\ \\
 </​hidden>​ </​hidden>​
- 
-\\ 
- 
-**7.4.4.** Define substraction under church numerals: $ SUB = \lambda n.\lambda m. \ ... $ (**Hint**: use $ PRED $). What happens if you try to substract a bigger number from a smaller one? Evaluate $ (SUB \ N1 \ N2 )$. 
- 
-**7.4.5.** Define $ LEQ $ (less or equal). $ LEQ \ n \ m $ should return **TRUE** if $ n \leq m $ and **FALSE** if $ n > m $. 
- 
-**7.4.6.** Define $ EQ $ (equality). $ EQ \ n \ m $ should return **TRUE** if $ n = m $ and **FALSE** otherwise. 
  
 ==== 7.5. Recursion and the Sage Bird ==== ==== 7.5. Recursion and the Sage Bird ====
Line 308: Line 295:
 The **Y**-combinator in lambda calculus looks like this: \\ The **Y**-combinator in lambda calculus looks like this: \\
 \\ \\
-$ FIX = \lambda f.(\lambda x.(f \ (x \ x)) \ (\lambda x.f \ (x \ x))) $+$ FIX = \lambda f.(\lambda x.(f \ (x \ x)) \ \lambda x.(f \ (x \ x))) $
 </​note>​ </​note>​
  
 ---- ----
  
-<hidden Solutions 7.5.1>+**Exercises** \\ 
 + 
 +**7.5.1. (*)** Using the **Y**-Combinator,​ define a function that computes the factorial of a number **n**. 
 + 
 +**7.5.2. (*)** Using the **Y**-Combinator,​ define a function $ FIB $ that computes the **n**-th fibonacci number. 
 + 
 +<hidden Solutions 7.5.>
  
 Solutions: Solutions:
 +\\ \\
 +7.5.1 \\
 +FACT  =  $ \lambda n. Y FACT' n $ \\
 +FACT' ​ =  $ \lambda rec. \lambda n. IF (ISZERO n) TRUE (MULT n (rec (PRED n)) -- if (n == 0) return 1 else n * rec(n - 1) $ \\
  
 </​hidden>​ </​hidden>​
  
  
-**Exercises** \\ 
- 
-**7.5.1. (*)** Using the **Y**-Combinator,​ define a function that computes the factorial of a number **n**. 
- 
-**7.5.2. (*)** Using the **Y**-Combinator,​ define a function $ FIB $ that computes the **n**-th fibonacci number.