substitution - [(-) (sub (parse (second WAE)) (parse (third...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
#lang planet plai/plai (define-type WAE [num (n number?)] [add (lhs WAE?) (rhs WAE?)] [sub (lhs WAE?) (rhs WAE?)] [with (name symbol?) (named-expr WAE?) (body WAE?)] [id (name symbol? (define (calc expr) (type-case WAE expr [num (n) n] [add (l r) (+ (calc l) (calc r))] [sub (l r) (- (calc l) (calc r))] [with (bound-id named-expr bound-body) (calc (subst bound-body bound-id (num (calc named-expr))))] ;; 1. evalulate first this, [id (v) (error 'calc "free indentifier")])) (define (parse WAE) (cond [(number? WAE) (num WAE)] [(list? WAE) (case (first WAE) [(+) (add (parse (second WAE)) (parse (third WAE)))]
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: [(-) (sub (parse (second WAE)) (parse (third WAE)))] ) ] ) ) (define (subst expr sub-id val) (type-case WAE expr [num (n) expr] [add (l r) (add (subst l sub-id val) (subst r sub-id val))] [sub (l r) (sub (subst l sub-id val) (subst r sub-id val))] [with (bound-id named-expr bound-body) (if (symbol=? bound-id sub-id) (with bound-id (subst named-expr sub-id val) bound-body) ;; scoping rule (with bound-id (subst named-expr sub-id val) (subst bound-body sub-id val)))] ;; scoping rule (if same or not) [id (v) (if (symbol=? v sub-id) val expr)]))...
View Full Document

This document was uploaded on 02/12/2010.

Ask a homework question - tutors are online