| 1 |
;;;;;;;;;;; |
|---|
| 2 |
;; Insert into an a-list(?) (to lazy to find the real thing) |
|---|
| 3 |
;; |
|---|
| 4 |
;; The payload of the list for each operator is a list of |
|---|
| 5 |
;; things insert under that operator |
|---|
| 6 |
;; |
|---|
| 7 |
;; a the op |
|---|
| 8 |
;; b the expression |
|---|
| 9 |
;; L the list to add to |
|---|
| 10 |
(defun InsertByOp (a b L) |
|---|
| 11 |
(if (endp L) |
|---|
| 12 |
`((,a (,b))) ; list dodn't have the op, start a new section |
|---|
| 13 |
(if (eql a (caar L)) |
|---|
| 14 |
(cons (list (caar L) (cons b (cadar L))) (cdr L)) |
|---|
| 15 |
(cons (car L) (InsertByOp a b (cdr L))) |
|---|
| 16 |
) |
|---|
| 17 |
) |
|---|
| 18 |
) |
|---|
| 19 |
|
|---|
| 20 |
#| |
|---|
| 21 |
(insertbyop '+ 'go '((+ (hello)) (- (world)))) |
|---|
| 22 |
(insertbyop '- 'go '((+ (hello)) (- (world)))) |
|---|
| 23 |
(insertbyop '/ 'go '((+ (hello)) (- (world)))) |
|---|
| 24 |
(insertbyop '* 'go '()) |
|---|
| 25 |
(insertbyop '% 'go (insertbyop '* 'go '())) |
|---|
| 26 |
|# |
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
;;;;;;;;;;; |
|---|
| 31 |
;; Sort a list of expressions into an a-list by the |
|---|
| 32 |
; top operator of each expression |
|---|
| 33 |
;; |
|---|
| 34 |
;; toSort a list of expressions |
|---|
| 35 |
(defun sort_Rewrite_By_Top_Op (toSort) |
|---|
| 36 |
(if (endp toSort) |
|---|
| 37 |
'((+) (-) (*) (/)) |
|---|
| 38 |
(insertByOp |
|---|
| 39 |
(caaar toSort) |
|---|
| 40 |
(car toSort) |
|---|
| 41 |
(sort_Rewrite_By_Top_Op (cdr toSort)) |
|---|
| 42 |
) |
|---|
| 43 |
) |
|---|
| 44 |
) |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
;;;;;;;;;;;;;;;;; |
|---|
| 49 |
;; test if one expression is a generalization of another. |
|---|
| 50 |
;; true if the first expression is a trimmed version of the second |
|---|
| 51 |
;; |
|---|
| 52 |
;; (description inexact) |
|---|
| 53 |
;; (implementation inexact: fails to note reuse of trimmed sub expressions) |
|---|
| 54 |
;; |
|---|
| 55 |
;; NOTE: Assumes well formed |
|---|
| 56 |
|
|---|
| 57 |
(defun Generalization_Of (LHS RHS) |
|---|
| 58 |
(let ((ret (Generalization_Of_ LHS RHS '()))) |
|---|
| 59 |
; (print ret) |
|---|
| 60 |
ret |
|---|
| 61 |
) |
|---|
| 62 |
|
|---|
| 63 |
) |
|---|
| 64 |
|
|---|
| 65 |
;;;;;;;;;;;;;;;; |
|---|
| 66 |
;; implementation of Generalization_Of |
|---|
| 67 |
(defun Generalization_Of_ (LHS RHS map) |
|---|
| 68 |
; (print "") |
|---|
| 69 |
; (print LHS) |
|---|
| 70 |
; (print RHS) |
|---|
| 71 |
(if (atom LHS) |
|---|
| 72 |
t |
|---|
| 73 |
(and |
|---|
| 74 |
(not (atom RHS)) |
|---|
| 75 |
(equal (car LHS) (car RHS)) |
|---|
| 76 |
(Generalization_Of_ (cadr LHS) (cadr RHS) map) |
|---|
| 77 |
(Generalization_Of_ (caddr LHS) (caddr RHS) map) |
|---|
| 78 |
) |
|---|
| 79 |
) |
|---|
| 80 |
) |
|---|
| 81 |
|
|---|
| 82 |
#| |
|---|
| 83 |
(equal nil (Generalization_Of '(+ a (- c b)) '(+ a b))) |
|---|
| 84 |
(equal nil (Generalization_Of '(+ a (- c (/ b d))) '(+ a b))) |
|---|
| 85 |
(equal nil (Generalization_Of '(+ a (- c (/ b d))) '(+ a (- c b)) )) |
|---|
| 86 |
(equal t (Generalization_Of '(+ a (- c b)) '(+ a (- c (/ b d))))) |
|---|
| 87 |
(equal t (Generalization_Of '(+ a b) '(+ a (- c b)) )) |
|---|
| 88 |
(equal t (Generalization_Of '(+ a b) '(+ a (- c (/ b d))))) |
|---|
| 89 |
|# |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
;;;;;;;;;;;;;;;;;;;; |
|---|
| 93 |
;; inserter function using Generalization_Of as test |
|---|
| 94 |
;; |
|---|
| 95 |
;; Insert A into list L such that A will be after |
|---|
| 96 |
;; anything it is a generalization of |
|---|
| 97 |
;; |
|---|
| 98 |
(defun insert_By_generality (A L) |
|---|
| 99 |
(if (endp L) |
|---|
| 100 |
`(,A) |
|---|
| 101 |
(if (Generalization_Of (caar L) (car A)) |
|---|
| 102 |
(cons A L) |
|---|
| 103 |
(cons (car L) (insert_By_generality A (cdr L))) |
|---|
| 104 |
) |
|---|
| 105 |
) |
|---|
| 106 |
) |
|---|
| 107 |
;(insert_By_generality '(+ a (- c b)) '((+ a (- c (/ b d))) (+ a b) )) |
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
;;;;;;;;;;;;;;;;;;;;; |
|---|
| 111 |
;; insertion sort using insert_By_generality |
|---|
| 112 |
;; |
|---|
| 113 |
(defun Sort_By_generality (L) |
|---|
| 114 |
(if (endp L) |
|---|
| 115 |
'() |
|---|
| 116 |
(insert_By_generality (car L) (Sort_By_generality (cdr L)) ) |
|---|
| 117 |
) |
|---|
| 118 |
) |
|---|
| 119 |
|
|---|
| 120 |
#| |
|---|
| 121 |
(sort_By_generality |
|---|
| 122 |
'( |
|---|
| 123 |
((+ a b ) 're) |
|---|
| 124 |
((+ a (-> c (/> b d ))) 'rc) |
|---|
| 125 |
((+ a (-> (+ c a) b )) 'rb) |
|---|
| 126 |
((+ a (-> c (/> b (*> d e)))) 'ra) |
|---|
| 127 |
((+ a (-> c b )) 'rd) |
|---|
| 128 |
) |
|---|
| 129 |
) |
|---|
| 130 |
|# |
|---|