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:07]
cata_chiru [7.4. Natural Numbers - Church numerals]
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:
Line 279: Line 266:
  
 <hidden Solution 7.4.> <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 305: 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>​
  
Line 316: Line 306:
 **7.5.2. (*)** Using the **Y**-Combinator,​ define a function $ FIB $ that computes the **n**-th fibonacci number. **7.5.2. (*)** Using the **Y**-Combinator,​ define a function $ FIB $ that computes the **n**-th fibonacci number.
  
-<hidden Solutions 7.5.1>+<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>​