Created
February 12, 2024 18:23
-
-
Save SHoltzen/b7db10c2f3cfe1c22335b51174a701d5 to your computer and use it in GitHub Desktop.
This file contains 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 | |
(define-type Exp | |
[addE (l : Exp) (r : Exp)] | |
[appendE (l : Exp) (r : Exp)] | |
[numE (n : Number)] | |
[iteE (g : Exp) (thn : Exp) (els : Exp)] | |
[stringE (s : String)]) | |
(define-type Value | |
[numV (n : Number)] | |
[stringV (s : String)]) | |
(define-type Type | |
[stringT] | |
[numT]) | |
(calc : (Exp -> Value)) | |
(define (calc e) | |
(type-case Exp e | |
[(numE n) (numV n)] | |
[(stringE s) (stringV s)] | |
[(addE l r ) (numV (+ (numV-n (calc l)) (numV-n (calc r))))] | |
[(iteE g thn els) | |
(if (eq? (numV-n (calc g)) 0) | |
(calc thn) | |
(calc els))] | |
[(appendE l r ) (stringV (string-append | |
(stringV-s (calc l)) | |
(stringV-s (calc r))))])) | |
(type-of : (Exp -> Type)) | |
(define (type-of e) | |
(type-case Exp e | |
[(numE n) (numT)] | |
[(stringE s) (stringT)] | |
[(addE l r) | |
(if (and (numT? (type-of l)) (numT? (type-of r))) | |
(numT) | |
(error 'type-error "tried to add non-numbers"))] | |
[(iteE g thn els) | |
(let [(t-g (type-of g)) | |
(t-thn (type-of thn)) | |
(t-els (type-of els))] | |
(if (and (equal? t-g (numT)) (equal? t-thn t-els)) | |
t-thn | |
(error 'type-error "Type error in if")))] | |
[(appendE l r) | |
(if (and (stringT? (type-of l)) (stringT? (type-of r))) | |
(stringT) | |
(error 'type-error "tried to append non-string")) | |
])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment