;; Nada Amin (namin@mit.edu) ;; 6.945 Problem Set 4 ;; Due: Wed. 5 Mar. 2008 (load "../test-manager/load.scm") (load "load.scm") ;; Problem 4.1 #| The ordering restrictions is necessary in the rule for the commutative law, because it ensures the rule is monotonic. Without this restriction, the rule would still apply every time it is applied, and the rule system will loop infinitely, constantly switching the terms in the multiplication. |# #| If the commutative laws did not force an odering, we would have to write the numerical simplification rules using segments to account that are not of interest. For example, the rules (rule (+ 0 (?? x)) none (+ (?? x))) (rule (+ (? x number?) (? y number?) (?? z)) none (+ (? (+ x y)) (?? z))) would have to be written (rule (+ (?? a) 0 (?? b)) none (+ (?? a) (?? b))) (rule (+ (?? a) (? x number?) (?? b) (? y number?) (?? c)) none (+ (? (+ x y)) (?? a) (?? b) (?? c))) Numerical simplification would become very expensive, because matching segments is expensive as it involves lots of backtracking. Every segment could take O(n) to match, and so the second new rule could take O(n^3) before failing to match. |# ;; Problem 4.3 #| I could make a more efficient sort by by-passing the rule system, using the predicate to check if the expression is sorted, and, if not, sorting it in the skeleton. (rule (* (?? s)) (and (length>1 s) (not (sorted? expr1 and sorted? have the obvious following definitions: |# (define (length>1 s) (and (not (null? s)) (not (null? (cdr s))))) (define (sorted? cmp s) (if (not (length>1 s)) #t (and (not (cmp (cadr s) (car s))) (sorted? cmp (cdr s))))) (define algebra-2bis (rule-simplifier (list ;; Sums (rule (+ (? a)) none (? a)) (rule (+ (?? a) (+ (?? b))) none (+ (?? a) (?? b))) (rule (+ (+ (?? a)) (?? b)) none (+ (?? a) (?? b))) (rule (+ (?? s)) (and (length>1 s) (not (sorted? expr1 s) (not (sorted? expr