[plt-scheme] writing an evaluation function/interpreter; type-case
On Wed, Feb 17, 2010 at 3:21 PM, ih <irvin.hwang at gmail.com> wrote:
> Hi,
> I'm trying to follow Ch. 4 of Types and Programming Languages by
> Benjamin Pierce and implement the language
>
> t:= true | false | if t then t else t
> OR using define-type
> (define-type term
> [TmTrue (t (lambda (a) (equal? a #t)))]
> [TmFalse (f (lambda (a) (equal? a #f)))]
> [TmIf (t1 term?) (t2 term?) (t3 term?)])
>
> I was trying to use the forms define-type and type-case from PLAI by
> Shriram Krishnamurthi, but type-case doesn't seem to have the same
> pattern matching functionality as is used Pierce's ocaml
> implementation i.e. I can't write
>
> (define (eval1 e)
> (type-case term e
> [TmTrue (t) t]
> [TmFalse(f) f]
> [TmIf (TmTrue t2 t3) (eval1 t2)]
> [TmIf (TmFalse t2 t3) (eval1 t3)]))
>
> I was trying to use "match", but it doesn't seem to be defined to work
> with the algebraic data type defined with define-type (or perhaps I am
> using match incorrectly).
>
> (define (eval1 e)
> (type-case term e
> [TmTrue (t) t]
> [TmFalse(f) f]
> [TmIf (t1 t2 t3) (match t1)
> ]
>
>
>
> Can anyone give guidance on the proper way to implement this in
> scheme?
You can push the test down one level.
Or you can use match and define-struct.
> Should I be using match with define-type?
They do not work together.
>
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
--
Jay McCarthy <jay at cs.byu.edu>
Assistant Professor / Brigham Young University
http://teammccarthy.org/jay
"The glory of God is Intelligence" - D&C 93