Computer Science

I have defined a microscopic verson of scheme that utilizes amb. I am currently using Dr. Scheme.

This is my definition:
(define (amb-eval s environment succeed fail)
(cond ((not (pair? s))
(succeed
(cond ((eq? s '#t) '#t)
((eq? s '()) '())
((eq? s '#f) '#f)
((number? s) s)
((string? s) s)
(else (micro-value s environment)))
fail))
((eq? (car s) 'quote) (succeed (cadr s) fail))
((eq? (car s) 'cond)
(micro-evalcond (cdr s) environment succeed fail))
((eq? (car s) 'set!)
(micro-set! (cadr s) (caddr s) environment succeed fail))
((eq? (car s) 'begin)
(micro-evalbegin (cdr s) environment succeed fail))
((eq? (car s) 'define)
(amb-eval (definition-val s)
environment
(lambda (val fail)
(micro-define-var (definition-var s) val environment)
(succeed 'ok fail))
fail))
((eq? (car s) 'unknown) (fail))
((eq? (car s) 'amb)
(micro-amb (cdr s) environment succeed fail))
((eq? (car s) 'lambda)
(succeed
(list 'procedure (cadr s) (cons 'begin (cddr s)) environment)
fail))
(else
(amb-eval (car s)
environment
(lambda (proc fail)
(get-args (cdr s)
environment
(lambda (args fail)
(micro-apply proc
args
environment
succeed
fail))
fail))
fail))))

(define (get-args arglist env succeed fail)
(if (null? arglist)
(succeed () fail)
(amb-eval (car arglist)
env
(lambda (arg fail)
(get-args (cdr arglist)
env
(lambda (args fail)
(succeed (cons arg args) fail))
fail))
fail)))

(define (micro-apply function args environment succeed fail)
(cond ((not (pair? function))
(display "ERROR: ")
(display function)
(display " is not a function")
(newline)
(fail))
((eq? (car function) 'primitive)
(succeed (apply (cadr function) args) fail))
((eq? (car function) 'procedure)
(amb-eval (caddr function)
(micro-bind (cadr function)
args
(cadddr function))
succeed
fail))))

(define (micro-amb choices env succeed fail)
(if (null? choices)
(fail)
(amb-eval (car choices)
env
succeed
(lambda ()
(micro-amb (cdr choices)
env
succeed
fail)))))

(define global-frame
(map (lambda (x) (list x 'primitive (eval x)))
'(car cdr cons pair? not exit null? eq? * +)))

(define global-environment (list global-frame))

(define (micro-r-e-p)
(define (main-loop try-again)
(newline)
(display "ambmicro> ")
(let ((s (read)))
(cond ((equal? s '(exit))
(display "Leaving ambmicro")
(newline))
((eq? s 'try-again) (try-again))
(else
(display "new problem")
(newline)
(amb-eval s
global-environment
(lambda (val next-try)
(micro-print val)
(main-loop next-try))
(lambda ()
(display "no more values for ")
(micro-print s)
(micro-r-e-p)))))))
(main-loop (lambda ()
(display "no current problem")
(newline)
(micro-r-e-p))))

(define (micro-evalcond clauses environment succeed fail)
(cond ((null? clauses) (succeed #f fail))
((eq? (caar clauses) 'else)
(micro-evalbegin (cdar clauses) environment succeed fail))
(else (amb-eval (caar clauses)
environment
(lambda (val fail)
(if val
(micro-evalbegin (cdar clauses)
environment
succeed
fail)
(micro-evalcond (cdr clauses)
environment
succeed
fail)))
fail))))

; An environment is a list of frames. A frame is a list of var-value
; pairs (an assoc list).

(define (micro-bind key-list value-list environment)
(define (micro-binder key-list value-list) ; returns a new frame
(cond ((or (null? key-list) (null? value-list))
'())
(else (cons (cons (car key-list) (car value-list))
(micro-binder (cdr key-list)
(cdr value-list))))))
(cons (micro-binder key-list value-list) environment))

(define (micro-get-var-val var environment)
(if (null? environment)
#f
(let ((vv (assoc var (car environment))))
(if vv
vv
(micro-get-var-val var (cdr environment))))))

(define (micro-value var environment)
(let ((vv (micro-get-var-val var environment)))
(if vv
(cdr vv)
(begin
(display "ERROR: No value for ")
(display var)
(display " found")
(newline)
'unknown))))

(define (micro-set! var val env succeed fail)
(let ((entry (micro-get-var-val var env)))
(if entry
(let ((old-val (cdr entry)))
(amb-eval val
env
(lambda (val fail)
(set-cdr! entry val)
(succeed 'ok
(lambda ()
(set-cdr! entry old-val)
(fail))))
fail))
(begin
(display "ERROR: variable ")
(display var)
(display " not previously defined")
(newline)
(fail)))))

(define (micro-evalbegin exprs env succeed fail)
(if (null? (cdr exprs))
(amb-eval (car exprs) env succeed fail)
(amb-eval (car exprs)
env
(lambda (val fail)
(micro-evalbegin (cdr exprs)
env
succeed
fail))
fail)))

(define (definition-var s)
(if (symbol? (cadr s))
(cadr s)
(caadr s)))

(define (definition-val s)
(if (symbol? (cadr s))
(caddr s)
(cons 'lambda (cons (cdadr s) (cddr s)))))

(define (micro-define-var var val env)
(let ((vv (assoc var (car env)))) ; search only first frame
(if vv
(set-cdr! vv val)
(set-car! env (cons (cons var val) (car env))))))

(define (micro-print x)
(cond ((not (pair? x)) (display x))

((eq? (car x) 'procedure)
(display "(procedure ")
(micro-print-help (list (cadr x) (caddr x)))
(display " <an environment>)"))

(else (display "(")
(micro-print-help x)
(display ")"))))

(define (micro-print-help x)
(if (pair? x)
(begin (micro-print (car x))
(if (pair? (cdr x))
(display " ")
(if (not (null? (cdr x))) (display " . ")))
(micro-print-help (cdr x)))
(if (not (null? x)) (display x))))

I am looking to add a way to implement require.

Require can be implemented while I'm running ambmicro but has to be re-entered everytime I use ambmicro. I'm looking to define it in ambeval but am unsure how.

Any help/suggestions would be helpful.
Jake

  1. 👍 0
  2. 👎 0
  3. 👁 163

Respond to this Question

First Name

Your Response

Similar Questions

  1. History

    Which accurately describes Newton’s Laws? newton defined the three laws of motion that govern planetary bodies and objects on earth newton defined three laws of thermodynamics that predict the conservation of energy newton

  2. Organic Chemistry

    Separation scheme for isolating benzoic acid Could you please show me a separation scheme for isolating benzoic acid from a reaction mixture if mixing a Grignard reagent phenylmagnesium bromide with dry ice (CO2) in ether? Thanks!

  3. Astronomy

    Imagine that Earth did not turn on its axis. (a) What would be true about the ecliptic? It would now define the day rather than the year. It would still be defined and could be seen from all points on Earth, but only when they

  4. English (Poetry)

    "From The Wave" by Thom Gunn How does Gunn's use of rhyme impact the poem? A. The alternating rhyming lines mirror the actions of waves. B. The rhyming scheme is used to show that waves are isolated events. C. The rhyming scheme

  1. math help

    in 2003 brijesh paid a tax of 20% of his salary. in 2004 his salary increased by 93×3/4 % and the tax scheme changed. under the new tax scheme he had to pay a fixed sum of rs.1000 and an additional 20% on the amount above rs

  2. Programming C#

    Write a program that prompts the user for a name, social security number, hourly pay rate , and the number of hours worked. - Gross pay defined as hourly pay rate time hours worked. - Federal withholding tax, defined 15% of the

  3. Grammar

    Which of these words contain an inflectional morpheme? 1. Successful 2. Acted Answer "Acted" A. Is this a correct statement? A syllable is defined by it's sound, but a porpheme is defined by it's meaning Answer "yes"

  4. math

    In parallelogram ABCD,M is the mid point of diagonal BD;if BM bisects angle ABC,then what is the measurement of angle AMB?

  1. American Literature

    How does Franklin's scheme for moral perfection compare with the self-help books available today, in general? Well Franklin's scheme is similar to self-help because both are for your benefit, making you a better person in life, to

  2. PSYCHOLOGY IN SOCIAL LIFE

    A planning committee has decided to determine the zoning of a particular area based on data provided by the town engineer. Which social decision scheme has the committee adopted? A. Majority-wins scheme B. First-shift rule C.

  3. English

    Which of the following do Petrarchan and Shakespearean sonnets have in common? A. rhyme scheme B. length C. question and answer format D. Both are organized as an octet and a sestet I chose A. rhyme scheme. Am I Correct?

  4. English 10-Check/Help

    I need help with the rhyme scheme and rhythm pattern. "It was many and many a year agoIn a kingdom by the sea,That a maiden there lived whom you may knowBy the name of ANNABEL LEE;And this maiden she lived with no other

You can view more similar questions or ask a new question.