;;;;;;;;;;;;;;;;;;;;;;;; ;; Common definitions ;; ;;;;;;;;;;;;;;;;;;;;;;;; ;; Define Nat (: Nat Type) (: Z Nat) (: S (-> Nat Nat)) ;; Define <= (: <= (-> $a $a Bool)) (= (<= $x $y) (or (< $x $y) (== $x $y))) ;; Define cast functions between Nat and Number (: fromNumber (-> Number Nat)) (= (fromNumber $n) (if (<= $n 0) Z (S (fromNumber (- $n 1))))) (: fromNat (-> Nat Number)) (= (fromNat Z) 0) (= (fromNat (S $k)) (+ 1 (fromNat $k))) ;;;;;;;;;;;;;;;;;;;;;; ;; Backward chainer ;; ;;;;;;;;;;;;;;;;;;;;;; ;; Curried Backward Chainer with fully annotated proofs. The ;; arguments of the backward chainer are: ;; ;; * Knowledge base: pointer to a space containing axioms and rules in ;; the format (: ). Note that rules are explicitely ;; curried, meaning that a rule with two premises is represented by ;; ;; (: (-> (-> ))) ;; ;; * Query: a metta term of the form (: ) where ;; and may contain free variables that may be ;; filled by the backward chainer. ;; ;; * Maximum depth: maximum depth of the generated proof tree. ;; ;; A result is the query with its variables grounded, fully or ;; partially. If multiple results are possible, they are returned as ;; a superposition. (: bc (-> $a ; Knowledge base space $b ; Query Nat ; Maximum depth $b)) ; Result ;; Base case (= (bc $kb (: $prf $ccln) $_) (match $kb (: $prf $ccln) (: $prf $ccln))) ;; Recursive step (= (bc $kb (: ($prfabs (: $prfarg $prms)) $ccln) (S $k)) (let* (((: $prfabs (-> $prms $ccln)) (bc $kb (: $prfabs (-> $prms $ccln)) $k)) ((: $prfarg $prms) (bc $kb (: $prfarg $prms) $k))) (: ($prfabs (: $prfarg $prms)) $ccln))) ;;;;;;;;;;;;;;;;;;;; ;; Knowledge base ;; ;;;;;;;;;;;;;;;;;;;; !(bind! &kb (new-space)) !(add-atom &kb (: Z Nat)) !(add-atom &kb (: S (-> Nat Nat))) !(add-atom &kb (: plus (-> Nat (-> Nat Nat)))) ;;;;;;;;;;; ;; Tests ;; ;;;;;;;;;;; ;; Add 1 and 2 (: ($op (: $x Nat) (: $y Nat)) Nat) ;; !(bc &kb (: (($op (: $x Nat)) (: $y Nat)) Nat) (fromNumber 3))