Skip to content

Commit

Permalink
Use seal to patch SICP and combinators examples for minimal MeTTa
Browse files Browse the repository at this point in the history
Implementing lambda requires variables with restricted scope, see
trueagi-io#39
  • Loading branch information
vsbogd committed Mar 1, 2024
1 parent 65c8f3f commit 0c7e768
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
22 changes: 13 additions & 9 deletions SICP_book/chapter_2_2.metta
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@

(: lambda1 (-> Variable Atom (-> $a $t)))
(= ((lambda1 $var $body) $val)
(let (quote $var) (quote $val) $body) )
(chain (eval (sealed ($var) ($var $body))) $seal
(unify ($sv $sb) $seal (let (quote $sv) (quote $val) $sb) (Error (unify ($sv $sb) $seal) "Unexpected error"))))

(= (square $x)
(* $x $x))
Expand Down Expand Up @@ -573,15 +574,14 @@
; Scale tree but using map for definition
(= (scale-tree-m $tree $factor)
(map (lambda1 $sub-tree
(case $sub-tree
(sealed ($_ $x $xs) (case $sub-tree
(((Cons $x $xs) (scale-tree-m $sub-tree $factor))
($_ (* $sub-tree $factor)))))
($_ (* $sub-tree $factor))))))
$tree))
!(assertEqual
(scale-tree-m (list (1 (list (2 (list (3 4)) 5)) (list (6 7)))) 10)
(tree (10 (20 (30 40) 50) (60 70))))


; Exercise 2.30.
;
; Define a procedure square-tree analogous to the square-list procedure
Expand Down Expand Up @@ -611,9 +611,9 @@

(= (square-tree-m $tree)
(map (lambda1 $sub-tree
(case $sub-tree
(sealed ($_ $x $xs) (case $sub-tree
(((Cons $x $xs) (square-tree-m $sub-tree))
($_ (* $sub-tree $sub-tree)))))
($_ (* $sub-tree $sub-tree))))))
$tree))

!(assertEqual
Expand Down Expand Up @@ -791,7 +791,8 @@

(: lambda2 (-> Variable Variable Atom (-> $a $b $t)))
(= ((lambda2 $var1 $var2 $body) $val1 $val2)
(let (quote ($var1 $var2)) (quote ($val1 $val2)) $body))
(chain (eval (sealed ($var1 $var2) ($var1 $var2 $body))) $seal
(unify ($sv1 $sv2 $sb) $seal (let (quote ($sv1 $sv2)) (quote ($val1 $val2)) $sb) (empty)) ))

(= (acc-map $p $sequence)
(accumulate (lambda2 $x $y (Cons ($p $x) $y)) Nil $sequence))
Expand Down Expand Up @@ -983,7 +984,10 @@

(: lambda3 (-> Variable Variable Variable Atom (-> $a $b $c $t)))
(= ((lambda3 $var1 $var2 $var3 $body) $val1 $val2 $val3)
(let (quote ($var1 $var2 $var3)) (quote ($val1 $val2 $val3)) $body))
(chain (eval (sealed ($var1 $var2 $var3) ($var1 $var2 $var3 $body))) $seal
(unify ($sv1 $sv2 $sv3 $sb) $seal
(let (quote ($sv1 $sv2 $sv3)) (quote ($val1 $val2 $val3)) $sb)
(Error (unify ($sv1 $sv2 $sv3 $sb) $seal) "Unexpected error"))))

(= (fold-left $op $initial $sequence)
(let $iter (lambda3 $result $rest $self
Expand Down Expand Up @@ -1692,4 +1696,4 @@
(= (right-split-s) (split beside below))
(= (up-split-s) (split below beside))

; -----------------------End of Exercise 2.45---------------------------
; -----------------------End of Exercise 2.45---------------------------
15 changes: 8 additions & 7 deletions combinator_logic_experiments/y_comb_examples.metta
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
; Non-recursive function defined using Y combinator
; Atom type is required type of the first argument otherwise
; implementation falls into infinite recursion
(: id' (-> Atom (-> $t $t)))
(= ((id' $f) $x) $x)
(= (id $x) ((Y id') $x))
(: id'' (-> Atom (-> $t $t)))
(= ((id'' $f) $x) $x)
(= (id' $x) ((Y id'') $x))

!(assertEqual (id A) A)
!(assertEqual (id' A) A)

; Recursive function defined using Y combinator
(: fac (-> Atom (-> Int Int)))
Expand All @@ -66,12 +66,13 @@
; inside let.
(: lambda (-> Variable Atom (-> Atom $t)))
(= ((lambda $var $body) $val)
(let (quoted $var) (quoted $val) $body))
(chain (eval (sealed ($var) ($var $body))) $seal
(unify ($sv $sb) $seal (let (quote $sv) (quote $val) $sb) (Error (unify ($sv $sb) $seal ...) "Pair of atoms is expe error"))))

; Non-recursive function defined using lambda and Y combinator
(= (id' $x) ((Y (lambda $f (lambda $x $x))) $x))
(= (id''' $x) ((Y (lambda $f (lambda $x $x))) $x))

!(assertEqual (id' A) A)
!(assertEqual (id''' A) A)

; Recursive function defined using lambda and Y combinator
(= (fact' $y)
Expand Down

0 comments on commit 0c7e768

Please sign in to comment.