This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: 1 9/19/06 From materials Copyright N. Ramsey & al. 1 DPL3a: MetaCircular Evaluator & Continuation Programming Two examples: 1. a MetaCircular Evaluator (a fancy way of describing an implementation of Scheme written in Scheme and using the Scheme interpreter for execution); 2. a Logic Solver (more specically a CNF solver) that uses continuations to simulate backtracking. 9/19/06 From materials Copyright N. Ramsey & al. 2 DPL3a: MetaCircular Evaluator How can you write a quick (and easily extensible) Scheme interpreter? The concrete syntax of Scheme is easy; a rudimentary interpreter should be easy to set up; and to modify. Make use of continuations to signal exceptions (a trivial case)... Trick: You can use much of the underlying Scheme - only some parts need to be implemented. For 4 other interpreters written in full Scheme, see http://www. cs . uml . edu/~giam/DPL/Code/SchemeCode/ 9/19/06 From materials Copyright N. Ramsey & al. 3 DPL3a: MetaCircular Evaluator We need to manage explicitly the environment and the store. 1. The environment will start empty, and acquire the primitives. 2. The store will hold the values; the environment will hold the names 3. They will be joined by locations: name a location a value (val emptystore '((next 0))) ; first (loc value) pair ; used for administration. (val sigma emptystore) ; only header pair We now need to construct the store management functions. 9/19/06 From materials Copyright N. Ramsey & al. 4 DPL3a: MetaCircular Evaluator (define load (l) (find-c l sigma (lambda(x) x) ;;; note continuations (lambda () (error (list2 'unbound-location: l))))) (define store (l v) (begin (set sigma (bind l v sigma)) v)) (define allocate (value) ;;; get space and store there (let* ((loc (load 'next))) ; find next available location (begin (store 'next (+ loc 1)); increment next available (store loc value) ; store value in location loc))) ; return location 9/19/06 From materials Copyright N. Ramsey & al. 5 DPL3a: MetaCircular Evaluator Where bind and find-c are given by: (define bind (x y alist) ;;; BUILT-IN to Scheme (if (null? alist) (list1 (list2 x y)) (if (= x (caar alist)) (cons (list2 x y) (cdr alist)) (cons (car alist) (bind x y (cdr alist)))))) (define find-c (key alist success-cont failure-cont) (letrec ;;; BUILT-IN to Scheme ((search (lambda (alist) (if (null? alist) (failure-cont) (if (equal? key (caar alist)) (success-cont (cadar alist)) (search (cdr alist))))))) (search alist))) 9/19/06 From materials Copyright N. Ramsey & al. 6 DPL3a: MetaCircular Evaluator Note that bind will change the second element of a pair in an association list - OK for store, not OK for environment, which is supposed to be immutable. Note how this is handled......
View Full Document
This note was uploaded on 02/13/2012 for the course CS 91.531 taught by Professor Giam during the Fall '09 term at UMass Lowell.
- Fall '09