In an earlier post, we’ve defined what formal systems are.

In this example, we’ll put formal systems into action by building a proof tree generator in the Racket programming language.

We should be able to specify axioms and inference rules, and then query the program so that it will produce all valid combinations of inference in attempt to reach the target result.

First, we’ll start by defining our data structures:

; A rule is a way to change a theorem (struct rule (name function) #:transparent) ; A theorem is consisted of an initial axiom and rules (ordered set) applied (struct theorem (axiom rules result) #:transparent) ; A prover system is consisted of a bunch of axioms and rules to apply between them (struct theorem-prover (axioms rules) #:transparent) ; An axiom is just a theorem already proven (define (axiom a) (theorem (list a) '() a))

Now, to apply a rule to a theorem, we create a new theorem whose result is all the rules applied to the target theorem:

; Apply a single rule to a theorem (define (theorem-apply-rule p t r) (theorem (theorem-axiom t) (append (theorem-rules t) (list r)) ((rule-function r) (theorem-result t) p)))

We will need a procedure that will apply all the rules to all theorems consisted in a given object `theorem-prover`

:

; Apply all prover's rules to a list of theorems (define (theorems-apply-rules-iter prover theorems result) (cond ((eq? theorems '()) result) (else (theorems-apply-rules-iter prover (cdr theorems) (append (map (lambda (r) (theorem-apply-rule prover (car theorems) r)) (theorem-prover-rules prover)) result))))) ; Helper procedure (define (theorems-apply-rules prover theorems) (theorems-apply-rules-iter prover theorems '()))

Now, in order to find a proof for a given `theorem-prover`

, we search through the theorem results and see if the target is there. If it is, we just return. Otherwise, we recursively go through the theorems and apply rules in order to attempt to find the target theorem. Here’s the procedure that searches for a proof:

; Find a proof by constructing a proof tree by iteratively applying theorem rules (define (find-proof-iter prover target max-depth found-proofs depth) (cond ; The case where the proof was found ((member target (map theorem-result found-proofs)) (findf (lambda (t) (equal? (theorem-result t) target)) found-proofs)) ; The case where max depth of search was reached ((> depth max-depth) #f) ; Otherwise just try to apply the known rules to the found proofs (else (letrec ([theorems (theorems-apply-rules prover found-proofs)] [proofs-set (list->set (map theorem-result found-proofs))] [theorems-set (list->set (map theorem-result theorems))]) (if (equal? (set-union proofs-set theorems-set) proofs-set) ; The case where no new theorems were produced, that is, A union B = A #f ; Otherwise keep producing new proofs (find-proof-iter prover target max-depth (merge-proofs found-proofs theorems) (+ 1 depth))))))) ; Helper procedure (define (find-proof prover target max-depth) (find-proof-iter prover target max-depth (theorem-prover-axioms prover) 0))

But what is `merge-proofs`

? It’s simply a procedure that given 2 lists of proofs, it will return them merged. However, we want to avoid duplicates to skip duplicate processing. So the proof tree should not contain duplicate nodes.

; Merge two list of proofs but skip duplicate proofs, giving the first argument priority ; This is used to avoid circular results in the search tree ; E.g. application of rules resulting in an earlier theorem/axiom (define (merge-proofs p1 p2) (remove-duplicates (append p1 p2) (lambda (t1 t2) (equal? (theorem-result t1) (theorem-result t2)))))

So, as an example usage:

; Example rules (define mu-rules (list (rule "One" (lambda (t p) (if (string-suffix? t "I") (string-append t "U") t))) (rule "Two" (lambda (t p) (let ([matches (regexp-match #rx"M(.*)" t)]) (if (and (list? matches) (>= 2 (length matches))) (string-append t (cadr matches)) t)))) (rule "Three" (lambda (t p) (string-replace t "III" "U" #:all? #f))) (rule "Four" (lambda (t p) (string-replace t "UU" "" #:all? #f))))) ; Example prover (define test-prover (theorem-prover (list (axiom "MI")) mu-rules)) ; Find the proof of "MIUIU" with max-depth of 5 (find-proof test-prover "MIUIU" 5)

As a result, we get: `(theorem '("MI") (list (rule "One" #) (rule "Two" #)) "MIUIU")`

, which says that for a starting theorem `MI`

, we apply rule “One” and rule “Two” (in that order) to get to `MIUIU`

(our target proof that we’ve specified) which is pretty awesome ðŸ™‚