Last active
February 29, 2024 15:50
-
-
Save SHoltzen/1b863f1ab2cccdcf14cf3f592fc6ca63 to your computer and use it in GitHub Desktop.
Starter code for quiz 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #lang plait | |
| ; ------------- | |
| ; - Problem 1 - | |
| ; ------------- | |
| (define-type TinyExpr | |
| [boolTE (b : Boolean)] | |
| [numTE (n : Number)] | |
| [varTE (v : Symbol)] | |
| [addTE (e1 : TinyExpr) (e2 : TinyExpr)] | |
| [ifTE (guard : TinyExpr) (thn : TinyExpr) (els : TinyExpr)] | |
| [let1TE (id : Symbol) (e1 : TinyExpr) (e2 : TinyExpr)]) | |
| ; substitute e1[x |-> e2] | |
| (subst : (TinyExpr Symbol TinyExpr -> TinyExpr)) | |
| (define (subst e1 x e2) | |
| (type-case TinyExpr e1 | |
| [(varTE s) (if (symbol=? s x) | |
| e2 | |
| (varTE s))] | |
| [(numTE n) (numTE n)] | |
| [(addTE add1 add2) (addTE (subst add1 x e2) (subst add2 x e2))] | |
| [(ifTE g thn els) (ifTE (subst g x e2) (subst thn x e2) (subst els x e2))] | |
| [(boolTE b) (boolTE b)] | |
| [(let1TE id assgn body) | |
| (if (symbol=? x id) | |
| (let1TE id e1 e2) ; shadowing case | |
| (let1TE id (subst assgn x e2) (subst body x e2)))] | |
| )) | |
| ; interpret expressions | |
| (interp : (TinyExpr -> TinyExpr)) | |
| (define (interp e) | |
| (type-case TinyExpr e | |
| [(varTE s) (error 'runtime "unrecognized symbol")] | |
| [(numTE n) (numTE n)] | |
| [(boolTE b) (boolTE b)] | |
| [(addTE e1 e2) (numTE (+ (numTE-n (interp e1)) (numTE-n (interp e2))))] | |
| [(ifTE g thn els) | |
| (if (boolTE-b (interp g)) | |
| (interp thn) | |
| (interp els))] | |
| [(let1TE x e1 e2) | |
| (interp (subst e2 x (interp e1)))])) | |
| ; -------------- | |
| ; - Problem 1a - | |
| ; -------------- | |
| (define correct-1? (error 'not-implemented "write #t or #f!")) | |
| (define correct-2? (error 'not-implemented "write #t or #f!")) | |
| (define correct-3? (error 'not-implemented "write #t or #f!")) | |
| ; -------------- | |
| ; - Problem 1b - | |
| ; -------------- | |
| (problem-1b : TinyExpr) | |
| (define problem-1b (error 'not-implemented "implement me!")) | |
| ; -------------- | |
| ; - Problem 1c - | |
| ; -------------- | |
| (problem-1c : TinyExpr) | |
| (define problem-1c (error 'not-implemented "implement me!")) | |
| ; -------------- | |
| ; - Problem 1d - | |
| ; -------------- | |
| (define true-1? (error 'not-implemented "write #t or #f!")) | |
| (define true-2? (error 'not-implemented "write #t or #f!")) | |
| (define true-3? (error 'not-implemented "write #t or #f!")) | |
| (define true-4? (error 'not-implemented "write #t or #f!")) | |
| ; ------------- | |
| ; - Problem 2 - | |
| ; ------------- | |
| (define-type ImpExp | |
| [varE (v : Symbol)] | |
| [numE (n : Number)] | |
| [boolE (b : Boolean)] | |
| [ifE (g : ImpExp) (thn : ImpExp) (els : ImpExp)] | |
| [addE (e1 : ImpExp) (e2 : ImpExp)]) | |
| (define-type ImpStatement | |
| [seqS (s1 : ImpStatement) (s2 : ImpStatement)] | |
| [assignS (var : Symbol) (assignment : ImpExp)]) | |
| (define-type ImpProgram | |
| [prog (p : ImpStatement) (ret : ImpExp)]) | |
| (define-type Value | |
| [VBool (b : Boolean)] | |
| [VNum (n : Number)]) | |
| (define-type-alias Env (Boxof (Hashof Symbol Value))) | |
| (define (mt-env) (box (hash empty))) ;; "empty environment" | |
| ; lookup s in h | |
| (define (lookup (h : Env) (s : Symbol)) | |
| (type-case (Optionof Value) (hash-ref (unbox h) s) | |
| [(none) (error 'runtime "unrecognized symbol")] | |
| [(some v) v])) | |
| ; mutably update old-env by mapping new-name to value | |
| (define (update! (old-env : Env) (new-name : Symbol) (value : Value)) | |
| (set-box! old-env (hash-set (unbox old-env) new-name value))) | |
| ; convert a value into an integer or fail with a runtime error | |
| (define (value->int (v : Value)) | |
| (type-case Value v | |
| [(VBool b) (error 'runtime "invalid argument")] | |
| [(VNum n) n])) | |
| ; convert a value into a Boolean or fail with a runtime error | |
| (define (value->bool (v : Value)) | |
| (type-case Value v | |
| [(VBool b) b] | |
| [(VNum n) (error 'runtime "invalid argument")])) | |
| ; ------------- | |
| ; - Problem 2a - | |
| ; ------------- | |
| ; Implement interp-e | |
| ; Don't forget to follow the style guidelines! | |
| ; ------------- | |
| ; - Problem 2b - | |
| ; ------------- | |
| ; Implement interp-p | |
| ; Don't forget to follow the style guidelines! | |
| ; ------------- | |
| ; - Problem 2c - | |
| ; ------------- | |
| (define-type Type | |
| [TNum] | |
| [TBool]) | |
| ; Implement type-of-p | |
| ; Don't forget to follow the style guidelines! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment