1 | ;;; -*- Mode: Lisp; Syntax: Common-Lisp; Package: Grobner; Base: 10 -*-
|
---|
2 | #|
|
---|
3 | $Id: prover.lisp,v 1.3 2009/01/22 04:06:51 marek Exp $
|
---|
4 | *--------------------------------------------------------------------------*
|
---|
5 | | Copyright (C) 1994, Marek Rychlik (e-mail: rychlik@math.arizona.edu) |
|
---|
6 | | Department of Mathematics, University of Arizona, Tucson, AZ 85721 |
|
---|
7 | | |
|
---|
8 | | Everyone is permitted to copy, distribute and modify the code in this |
|
---|
9 | | directory, as long as this copyright note is preserved verbatim. |
|
---|
10 | *--------------------------------------------------------------------------*
|
---|
11 | |#
|
---|
12 | (defpackage "PROVER"
|
---|
13 | (:use "COMMON-LISP" "GROBNER" "PARSE" "ORDER" "COEFFICIENT-RING" "PRINTER")
|
---|
14 | (:export identical-points perpendicular parallel collinear translate-theorem prove-theorem
|
---|
15 | equidistant midpoint translate-statements real-identical-points
|
---|
16 | euclidean-distance
|
---|
17 | *prover-order*))
|
---|
18 |
|
---|
19 | (in-package "PROVER")
|
---|
20 |
|
---|
21 | (proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
|
---|
22 |
|
---|
23 | (defvar *prover-order* #'grevlex>
|
---|
24 | "Admissible monomial order used internally in the proofs of theorems.")
|
---|
25 |
|
---|
26 | ;; Translate a geometric theorem specification into a statement of the
|
---|
27 | ;; form
|
---|
28 | ;; for all u1, u2, ... , us f1=f2=...=fn=0 => g=0
|
---|
29 |
|
---|
30 | (defun csym (symbol number)
|
---|
31 | "Return symbol whose name is a concatenation of (SYMBOL-NAME SYMBOL)
|
---|
32 | and a number NUMBER."
|
---|
33 | (intern (concatenate 'string (symbol-name symbol) (format nil "~d" number))))
|
---|
34 |
|
---|
35 | (defmacro real-identical-points (A B)
|
---|
36 | "Return [ (A1-B1)**2 + (A2-B2)**2 ] in lisp (prefix) notation.
|
---|
37 | The second value is the list of variables (A1 B1 A2 B2). Note that
|
---|
38 | if the distance between two complex points A, B is zero, it does not
|
---|
39 | mean that the points are identical. Use IDENTICAL-POINTS to express
|
---|
40 | the fact that A and B are really identical. Use this macro in conclusions
|
---|
41 | of theorems, as it may not be possible to prove that A and B are trully
|
---|
42 | identical in the complex domain."
|
---|
43 | (let ((A1 (csym A 1))
|
---|
44 | (A2 (csym A 2))
|
---|
45 | (B1 (csym B 1))
|
---|
46 | (B2 (csym B 2)))
|
---|
47 | `(list
|
---|
48 | '((+ (expt (- ,A1 ,B1) 2) (expt (- ,A2 ,B2) 2)))
|
---|
49 | '(,A1 ,A2 ,B1 ,B2))))
|
---|
50 |
|
---|
51 | (defmacro identical-points (A B)
|
---|
52 | "Return [ A1-B1, A2-B2 ] in lisp (prefix) notation.
|
---|
53 | The second value is the list of variables (A1 B1 A2 B2). Note that
|
---|
54 | sometimes one is able to prove only that (A1-B1)**2 + (A2-B2)**2 = 0.
|
---|
55 | This equation in the complex domain has solutions with A and B distinct.
|
---|
56 | Use REAL-IDENTICAL-POINTS to express the fact that the distance between
|
---|
57 | two points is 0. Use this macro in assumptions of theorems, although this
|
---|
58 | is seldom necessary because we assume most of the time that in assumptions
|
---|
59 | all points are distinct if they are denoted by different symbols."
|
---|
60 | (let ((A1 (csym A 1))
|
---|
61 | (A2 (csym A 2))
|
---|
62 | (B1 (csym B 1))
|
---|
63 | (B2 (csym B 2)))
|
---|
64 | `(list
|
---|
65 | '((- ,A1 ,B1) (- ,A2 ,B2))
|
---|
66 | '(,A1 ,A2 ,B1 ,B2))))
|
---|
67 |
|
---|
68 | (defmacro perpendicular (A B C D)
|
---|
69 | "Return [ (A1-B1) * (C1-D1) + (A2-B2) * (C2-D2) ] in lisp (prefix) notation.
|
---|
70 | The second value is the list of variables (A1 A2 B1 B2 C1 C2 D1 D2)."
|
---|
71 | (let ((A1 (csym A 1))
|
---|
72 | (A2 (csym A 2))
|
---|
73 | (B1 (csym B 1))
|
---|
74 | (B2 (csym B 2))
|
---|
75 | (C1 (csym C 1))
|
---|
76 | (C2 (csym C 2))
|
---|
77 | (D1 (csym D 1))
|
---|
78 | (D2 (csym D 2)))
|
---|
79 | `(list
|
---|
80 | '((+ (* (- ,A1 ,B1) (- ,C1 ,D1))
|
---|
81 | (* (- ,A2 ,B2) (- ,C2 ,D2))))
|
---|
82 | '(,A1 ,A2 ,B1 ,B2 ,C1 ,C2 ,D1 ,D2))))
|
---|
83 |
|
---|
84 | (defmacro parallel (A B C D)
|
---|
85 | "Return [ (A1-B1) * (C2-D2) - (A2-B2) * (C1-D1) ] in lisp (prefix) notation.
|
---|
86 | The second value is the list of variables (A1 A2 B1 B2 C1 C2 D1 D2)."
|
---|
87 | (let ((A1 (csym A 1))
|
---|
88 | (A2 (csym A 2))
|
---|
89 | (B1 (csym B 1))
|
---|
90 | (B2 (csym B 2))
|
---|
91 | (C1 (csym C 1))
|
---|
92 | (C2 (csym C 2))
|
---|
93 | (D1 (csym D 1))
|
---|
94 | (D2 (csym D 2)))
|
---|
95 | `(list
|
---|
96 | '((- (* (- ,A1 ,B1) (- ,C2 ,D2))
|
---|
97 | (* (- ,A2 ,B2) (- ,C1 ,D1))))
|
---|
98 | '(,A1 ,A2 ,B1 ,B2 ,C1 ,C2 ,D1 ,D2))))
|
---|
99 |
|
---|
100 |
|
---|
101 | (defmacro collinear (A B C)
|
---|
102 | "Return the determinant det([[A1,A2,1],[B1,B2,1],[C1,C2,1]]) in lisp (prefix) notation.
|
---|
103 | The second value is the list of variables (A1 A2 B1 B2 C1 C2)."
|
---|
104 | (let ((A1 (csym A 1))
|
---|
105 | (A2 (csym A 2))
|
---|
106 | (B1 (csym B 1))
|
---|
107 | (B2 (csym B 2))
|
---|
108 | (C1 (csym C 1))
|
---|
109 | (C2 (csym C 2)))
|
---|
110 | `(list
|
---|
111 | '((+ (- (* ,B1 ,C2) (* ,B2 ,C1))
|
---|
112 | (- (* ,A2 ,C1) (* ,A1 ,C2))
|
---|
113 | (- (* ,A1 ,B2) (* ,A2 ,B1))))
|
---|
114 | '(,A1 ,A2 ,B1 ,B2 ,C1 ,C2))))
|
---|
115 |
|
---|
116 | (defmacro equidistant (A B C D)
|
---|
117 | "Return the polynomial [(A1-B1)**2+(A2-B2)**2-(C1-D1)**2-(C2-D2)**2] in lisp (prefix)
|
---|
118 | notation. The second value is the list of variables (A1 A2 B1 B2 C1 C2 D1 D2)."
|
---|
119 | (let ((A1 (csym A 1))
|
---|
120 | (A2 (csym A 2))
|
---|
121 | (B1 (csym B 1))
|
---|
122 | (B2 (csym B 2))
|
---|
123 | (C1 (csym C 1))
|
---|
124 | (C2 (csym C 2))
|
---|
125 | (D1 (csym D 1))
|
---|
126 | (D2 (csym D 2)))
|
---|
127 | `(list
|
---|
128 | '((- (+ (expt (- ,A1 ,B1) 2) (expt (- ,A2 ,B2) 2))
|
---|
129 | (+ (expt (- ,C1 ,D1) 2) (expt (- ,C2 ,D2) 2))))
|
---|
130 | '(,A1 ,A2 ,B1 ,B2 ,C1 ,C2 ,D1 ,D2))))
|
---|
131 |
|
---|
132 | (defmacro euclidean-distance (A B R)
|
---|
133 | "Return the polynomial [(A1-B1)**2+(A2-B2)**2-R^2] in lisp (prefix)
|
---|
134 | notation. The second value is the list of variables (A1 A2 B1 B2 R)."
|
---|
135 | (let ((A1 (csym A 1))
|
---|
136 | (A2 (csym A 2))
|
---|
137 | (B1 (csym B 1))
|
---|
138 | (B2 (csym B 2)))
|
---|
139 | `(list
|
---|
140 | '((- (+ (expt (- ,A1 ,B1) 2) (expt (- ,A2 ,B2) 2))
|
---|
141 | (expt ,R 2)))
|
---|
142 | '(,A1 ,A2 ,B1 ,B2 ,R))))
|
---|
143 |
|
---|
144 | (defmacro midpoint (A B C)
|
---|
145 | "Express the fact that C is a midpoint of the segment AB.
|
---|
146 | Returns the list [ 2*C1-A1-B1, 2*C2-A2-B2 ]. The second value returned is the list
|
---|
147 | of variables (A1 A2 B1 B2 C1 C2)."
|
---|
148 | (let ((A1 (csym A 1))
|
---|
149 | (A2 (csym A 2))
|
---|
150 | (B1 (csym B 1))
|
---|
151 | (B2 (csym B 2))
|
---|
152 | (C1 (csym C 1))
|
---|
153 | (C2 (csym C 2)))
|
---|
154 | `(list
|
---|
155 | '((- (* 2 ,C1) ,A1 ,B1) (- (* 2 ,C2) ,A2 ,B2))
|
---|
156 | '(,A1 ,A2 ,B1 ,B2 ,C1 ,C2))))
|
---|
157 |
|
---|
158 | (defmacro translate-statements (&rest statements)
|
---|
159 | `(list (mapcar #'car (list ,@statements))
|
---|
160 | (remove-duplicates (apply #'append (mapcar #'cadr (list ,@statements))))))
|
---|
161 |
|
---|
162 | (defmacro translate-assumptions (&rest assumptions)
|
---|
163 | (let ((x (gensym)))
|
---|
164 | `(let ((,x (translate-statements ,@assumptions)))
|
---|
165 | (list (apply #'append (car ,x))
|
---|
166 | (cadr ,x)))))
|
---|
167 |
|
---|
168 | (defmacro translate-conclusions (&rest conclusions)
|
---|
169 | `(translate-statements ,@conclusions))
|
---|
170 |
|
---|
171 | (defmacro translate-theorem ((&rest assumptions) (&rest conclusions))
|
---|
172 | "Translates a planar geometry theorem into a system of polynomial equations.
|
---|
173 | Each assumption or conclusion takes form of a declaration (RELATION-NAME A B C ...)
|
---|
174 | where A B C are points, entered as symbols and RELATION-NAME is a name of
|
---|
175 | a geometric relation, for example, (COLLINEAR A B C) means that points A, B, C
|
---|
176 | are all collinear. The translated equations use the convention that (A1,A2)
|
---|
177 | are the coordinates of the point A. This macro returns multiple values.
|
---|
178 | The first value is a list of polynomial expressions and the second value
|
---|
179 | is an automatically generated list of variables from points A, B, C, etc.
|
---|
180 | For convenience, several macros have been defined in order to make specifying
|
---|
181 | common geometric relations easy."
|
---|
182 | `(values
|
---|
183 | (translate-assumptions ,@assumptions)
|
---|
184 | (translate-conclusions ,@conclusions)))
|
---|
185 |
|
---|
186 | (defmacro prove-theorem ((&rest assumptions) (&rest conclusions)
|
---|
187 | &key (order *prover-order*))
|
---|
188 | "Proves a geometric theorem, specified in the same manner as in
|
---|
189 | the macro TRANSLATE-THEOREM. The proof is achieved by a call to
|
---|
190 | IDEAL-POLYSATURATION. The theorem is true if the returned value
|
---|
191 | is a trivial ideal containing 1."
|
---|
192 | (let ((vars (gensym))
|
---|
193 | (ideal (gensym))
|
---|
194 | (assump (gensym))
|
---|
195 | (concl (gensym)))
|
---|
196 | `(multiple-value-bind (,assump ,concl)
|
---|
197 | (translate-theorem ,assumptions ,conclusions)
|
---|
198 | (let* ((,vars (union (second ,assump) (second ,concl)))
|
---|
199 | (,ideal (ideal-polysaturation
|
---|
200 | (cdr (poly-eval `(:[ ,@(car ,assump)) ,vars
|
---|
201 | ,order *ring-of-integers*))
|
---|
202 | (mapcar #'(lambda (x)
|
---|
203 | (cdr (poly-eval (cons :[ x) ,vars
|
---|
204 | ,order *ring-of-integers*)))
|
---|
205 | (car ,concl))
|
---|
206 | ,order
|
---|
207 | 0
|
---|
208 | nil
|
---|
209 | *ring-of-integers*)))
|
---|
210 | (poly-print (cons '[ ,ideal) ,vars)
|
---|
211 | (values)))))
|
---|