Created
February 22, 2024 16:29
-
-
Save SHoltzen/1ba106e597f42608a3acbdea30ac97a6 to your computer and use it in GitHub Desktop.
Starter code for assignment 6
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
| ; assignment 6 | |
| #lang plait | |
| ;;;;;;;;;;;;; | |
| ; Problem 1 ; | |
| ;;;;;;;;;;;;; | |
| ; The grammar and types for our language | |
| (define-type Type | |
| [TNum] | |
| [TFun (arg : Type) (ret : Type)]) | |
| (define-type Exp | |
| [letrecE (s : Symbol) (t : Type) (e : Exp) (body : Exp)] | |
| [numE (n : Number)] | |
| [varE (s : Symbol)] | |
| [locE (n : Number)] | |
| [lamE (arg : Symbol) (t : Type) (body : Exp)] | |
| [appE (e1 : Exp) (e2 : Exp)]) | |
| ; (fresh-loc!) is a counter function for "allocating" new heap cells. | |
| (define counter (box 0)) | |
| (define (fresh-loc!) | |
| (begin | |
| (set-box! counter (+ (unbox counter) 1)) | |
| (unbox counter))) | |
| ; This is an interface for the heap we will be interacting with in our program. | |
| (define-type-alias MutableHeap (Boxof (Hashof Number Exp))) | |
| (define mt-heap (box (hash empty))) ;; "empty environment" | |
| (define (lookup (h : MutableHeap) (n : Number)) | |
| (type-case (Optionof Exp) (hash-ref (unbox h) n) | |
| [(none) (error 'type-error "unrecognized symbol")] | |
| [(some v) v])) | |
| (extend : (MutableHeap Number Exp -> Void)) | |
| (define (extend old-env new-name value) | |
| (set-box! old-env (hash-set (unbox old-env) new-name value))) | |
| ; We will also give you substitution. | |
| (subst : (Exp Symbol Exp -> Exp)) | |
| (define (subst e1 x e2) | |
| (type-case Exp e1 | |
| [(varE s) (if (symbol=? s x) | |
| e2 | |
| (varE s))] | |
| [(numE n) (numE n)] | |
| [(locE e) (locE e)] | |
| [(letrecE s t e body) | |
| (if (symbol=? s x) | |
| (letrecE s t e body) | |
| (letrecE s t (subst e x e2) (subst body x e2)))] | |
| [(lamE id typ body) | |
| (if (symbol=? x id) | |
| (lamE id typ body) ; shadowing case | |
| (lamE id typ (subst body x e2)))] | |
| [(appE e1App e2App) | |
| (appE (subst e1App x e2) | |
| (subst e2App x e2))])) | |
| (interp : (MutableHeap Exp -> Exp)) | |
| (define (interp heap e) | |
| (type-case Exp e | |
| [(varE s) (error 'runtime "unbound symbol")] | |
| [(lamE id typ body) (lamE id typ body)] | |
| [(numE n) (numE n)] | |
| [(locE n) | |
| (error 'implement-me "problem 1a")] | |
| [(letrecE x t e body) | |
| (error 'implement-me "problem 1a")] | |
| [(appE e1 e2) | |
| ; run e1 to get (lambda (id) body) | |
| ; run e2 to get a value argV | |
| ; run body[id |-> v] | |
| (letrec [(e1V (interp heap e1)) | |
| (body (lamE-body e1V)) | |
| (id (lamE-arg e1V)) | |
| (argV (interp heap e2))] | |
| (interp heap (subst body id argV)))])) | |
| ; Here's the looping program using letrec we saw at the beginning of the problem write-up. | |
| (define loopy | |
| (letrecE 'f (TFun (TNum) (TNum)) (lamE 'x (TFun (TNum) (TNum)) (appE (varE 'f) (varE 'x))) | |
| (appE (varE 'f) (numE 10)))) | |
| ; The below test, when implemented correctly, should run forever... | |
| ; (interp mt-env loopy) | |
| ;;;;;;;;;;;;; | |
| ; Problem 2 ; | |
| ;;;;;;;;;;;;; | |
| (define-type VExpr | |
| [let1E (x : Symbol) (assgn : VExpr) (body : VExpr)] | |
| [getvecE (e : VExpr) (index : Number)] | |
| [vvecE (v : (Vectorof Number))] | |
| [vnumE (n : Number)] | |
| [vvarE (s : Symbol)]) | |
| ; Problem 2a | |
| (interpv : (VExpr -> VExpr)) | |
| (define (interpv e) | |
| (error 'implement-me "")) | |
| ; Problem 2b | |
| ; ... your test/exn tests here ... | |
| ; We define below the types you will be working with | |
| ; in our language, as well as the typing environment (context) | |
| ; and helper functions. | |
| (define-type VType | |
| [TVNum] | |
| [TVec (l : Number)]) | |
| (define-type-alias TEnv (Hashof Symbol VType)) | |
| (define mt-env (hash empty)) ;; "empty environment" | |
| (define (lookup-type (n : TEnv) (s : Symbol)) | |
| (type-case (Optionof VType) (hash-ref n s) | |
| [(none) (error 'type-error "unrecognized symbol")] | |
| [(some v) v])) | |
| (extend-tenv : (TEnv Symbol VType -> TEnv)) | |
| (define (extend-tenv old-env new-name value) | |
| (hash-set old-env new-name value)) | |
| ; Problem 2c | |
| (type-of : (TEnv VExpr -> VType)) | |
| (define (type-of tenv e) | |
| (error 'implement-me "")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment