-
Notifications
You must be signed in to change notification settings - Fork 5
/
quoting_scoping_he_579.metta
63 lines (52 loc) · 2.32 KB
/
quoting_scoping_he_579.metta
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
; Test File: Quoting/Scoping Mechanism in MeTTa
; Bug Report: https://github.com/trueagi-io/hyperon-experimental/issues/579
; Issue Overview:
; This test demonstrates scenarios where unification and substitution of variables in MeTTa
; lead to unexpected results. It highlights the need for a quoting/scoping mechanism to
; prevent unification/substitution in specific contexts.
; Example 1: Basic Quoting Issue
; --------------------------------
!(assertEqualToResult
(let (quote $x) (quote A) $x)
(A))
; Explanation:
; Without a scoping mechanism, `quote` does not isolate `$x`, and substitution occurs.
!(assertEqualToResult
(let (quote $x) (quote (+ 1 1)) $x)
(2))
; Explanation:
; `quote` fails to preserve the quoted structure, and evaluation proceeds, producing `2`.
; Example 2: Real-World Example with Combinatory Logic
; ---------------------------------------------------
; Knowledge Base Setup
!(bind! &kb (new-space))
; Add K and S combinators to the knowledge base
!(add-atom &kb (: AK (-> $a (-> $b $a))))
!(add-atom &kb (: AS (-> (-> $a (-> $b $c)) (-> (-> $a $b) (-> $a $c)))))
; Define Backward Chainer
; Base case
(= (bc (: $prf $ccln) $_)
(match &kb (: $prf $ccln) (: $prf $ccln)))
; Recursive step
(= (bc (: ($prfabs $prfarg) $ccln) (S $k))
(let* (
((: $prfabs (-> $prms $ccln)) (bc (: $prfabs (-> $prms $ccln)) $k))
((: $prfarg $prms) (bc (: $prfarg $prms) $k))
)
(: ($prfabs $prfarg) $ccln)))
; Synthesize function composition (.)
!(assertEqualToResult
(bc (: $prg (-> (-> $b $c) (-> (-> $a $b) (-> $a $c)))) (S (S (S Z))))
((AS (AK AS)) AK)) ; Expected solution
; Test with maximum depth 0 to prevent variable generalization
!(assertEqualToResult
(bc (: $prg (-> (-> $b $c) (-> (-> $a $b) (-> $a $c)))) Z)
(AK)) ; Demonstrates a specific form due to absence of scoping.
; Example 3: Proposed Solution with Scoped Variables
; --------------------------------------------------
; Ideally, a scoping mechanism would enable queries like:
; !(bc (: $prg (∀ $a (∀ $b (∀ $c (-> (-> $b $c) (-> (-> $a $b) (-> $a $c))))))) Z)
; Such scoping would restrict solutions to fully generic combinatory logic structures.
; Summary:
; - These examples demonstrate the need for a quoting/scoping mechanism in MeTTa.
; - Scoping would allow more precise reasoning about programs and type variables.