I was reading Shriram's PLAI and this I got stuck in these questions:
Can you prove that the eager and lazy regimes will always produce the same answer? (Shriram asks to look at the language he developed but is there another way to prove this and how?)
Will eager evaluation always reduce with fewer steps?
Here is the Code of Substitution and Eager Evaluation in Clojure.
;; Gets W-lang and returns the computed number
;; (evaluate (let x (num 10) (let y x x)) -> 10
;; (evaluate (let x (num 10) (let y (add x (num 10)) y)) ->20
;; (evaluate (let x 10 (let x x x ))) -> 10
;; (evaluate (let x 10 (let x (+ 10 x)
;; (let y (let y (+ 10 x) y)
;; (+ x y))))-> 50
(defn evaluate[W-lang]
(condp = (first W-lang)
'num (second W-lang)
'add (+ (evaluate (second W-lang))
(evaluate (third W-lang)))
'sub (- (evaluate (second W-lang))
(evaluate (third W-lang)))
'let (evaluate (subst (second W-lang)(third W-lang)
(forth W-lang)))))
;; subst: symbol value W-Lang -> W-lang
;; substitutes the symbol and returns a W-Lang
(defn subst[id value W-lang]
(cond
(symbol? W-lang) (if (= id W-lang) value W-lang)
(seq? W-lang)
(condp = (first W-lang)
'num (list 'num (first (rest W-lang)))
'add (list 'add (subst id value (second W-lang))
(subst id value (third W-lang)))
'sub (list 'sub (subst id value (second W-lang))
(subst id value (third W-lang)))
'let
(if (= (second W-lang) id)
(list 'let (second W-lang)
(subst id value (third W-lang))
(forth W-lang))
(list 'let(second W-lang)
(subst id value (third W-lang))
(subst id value (forth W-lang))))
W-lang)))