diff options
Diffstat (limited to 'stlc-ref.rkt')
-rw-r--r-- | stlc-ref.rkt | 38 |
1 files changed, 3 insertions, 35 deletions
diff --git a/stlc-ref.rkt b/stlc-ref.rkt index a3b6a90..29f4527 100644 --- a/stlc-ref.rkt +++ b/stlc-ref.rkt @@ -22,7 +22,8 @@ ; references [`(ref ,id) `(ref ,id)] ; refs. pointer to immutable bound value [`(deref ,id) ; deref. accesses a stored reference from the heap - (interpret- (dict-get heap id) ctx heap)] + #:when (dict-has-key? heap id) + (interpret- (dict-ref heap id) ctx heap)] [`(set ,id ,expr ,body) ; set. arbitrarily updates a reference to point elsewhere ; note: MUST resolve the binding before applying!! !! (interpret- body ctx (dict-set heap id (interpret- expr ctx heap)))] @@ -38,42 +39,9 @@ ; desugaring and error handling [`(let ,id ,expr ,body) (interpret- `((λ ,id ,body) ,expr) ctx heap)] + [`(deref ,id) (err (format "attempting to deref unknown reference ~a" id))] [expr (err (format "interpreting an unknown expression ~a" expr))])) -;; removes typing annotations. -(define (strip expr) - (match expr - [`(,a ,b) `(,(strip a) ,(strip b))] - [`(λ ,id ,body) `(λ ,id ,(strip body))] - [`(let ,id ,expr ,body) `(let ,id ,(strip expr) ,(strip body))] - [`(λ ,id (: ,type) ,body) (strip `(λ ,id ,body))] - [`(let ,id (: ,type) ,expr ,body) (strip `(let ,id ,expr ,body))] - [expr expr])) - -(define (dict-get dict key) - (dict-ref dict key (λ () (err (format "identifier ~a not found" key))))) - -(define (fmt expr) - (match expr - [`(λ ,id ,body) (format "λ~a.[~a]" id (fmt body))] - [`((λ ,id ,body) ,arg) (format "~a = ~a; ~a" id (fmt arg) (fmt body))] - [`(λ ,id (: ,type) ,body) (format "~a: ~a; ~a" id (fmt type) (fmt body))] - [`((λ ,id (: ,type) ,body) ,arg) (format "~a: ~a; ~a = ~a; ~a" id (fmt type) id (fmt arg) (fmt body))] - [`(λ ,id ,body ,ctx) (format "λ~a.[~a]" id (fmt body))] - [`(let ,id ,expr ,body) (format "~a = ~a; ~a" id (fmt expr) (fmt body))] - [`(let ,id (: ,type) ,expr ,body) (format "~a: ~a; ~a = ~a; ~a" id (fmt type) id (fmt expr) (fmt body))] - [`(set ,id ,expr ,body) (format "~a := ~a; ~a" id (fmt expr) (fmt body))] - [`(→ ,a ,b) (format "(~a → ~a)" (fmt a) (fmt b))] - [`(Ref ,a) (format "Ref ~a" (fmt a))] - [`(ref ,a) (format "&~a" (fmt a))] - [`(deref ,a) (format "*~a" (fmt a))] - [`(,a ,b) (format "(~a ~a)" (fmt a) (fmt b))] - [(hash-table) "{}"] - [(hash-table (k v)) (format "{~a: ~a}" (fmt k) (fmt v))] - [(hash-table (k1 v1) (k2 v2)) (format "{~a: ~a; ~a: ~a}" (fmt k1) (fmt v1) (fmt k2) (fmt v2))] - [(hash-table (k1 v1) (k2 v2) (k3 v3)) (format "{~a: ~a; ~a: ~a; ~a: ~a}" (fmt k1) (fmt v1) (fmt k2) (fmt v2) (fmt k3) (fmt v3))] - [expr (format "~a" expr)])) - ; simple diverging program in STLC-ref ; https://www.youtube.com/watch?v=XNgE8kBfSz8 (interpret ' |