| 1 |
;;; DYNAMIC -- Obtained from Andrew Wright. |
| 2 |
|
| 3 |
;; Fritz's dynamic type inferencer, set up to run on itself |
| 4 |
;; (see the end of this file). |
| 5 |
|
| 6 |
|
| 7 |
;---------------------------------------------------------------------------- |
| 8 |
; Environment management |
| 9 |
;---------------------------------------------------------------------------- |
| 10 |
|
| 11 |
;; environments are lists of pairs, the first component being the key |
| 12 |
|
| 13 |
;; general environment operations |
| 14 |
;; |
| 15 |
;; empty-env: Env |
| 16 |
;; gen-binding: Key x Value -> Binding |
| 17 |
;; binding-key: Binding -> Key |
| 18 |
;; binding-value: Binding -> Value |
| 19 |
;; binding-show: Binding -> Symbol* |
| 20 |
;; extend-env-with-binding: Env x Binding -> Env |
| 21 |
;; extend-env-with-env: Env x Env -> Env |
| 22 |
;; lookup: Key x Env -> (Binding + False) |
| 23 |
;; env->list: Env -> Binding* |
| 24 |
;; env-show: Env -> Symbol* |
| 25 |
|
| 26 |
|
| 27 |
; bindings |
| 28 |
|
| 29 |
(define gen-binding cons) |
| 30 |
; generates a type binding, binding a symbol to a type variable |
| 31 |
|
| 32 |
(define binding-key car) |
| 33 |
; returns the key of a type binding |
| 34 |
|
| 35 |
(define binding-value cdr) |
| 36 |
; returns the tvariable of a type binding |
| 37 |
|
| 38 |
(define (key-show key) |
| 39 |
; default show procedure for keys |
| 40 |
key) |
| 41 |
|
| 42 |
(define (value-show value) |
| 43 |
; default show procedure for values |
| 44 |
value) |
| 45 |
|
| 46 |
(define (binding-show binding) |
| 47 |
; returns a printable representation of a type binding |
| 48 |
(cons (key-show (binding-key binding)) |
| 49 |
(cons ': (value-show (binding-value binding))))) |
| 50 |
|
| 51 |
|
| 52 |
; environments |
| 53 |
|
| 54 |
(define dynamic-empty-env '()) |
| 55 |
; returns the empty environment |
| 56 |
|
| 57 |
(define (extend-env-with-binding env binding) |
| 58 |
; extends env with a binding, which hides any other binding in env |
| 59 |
; for the same key (see dynamic-lookup) |
| 60 |
; returns the extended environment |
| 61 |
(cons binding env)) |
| 62 |
|
| 63 |
(define (extend-env-with-env env ext-env) |
| 64 |
; extends environment env with environment ext-env |
| 65 |
; a binding for a key in ext-env hides any binding in env for |
| 66 |
; the same key (see dynamic-lookup) |
| 67 |
; returns the extended environment |
| 68 |
(append ext-env env)) |
| 69 |
|
| 70 |
(define dynamic-lookup (lambda (x l) (assv x l))) |
| 71 |
; returns the first pair in env that matches the key; returns #f |
| 72 |
; if no such pair exists |
| 73 |
|
| 74 |
(define (env->list e) |
| 75 |
; converts an environment to a list of bindings |
| 76 |
e) |
| 77 |
|
| 78 |
(define (env-show env) |
| 79 |
; returns a printable list representation of a type environment |
| 80 |
(map binding-show env)) |
| 81 |
;---------------------------------------------------------------------------- |
| 82 |
; Parsing for Scheme |
| 83 |
;---------------------------------------------------------------------------- |
| 84 |
|
| 85 |
|
| 86 |
;; Needed packages: environment management |
| 87 |
|
| 88 |
;(load "env-mgmt.ss") |
| 89 |
;(load "pars-act.ss") |
| 90 |
|
| 91 |
;; Lexical notions |
| 92 |
|
| 93 |
(define syntactic-keywords |
| 94 |
;; source: IEEE Scheme, 7.1, <expression keyword>, <syntactic keyword> |
| 95 |
'(lambda if set! begin cond and or case let let* letrec do |
| 96 |
quasiquote else => define unquote unquote-splicing)) |
| 97 |
|
| 98 |
|
| 99 |
;; Parse routines |
| 100 |
|
| 101 |
; Datum |
| 102 |
|
| 103 |
; dynamic-parse-datum: parses nonterminal <datum> |
| 104 |
|
| 105 |
(define (dynamic-parse-datum e) |
| 106 |
;; Source: IEEE Scheme, sect. 7.2, <datum> |
| 107 |
;; Note: "'" is parsed as 'quote, "`" as 'quasiquote, "," as |
| 108 |
;; 'unquote, ",@" as 'unquote-splicing (see sect. 4.2.5, p. 18) |
| 109 |
;; ***Note***: quasi-quotations are not permitted! (It would be |
| 110 |
;; necessary to pass the environment to dynamic-parse-datum.) |
| 111 |
(cond |
| 112 |
((null? e) |
| 113 |
(dynamic-parse-action-null-const)) |
| 114 |
((boolean? e) |
| 115 |
(dynamic-parse-action-boolean-const e)) |
| 116 |
((char? e) |
| 117 |
(dynamic-parse-action-char-const e)) |
| 118 |
((number? e) |
| 119 |
(dynamic-parse-action-number-const e)) |
| 120 |
((string? e) |
| 121 |
(dynamic-parse-action-string-const e)) |
| 122 |
((symbol? e) |
| 123 |
(dynamic-parse-action-symbol-const e)) |
| 124 |
((vector? e) |
| 125 |
(dynamic-parse-action-vector-const (map dynamic-parse-datum (vector->list e)))) |
| 126 |
((pair? e) |
| 127 |
(dynamic-parse-action-pair-const (dynamic-parse-datum (car e)) |
| 128 |
(dynamic-parse-datum (cdr e)))) |
| 129 |
(else (fatal-error 'dynamic-parse-datum "Unknown datum: ~s" e)))) |
| 130 |
|
| 131 |
|
| 132 |
; VarDef |
| 133 |
|
| 134 |
; dynamic-parse-formal: parses nonterminal <variable> in defining occurrence position |
| 135 |
|
| 136 |
(define (dynamic-parse-formal f-env e) |
| 137 |
; e is an arbitrary object, f-env is a forbidden environment; |
| 138 |
; returns: a variable definition (a binding for the symbol), plus |
| 139 |
; the value of the binding as a result |
| 140 |
(if (symbol? e) |
| 141 |
(cond |
| 142 |
((memq e syntactic-keywords) |
| 143 |
(fatal-error 'dynamic-parse-formal "Illegal identifier (keyword): ~s" e)) |
| 144 |
((dynamic-lookup e f-env) |
| 145 |
(fatal-error 'dynamic-parse-formal "Duplicate variable definition: ~s" e)) |
| 146 |
(else (let ((dynamic-parse-action-result (dynamic-parse-action-var-def e))) |
| 147 |
(cons (gen-binding e dynamic-parse-action-result) |
| 148 |
dynamic-parse-action-result)))) |
| 149 |
(fatal-error 'dynamic-parse-formal "Not an identifier: ~s" e))) |
| 150 |
|
| 151 |
; dynamic-parse-formal* |
| 152 |
|
| 153 |
(define (dynamic-parse-formal* formals) |
| 154 |
;; parses a list of formals and returns a pair consisting of generated |
| 155 |
;; environment and list of parsing action results |
| 156 |
(letrec |
| 157 |
((pf* |
| 158 |
(lambda (f-env results formals) |
| 159 |
;; f-env: "forbidden" environment (to avoid duplicate defs) |
| 160 |
;; results: the results of the parsing actions |
| 161 |
;; formals: the unprocessed formals |
| 162 |
;; Note: generates the results of formals in reverse order! |
| 163 |
(cond |
| 164 |
((null? formals) |
| 165 |
(cons f-env results)) |
| 166 |
((pair? formals) |
| 167 |
(let* ((fst-formal (car formals)) |
| 168 |
(binding-result (dynamic-parse-formal f-env fst-formal)) |
| 169 |
(binding (car binding-result)) |
| 170 |
(var-result (cdr binding-result))) |
| 171 |
(pf* |
| 172 |
(extend-env-with-binding f-env binding) |
| 173 |
(cons var-result results) |
| 174 |
(cdr formals)))) |
| 175 |
(else (fatal-error 'dynamic-parse-formal* "Illegal formals: ~s" formals)))))) |
| 176 |
(let ((renv-rres (pf* dynamic-empty-env '() formals))) |
| 177 |
(cons (car renv-rres) (reverse (cdr renv-rres)))))) |
| 178 |
|
| 179 |
|
| 180 |
; dynamic-parse-formals: parses <formals> |
| 181 |
|
| 182 |
(define (dynamic-parse-formals formals) |
| 183 |
;; parses <formals>; see IEEE Scheme, sect. 7.3 |
| 184 |
;; returns a pair: env and result |
| 185 |
(letrec ((pfs (lambda (f-env formals) |
| 186 |
(cond |
| 187 |
((null? formals) |
| 188 |
(cons dynamic-empty-env (dynamic-parse-action-null-formal))) |
| 189 |
((pair? formals) |
| 190 |
(let* ((fst-formal (car formals)) |
| 191 |
(rem-formals (cdr formals)) |
| 192 |
(bind-res (dynamic-parse-formal f-env fst-formal)) |
| 193 |
(bind (car bind-res)) |
| 194 |
(res (cdr bind-res)) |
| 195 |
(nf-env (extend-env-with-binding f-env bind)) |
| 196 |
(renv-res* (pfs nf-env rem-formals)) |
| 197 |
(renv (car renv-res*)) |
| 198 |
(res* (cdr renv-res*))) |
| 199 |
(cons |
| 200 |
(extend-env-with-binding renv bind) |
| 201 |
(dynamic-parse-action-pair-formal res res*)))) |
| 202 |
(else |
| 203 |
(let* ((bind-res (dynamic-parse-formal f-env formals)) |
| 204 |
(bind (car bind-res)) |
| 205 |
(res (cdr bind-res))) |
| 206 |
(cons |
| 207 |
(extend-env-with-binding dynamic-empty-env bind) |
| 208 |
res))))))) |
| 209 |
(pfs dynamic-empty-env formals))) |
| 210 |
|
| 211 |
|
| 212 |
; Expr |
| 213 |
|
| 214 |
; dynamic-parse-expression: parses nonterminal <expression> |
| 215 |
|
| 216 |
(define (dynamic-parse-expression env e) |
| 217 |
(cond |
| 218 |
((symbol? e) |
| 219 |
(dynamic-parse-variable env e)) |
| 220 |
((pair? e) |
| 221 |
(let ((op (car e)) (args (cdr e))) |
| 222 |
(case op |
| 223 |
((quote) (dynamic-parse-quote env args)) |
| 224 |
((lambda) (dynamic-parse-lambda env args)) |
| 225 |
((if) (dynamic-parse-if env args)) |
| 226 |
((set!) (dynamic-parse-set env args)) |
| 227 |
((begin) (dynamic-parse-begin env args)) |
| 228 |
((cond) (dynamic-parse-cond env args)) |
| 229 |
((case) (dynamic-parse-case env args)) |
| 230 |
((and) (dynamic-parse-and env args)) |
| 231 |
((or) (dynamic-parse-or env args)) |
| 232 |
((let) (dynamic-parse-let env args)) |
| 233 |
((let*) (dynamic-parse-let* env args)) |
| 234 |
((letrec) (dynamic-parse-letrec env args)) |
| 235 |
((do) (dynamic-parse-do env args)) |
| 236 |
((quasiquote) (dynamic-parse-quasiquote env args)) |
| 237 |
(else (dynamic-parse-procedure-call env op args))))) |
| 238 |
(else (dynamic-parse-datum e)))) |
| 239 |
|
| 240 |
; dynamic-parse-expression* |
| 241 |
|
| 242 |
(define (dynamic-parse-expression* env exprs) |
| 243 |
;; Parses lists of expressions (returns them in the right order!) |
| 244 |
(letrec ((pe* |
| 245 |
(lambda (results es) |
| 246 |
(cond |
| 247 |
((null? es) results) |
| 248 |
((pair? es) (pe* (cons (dynamic-parse-expression env (car es)) results) (cdr es))) |
| 249 |
(else (fatal-error 'dynamic-parse-expression* "Not a list of expressions: ~s" es)))))) |
| 250 |
(reverse (pe* '() exprs)))) |
| 251 |
|
| 252 |
|
| 253 |
; dynamic-parse-expressions |
| 254 |
|
| 255 |
(define (dynamic-parse-expressions env exprs) |
| 256 |
;; parses lists of arguments of a procedure call |
| 257 |
(cond |
| 258 |
((null? exprs) (dynamic-parse-action-null-arg)) |
| 259 |
((pair? exprs) (let* ((fst-expr (car exprs)) |
| 260 |
(rem-exprs (cdr exprs)) |
| 261 |
(fst-res (dynamic-parse-expression env fst-expr)) |
| 262 |
(rem-res (dynamic-parse-expressions env rem-exprs))) |
| 263 |
(dynamic-parse-action-pair-arg fst-res rem-res))) |
| 264 |
(else (fatal-error 'dynamic-parse-expressions "Illegal expression list: ~s" |
| 265 |
exprs)))) |
| 266 |
|
| 267 |
|
| 268 |
; dynamic-parse-variable: parses variables (applied occurrences) |
| 269 |
|
| 270 |
(define (dynamic-parse-variable env e) |
| 271 |
(if (symbol? e) |
| 272 |
(if (memq e syntactic-keywords) |
| 273 |
(fatal-error 'dynamic-parse-variable "Illegal identifier (keyword): ~s" e) |
| 274 |
(let ((assoc-var-def (dynamic-lookup e env))) |
| 275 |
(if assoc-var-def |
| 276 |
(dynamic-parse-action-variable (binding-value assoc-var-def)) |
| 277 |
(dynamic-parse-action-identifier e)))) |
| 278 |
(fatal-error 'dynamic-parse-variable "Not an identifier: ~s" e))) |
| 279 |
|
| 280 |
|
| 281 |
; dynamic-parse-procedure-call |
| 282 |
|
| 283 |
(define (dynamic-parse-procedure-call env op args) |
| 284 |
(dynamic-parse-action-procedure-call |
| 285 |
(dynamic-parse-expression env op) |
| 286 |
(dynamic-parse-expressions env args))) |
| 287 |
|
| 288 |
|
| 289 |
; dynamic-parse-quote |
| 290 |
|
| 291 |
(define (dynamic-parse-quote env args) |
| 292 |
(if (list-of-1? args) |
| 293 |
(dynamic-parse-datum (car args)) |
| 294 |
(fatal-error 'dynamic-parse-quote "Not a datum (multiple arguments): ~s" args))) |
| 295 |
|
| 296 |
|
| 297 |
; dynamic-parse-lambda |
| 298 |
|
| 299 |
(define (dynamic-parse-lambda env args) |
| 300 |
(if (pair? args) |
| 301 |
(let* ((formals (car args)) |
| 302 |
(body (cdr args)) |
| 303 |
(nenv-fresults (dynamic-parse-formals formals)) |
| 304 |
(nenv (car nenv-fresults)) |
| 305 |
(fresults (cdr nenv-fresults))) |
| 306 |
(dynamic-parse-action-lambda-expression |
| 307 |
fresults |
| 308 |
(dynamic-parse-body (extend-env-with-env env nenv) body))) |
| 309 |
(fatal-error 'dynamic-parse-lambda "Illegal formals/body: ~s" args))) |
| 310 |
|
| 311 |
|
| 312 |
; dynamic-parse-body |
| 313 |
|
| 314 |
(define (dynamic-parse-body env body) |
| 315 |
; <body> = <definition>* <expression>+ |
| 316 |
(define (def-var* f-env body) |
| 317 |
; finds the defined variables in a body and returns an |
| 318 |
; environment containing them |
| 319 |
(if (pair? body) |
| 320 |
(let ((n-env (def-var f-env (car body)))) |
| 321 |
(if n-env |
| 322 |
(def-var* n-env (cdr body)) |
| 323 |
f-env)) |
| 324 |
f-env)) |
| 325 |
(define (def-var f-env clause) |
| 326 |
; finds the defined variables in a single clause and extends |
| 327 |
; f-env accordingly; returns false if it's not a definition |
| 328 |
(if (pair? clause) |
| 329 |
(case (car clause) |
| 330 |
((define) (if (pair? (cdr clause)) |
| 331 |
(let ((pattern (cadr clause))) |
| 332 |
(cond |
| 333 |
((symbol? pattern) |
| 334 |
(extend-env-with-binding |
| 335 |
f-env |
| 336 |
(gen-binding pattern |
| 337 |
(dynamic-parse-action-var-def pattern)))) |
| 338 |
((and (pair? pattern) (symbol? (car pattern))) |
| 339 |
(extend-env-with-binding |
| 340 |
f-env |
| 341 |
(gen-binding (car pattern) |
| 342 |
(dynamic-parse-action-var-def |
| 343 |
(car pattern))))) |
| 344 |
(else f-env))) |
| 345 |
f-env)) |
| 346 |
((begin) (def-var* f-env (cdr clause))) |
| 347 |
(else #f)) |
| 348 |
#f)) |
| 349 |
(if (pair? body) |
| 350 |
(dynamic-parse-command* (def-var* env body) body) |
| 351 |
(fatal-error 'dynamic-parse-body "Illegal body: ~s" body))) |
| 352 |
|
| 353 |
; dynamic-parse-if |
| 354 |
|
| 355 |
(define (dynamic-parse-if env args) |
| 356 |
(cond |
| 357 |
((list-of-3? args) |
| 358 |
(dynamic-parse-action-conditional |
| 359 |
(dynamic-parse-expression env (car args)) |
| 360 |
(dynamic-parse-expression env (cadr args)) |
| 361 |
(dynamic-parse-expression env (caddr args)))) |
| 362 |
((list-of-2? args) |
| 363 |
(dynamic-parse-action-conditional |
| 364 |
(dynamic-parse-expression env (car args)) |
| 365 |
(dynamic-parse-expression env (cadr args)) |
| 366 |
(dynamic-parse-action-empty))) |
| 367 |
(else (fatal-error 'dynamic-parse-if "Not an if-expression: ~s" args)))) |
| 368 |
|
| 369 |
|
| 370 |
; dynamic-parse-set |
| 371 |
|
| 372 |
(define (dynamic-parse-set env args) |
| 373 |
(if (list-of-2? args) |
| 374 |
(dynamic-parse-action-assignment |
| 375 |
(dynamic-parse-variable env (car args)) |
| 376 |
(dynamic-parse-expression env (cadr args))) |
| 377 |
(fatal-error 'dynamic-parse-set "Not a variable/expression pair: ~s" args))) |
| 378 |
|
| 379 |
|
| 380 |
; dynamic-parse-begin |
| 381 |
|
| 382 |
(define (dynamic-parse-begin env args) |
| 383 |
(dynamic-parse-action-begin-expression |
| 384 |
(dynamic-parse-body env args))) |
| 385 |
|
| 386 |
|
| 387 |
; dynamic-parse-cond |
| 388 |
|
| 389 |
(define (dynamic-parse-cond env args) |
| 390 |
(if (and (pair? args) (list? args)) |
| 391 |
(dynamic-parse-action-cond-expression |
| 392 |
(map (lambda (e) |
| 393 |
(dynamic-parse-cond-clause env e)) |
| 394 |
args)) |
| 395 |
(fatal-error 'dynamic-parse-cond "Not a list of cond-clauses: ~s" args))) |
| 396 |
|
| 397 |
; dynamic-parse-cond-clause |
| 398 |
|
| 399 |
(define (dynamic-parse-cond-clause env e) |
| 400 |
;; ***Note***: Only (<test> <sequence>) is permitted! |
| 401 |
(if (pair? e) |
| 402 |
(cons |
| 403 |
(if (eqv? (car e) 'else) |
| 404 |
(dynamic-parse-action-empty) |
| 405 |
(dynamic-parse-expression env (car e))) |
| 406 |
(dynamic-parse-body env (cdr e))) |
| 407 |
(fatal-error 'dynamic-parse-cond-clause "Not a cond-clause: ~s" e))) |
| 408 |
|
| 409 |
|
| 410 |
; dynamic-parse-and |
| 411 |
|
| 412 |
(define (dynamic-parse-and env args) |
| 413 |
(if (list? args) |
| 414 |
(dynamic-parse-action-and-expression |
| 415 |
(dynamic-parse-expression* env args)) |
| 416 |
(fatal-error 'dynamic-parse-and "Not a list of arguments: ~s" args))) |
| 417 |
|
| 418 |
|
| 419 |
; dynamic-parse-or |
| 420 |
|
| 421 |
(define (dynamic-parse-or env args) |
| 422 |
(if (list? args) |
| 423 |
(dynamic-parse-action-or-expression |
| 424 |
(dynamic-parse-expression* env args)) |
| 425 |
(fatal-error 'dynamic-parse-or "Not a list of arguments: ~s" args))) |
| 426 |
|
| 427 |
|
| 428 |
; dynamic-parse-case |
| 429 |
|
| 430 |
(define (dynamic-parse-case env args) |
| 431 |
(if (and (list? args) (> (length args) 1)) |
| 432 |
(dynamic-parse-action-case-expression |
| 433 |
(dynamic-parse-expression env (car args)) |
| 434 |
(map (lambda (e) |
| 435 |
(dynamic-parse-case-clause env e)) |
| 436 |
(cdr args))) |
| 437 |
(fatal-error 'dynamic-parse-case "Not a list of clauses: ~s" args))) |
| 438 |
|
| 439 |
; dynamic-parse-case-clause |
| 440 |
|
| 441 |
(define (dynamic-parse-case-clause env e) |
| 442 |
(if (pair? e) |
| 443 |
(cons |
| 444 |
(cond |
| 445 |
((eqv? (car e) 'else) |
| 446 |
(list (dynamic-parse-action-empty))) |
| 447 |
((list? (car e)) |
| 448 |
(map dynamic-parse-datum (car e))) |
| 449 |
(else (fatal-error 'dynamic-parse-case-clause "Not a datum list: ~s" (car e)))) |
| 450 |
(dynamic-parse-body env (cdr e))) |
| 451 |
(fatal-error 'dynamic-parse-case-clause "Not case clause: ~s" e))) |
| 452 |
|
| 453 |
|
| 454 |
; dynamic-parse-let |
| 455 |
|
| 456 |
(define (dynamic-parse-let env args) |
| 457 |
(if (pair? args) |
| 458 |
(if (symbol? (car args)) |
| 459 |
(dynamic-parse-named-let env args) |
| 460 |
(dynamic-parse-normal-let env args)) |
| 461 |
(fatal-error 'dynamic-parse-let "Illegal bindings/body: ~s" args))) |
| 462 |
|
| 463 |
|
| 464 |
; dynamic-parse-normal-let |
| 465 |
|
| 466 |
(define (dynamic-parse-normal-let env args) |
| 467 |
;; parses "normal" let-expressions |
| 468 |
(let* ((bindings (car args)) |
| 469 |
(body (cdr args)) |
| 470 |
(env-ast (dynamic-parse-parallel-bindings env bindings)) |
| 471 |
(nenv (car env-ast)) |
| 472 |
(bresults (cdr env-ast))) |
| 473 |
(dynamic-parse-action-let-expression |
| 474 |
bresults |
| 475 |
(dynamic-parse-body (extend-env-with-env env nenv) body)))) |
| 476 |
|
| 477 |
; dynamic-parse-named-let |
| 478 |
|
| 479 |
(define (dynamic-parse-named-let env args) |
| 480 |
;; parses a named let-expression |
| 481 |
(if (pair? (cdr args)) |
| 482 |
(let* ((variable (car args)) |
| 483 |
(bindings (cadr args)) |
| 484 |
(body (cddr args)) |
| 485 |
(vbind-vres (dynamic-parse-formal dynamic-empty-env variable)) |
| 486 |
(vbind (car vbind-vres)) |
| 487 |
(vres (cdr vbind-vres)) |
| 488 |
(env-ast (dynamic-parse-parallel-bindings env bindings)) |
| 489 |
(nenv (car env-ast)) |
| 490 |
(bresults (cdr env-ast))) |
| 491 |
(dynamic-parse-action-named-let-expression |
| 492 |
vres bresults |
| 493 |
(dynamic-parse-body (extend-env-with-env |
| 494 |
(extend-env-with-binding env vbind) |
| 495 |
nenv) body))) |
| 496 |
(fatal-error 'dynamic-parse-named-let "Illegal named let-expression: ~s" args))) |
| 497 |
|
| 498 |
|
| 499 |
; dynamic-parse-parallel-bindings |
| 500 |
|
| 501 |
(define (dynamic-parse-parallel-bindings env bindings) |
| 502 |
; returns a pair consisting of an environment |
| 503 |
; and a list of pairs (variable . asg) |
| 504 |
; ***Note***: the list of pairs is returned in reverse unzipped form! |
| 505 |
(if (list-of-list-of-2s? bindings) |
| 506 |
(let* ((env-formals-asg |
| 507 |
(dynamic-parse-formal* (map car bindings))) |
| 508 |
(nenv (car env-formals-asg)) |
| 509 |
(bresults (cdr env-formals-asg)) |
| 510 |
(exprs-asg |
| 511 |
(dynamic-parse-expression* env (map cadr bindings)))) |
| 512 |
(cons nenv (cons bresults exprs-asg))) |
| 513 |
(fatal-error 'dynamic-parse-parallel-bindings |
| 514 |
"Not a list of bindings: ~s" bindings))) |
| 515 |
|
| 516 |
|
| 517 |
; dynamic-parse-let* |
| 518 |
|
| 519 |
(define (dynamic-parse-let* env args) |
| 520 |
(if (pair? args) |
| 521 |
(let* ((bindings (car args)) |
| 522 |
(body (cdr args)) |
| 523 |
(env-ast (dynamic-parse-sequential-bindings env bindings)) |
| 524 |
(nenv (car env-ast)) |
| 525 |
(bresults (cdr env-ast))) |
| 526 |
(dynamic-parse-action-let*-expression |
| 527 |
bresults |
| 528 |
(dynamic-parse-body (extend-env-with-env env nenv) body))) |
| 529 |
(fatal-error 'dynamic-parse-let* "Illegal bindings/body: ~s" args))) |
| 530 |
|
| 531 |
; dynamic-parse-sequential-bindings |
| 532 |
|
| 533 |
(define (dynamic-parse-sequential-bindings env bindings) |
| 534 |
; returns a pair consisting of an environment |
| 535 |
; and a list of pairs (variable . asg) |
| 536 |
;; ***Note***: the list of pairs is returned in reverse unzipped form! |
| 537 |
(letrec |
| 538 |
((psb |
| 539 |
(lambda (f-env c-env var-defs expr-asgs binds) |
| 540 |
;; f-env: forbidden environment |
| 541 |
;; c-env: constructed environment |
| 542 |
;; var-defs: results of formals |
| 543 |
;; expr-asgs: results of corresponding expressions |
| 544 |
;; binds: reminding bindings to process |
| 545 |
(cond |
| 546 |
((null? binds) |
| 547 |
(cons f-env (cons var-defs expr-asgs))) |
| 548 |
((pair? binds) |
| 549 |
(let ((fst-bind (car binds))) |
| 550 |
(if (list-of-2? fst-bind) |
| 551 |
(let* ((fbinding-bres |
| 552 |
(dynamic-parse-formal f-env (car fst-bind))) |
| 553 |
(fbind (car fbinding-bres)) |
| 554 |
(bres (cdr fbinding-bres)) |
| 555 |
(new-expr-asg |
| 556 |
(dynamic-parse-expression c-env (cadr fst-bind)))) |
| 557 |
(psb |
| 558 |
(extend-env-with-binding f-env fbind) |
| 559 |
(extend-env-with-binding c-env fbind) |
| 560 |
(cons bres var-defs) |
| 561 |
(cons new-expr-asg expr-asgs) |
| 562 |
(cdr binds))) |
| 563 |
(fatal-error 'dynamic-parse-sequential-bindings |
| 564 |
"Illegal binding: ~s" fst-bind)))) |
| 565 |
(else (fatal-error 'dynamic-parse-sequential-bindings |
| 566 |
"Illegal bindings: ~s" binds)))))) |
| 567 |
(let ((env-vdefs-easgs (psb dynamic-empty-env env '() '() bindings))) |
| 568 |
(cons (car env-vdefs-easgs) |
| 569 |
(cons (reverse (cadr env-vdefs-easgs)) |
| 570 |
(reverse (cddr env-vdefs-easgs))))))) |
| 571 |
|
| 572 |
|
| 573 |
; dynamic-parse-letrec |
| 574 |
|
| 575 |
(define (dynamic-parse-letrec env args) |
| 576 |
(if (pair? args) |
| 577 |
(let* ((bindings (car args)) |
| 578 |
(body (cdr args)) |
| 579 |
(env-ast (dynamic-parse-recursive-bindings env bindings)) |
| 580 |
(nenv (car env-ast)) |
| 581 |
(bresults (cdr env-ast))) |
| 582 |
(dynamic-parse-action-letrec-expression |
| 583 |
bresults |
| 584 |
(dynamic-parse-body (extend-env-with-env env nenv) body))) |
| 585 |
(fatal-error 'dynamic-parse-letrec "Illegal bindings/body: ~s" args))) |
| 586 |
|
| 587 |
; dynamic-parse-recursive-bindings |
| 588 |
|
| 589 |
(define (dynamic-parse-recursive-bindings env bindings) |
| 590 |
;; ***Note***: the list of pairs is returned in reverse unzipped form! |
| 591 |
(if (list-of-list-of-2s? bindings) |
| 592 |
(let* ((env-formals-asg |
| 593 |
(dynamic-parse-formal* (map car bindings))) |
| 594 |
(formals-env |
| 595 |
(car env-formals-asg)) |
| 596 |
(formals-res |
| 597 |
(cdr env-formals-asg)) |
| 598 |
(exprs-asg |
| 599 |
(dynamic-parse-expression* |
| 600 |
(extend-env-with-env env formals-env) |
| 601 |
(map cadr bindings)))) |
| 602 |
(cons |
| 603 |
formals-env |
| 604 |
(cons formals-res exprs-asg))) |
| 605 |
(fatal-error 'dynamic-parse-recursive-bindings "Illegal bindings: ~s" bindings))) |
| 606 |
|
| 607 |
|
| 608 |
; dynamic-parse-do |
| 609 |
|
| 610 |
(define (dynamic-parse-do env args) |
| 611 |
;; parses do-expressions |
| 612 |
;; ***Note***: Not implemented! |
| 613 |
(fatal-error 'dynamic-parse-do "Nothing yet...")) |
| 614 |
|
| 615 |
; dynamic-parse-quasiquote |
| 616 |
|
| 617 |
(define (dynamic-parse-quasiquote env args) |
| 618 |
;; ***Note***: Not implemented! |
| 619 |
(fatal-error 'dynamic-parse-quasiquote "Nothing yet...")) |
| 620 |
|
| 621 |
|
| 622 |
;; Command |
| 623 |
|
| 624 |
; dynamic-parse-command |
| 625 |
|
| 626 |
(define (dynamic-parse-command env c) |
| 627 |
(if (pair? c) |
| 628 |
(let ((op (car c)) |
| 629 |
(args (cdr c))) |
| 630 |
(case op |
| 631 |
((define) (dynamic-parse-define env args)) |
| 632 |
; ((begin) (dynamic-parse-command* env args)) ;; AKW |
| 633 |
((begin) (dynamic-parse-action-begin-expression (dynamic-parse-command* env args))) |
| 634 |
(else (dynamic-parse-expression env c)))) |
| 635 |
(dynamic-parse-expression env c))) |
| 636 |
|
| 637 |
|
| 638 |
; dynamic-parse-command* |
| 639 |
|
| 640 |
(define (dynamic-parse-command* env commands) |
| 641 |
;; parses a sequence of commands |
| 642 |
(if (list? commands) |
| 643 |
(map (lambda (command) (dynamic-parse-command env command)) commands) |
| 644 |
(fatal-error 'dynamic-parse-command* "Invalid sequence of commands: ~s" commands))) |
| 645 |
|
| 646 |
|
| 647 |
; dynamic-parse-define |
| 648 |
|
| 649 |
(define (dynamic-parse-define env args) |
| 650 |
;; three cases -- see IEEE Scheme, sect. 5.2 |
| 651 |
;; ***Note***: the parser admits forms (define (x . y) ...) |
| 652 |
;; ***Note***: Variables are treated as applied occurrences! |
| 653 |
(if (pair? args) |
| 654 |
(let ((pattern (car args)) |
| 655 |
(exp-or-body (cdr args))) |
| 656 |
(cond |
| 657 |
((symbol? pattern) |
| 658 |
(if (list-of-1? exp-or-body) |
| 659 |
(dynamic-parse-action-definition |
| 660 |
(dynamic-parse-variable env pattern) |
| 661 |
(dynamic-parse-expression env (car exp-or-body))) |
| 662 |
(fatal-error 'dynamic-parse-define "Not a single expression: ~s" exp-or-body))) |
| 663 |
((pair? pattern) |
| 664 |
(let* ((function-name (car pattern)) |
| 665 |
(function-arg-names (cdr pattern)) |
| 666 |
(env-ast (dynamic-parse-formals function-arg-names)) |
| 667 |
(formals-env (car env-ast)) |
| 668 |
(formals-ast (cdr env-ast))) |
| 669 |
(dynamic-parse-action-function-definition |
| 670 |
(dynamic-parse-variable env function-name) |
| 671 |
formals-ast |
| 672 |
(dynamic-parse-body (extend-env-with-env env formals-env) exp-or-body)))) |
| 673 |
(else (fatal-error 'dynamic-parse-define "Not a valid pattern: ~s" pattern)))) |
| 674 |
(fatal-error 'dynamic-parse-define "Not a valid definition: ~s" args))) |
| 675 |
|
| 676 |
;; Auxiliary routines |
| 677 |
|
| 678 |
; forall? |
| 679 |
|
| 680 |
(define (forall? pred list) |
| 681 |
(if (null? list) |
| 682 |
#t |
| 683 |
(and (pred (car list)) (forall? pred (cdr list))))) |
| 684 |
|
| 685 |
; list-of-1? |
| 686 |
|
| 687 |
(define (list-of-1? l) |
| 688 |
(and (pair? l) (null? (cdr l)))) |
| 689 |
|
| 690 |
; list-of-2? |
| 691 |
|
| 692 |
(define (list-of-2? l) |
| 693 |
(and (pair? l) (pair? (cdr l)) (null? (cddr l)))) |
| 694 |
|
| 695 |
; list-of-3? |
| 696 |
|
| 697 |
(define (list-of-3? l) |
| 698 |
(and (pair? l) (pair? (cdr l)) (pair? (cddr l)) (null? (cdddr l)))) |
| 699 |
|
| 700 |
; list-of-list-of-2s? |
| 701 |
|
| 702 |
(define (list-of-list-of-2s? e) |
| 703 |
(cond |
| 704 |
((null? e) |
| 705 |
#t) |
| 706 |
((pair? e) |
| 707 |
(and (list-of-2? (car e)) (list-of-list-of-2s? (cdr e)))) |
| 708 |
(else #f))) |
| 709 |
|
| 710 |
|
| 711 |
;; File processing |
| 712 |
|
| 713 |
; dynamic-parse-from-port |
| 714 |
|
| 715 |
(define (dynamic-parse-from-port port) |
| 716 |
(let ((next-input (read port))) |
| 717 |
(if (eof-object? next-input) |
| 718 |
'() |
| 719 |
(dynamic-parse-action-commands |
| 720 |
(dynamic-parse-command dynamic-empty-env next-input) |
| 721 |
(dynamic-parse-from-port port))))) |
| 722 |
|
| 723 |
; dynamic-parse-file |
| 724 |
|
| 725 |
(define (dynamic-parse-file file-name) |
| 726 |
(let ((input-port (open-input-file file-name))) |
| 727 |
(dynamic-parse-from-port input-port))) |
| 728 |
;---------------------------------------------------------------------------- |
| 729 |
; Implementation of Union/find data structure in Scheme |
| 730 |
;---------------------------------------------------------------------------- |
| 731 |
|
| 732 |
;; for union/find the following attributes are necessary: rank, parent |
| 733 |
;; (see Tarjan, "Data structures and network algorithms", 1983) |
| 734 |
;; In the Scheme realization an element is represented as a single |
| 735 |
;; cons cell; its address is the element itself; the car field contains |
| 736 |
;; the parent, the cdr field is an address for a cons |
| 737 |
;; cell containing the rank (car field) and the information (cdr field) |
| 738 |
|
| 739 |
|
| 740 |
;; general union/find data structure |
| 741 |
;; |
| 742 |
;; gen-element: Info -> Elem |
| 743 |
;; find: Elem -> Elem |
| 744 |
;; link: Elem! x Elem! -> Elem |
| 745 |
;; asymm-link: Elem! x Elem! -> Elem |
| 746 |
;; info: Elem -> Info |
| 747 |
;; set-info!: Elem! x Info -> Void |
| 748 |
|
| 749 |
|
| 750 |
(define (gen-element info) |
| 751 |
; generates a new element: the parent field is initialized to '(), |
| 752 |
; the rank field to 0 |
| 753 |
(cons '() (cons 0 info))) |
| 754 |
|
| 755 |
(define info (lambda (l) (cddr l))) |
| 756 |
; returns the information stored in an element |
| 757 |
|
| 758 |
(define (set-info! elem info) |
| 759 |
; sets the info-field of elem to info |
| 760 |
(set-cdr! (cdr elem) info)) |
| 761 |
|
| 762 |
; (define (find! x) |
| 763 |
; ; finds the class representative of x and sets the parent field |
| 764 |
; ; directly to the class representative (a class representative has |
| 765 |
; ; '() as its parent) (uses path halving) |
| 766 |
; ;(display "Find!: ") |
| 767 |
; ;(display (pretty-print (info x))) |
| 768 |
; ;(newline) |
| 769 |
; (let ((px (car x))) |
| 770 |
; (if (null? px) |
| 771 |
; x |
| 772 |
; (let ((ppx (car px))) |
| 773 |
; (if (null? ppx) |
| 774 |
; px |
| 775 |
; (begin |
| 776 |
; (set-car! x ppx) |
| 777 |
; (find! ppx))))))) |
| 778 |
|
| 779 |
(define (find! elem) |
| 780 |
; finds the class representative of elem and sets the parent field |
| 781 |
; directly to the class representative (a class representative has |
| 782 |
; '() as its parent) |
| 783 |
;(display "Find!: ") |
| 784 |
;(display (pretty-print (info elem))) |
| 785 |
;(newline) |
| 786 |
(let ((p-elem (car elem))) |
| 787 |
(if (null? p-elem) |
| 788 |
elem |
| 789 |
(let ((rep-elem (find! p-elem))) |
| 790 |
(set-car! elem rep-elem) |
| 791 |
rep-elem)))) |
| 792 |
|
| 793 |
(define (link! elem-1 elem-2) |
| 794 |
; links class elements by rank |
| 795 |
; they must be distinct class representatives |
| 796 |
; returns the class representative of the merged equivalence classes |
| 797 |
;(display "Link!: ") |
| 798 |
;(display (pretty-print (list (info elem-1) (info elem-2)))) |
| 799 |
;(newline) |
| 800 |
(let ((rank-1 (cadr elem-1)) |
| 801 |
(rank-2 (cadr elem-2))) |
| 802 |
(cond |
| 803 |
((= rank-1 rank-2) |
| 804 |
(set-car! (cdr elem-2) (+ rank-2 1)) |
| 805 |
(set-car! elem-1 elem-2) |
| 806 |
elem-2) |
| 807 |
((> rank-1 rank-2) |
| 808 |
(set-car! elem-2 elem-1) |
| 809 |
elem-1) |
| 810 |
(else |
| 811 |
(set-car! elem-1 elem-2) |
| 812 |
elem-2)))) |
| 813 |
|
| 814 |
(define asymm-link! (lambda (l x) (set-car! l x))) |
| 815 |
|
| 816 |
;(define (asymm-link! elem-1 elem-2) |
| 817 |
; links elem-1 onto elem-2 no matter what rank; |
| 818 |
; does not update the rank of elem-2 and does not return a value |
| 819 |
; the two arguments must be distinct |
| 820 |
;(display "AsymmLink: ") |
| 821 |
;(display (pretty-print (list (info elem-1) (info elem-2)))) |
| 822 |
;(newline) |
| 823 |
;(set-car! elem-1 elem-2)) |
| 824 |
|
| 825 |
;---------------------------------------------------------------------------- |
| 826 |
; Type management |
| 827 |
;---------------------------------------------------------------------------- |
| 828 |
|
| 829 |
; introduces type variables and types for Scheme, |
| 830 |
|
| 831 |
|
| 832 |
;; type TVar (type variables) |
| 833 |
;; |
| 834 |
;; gen-tvar: () -> TVar |
| 835 |
;; gen-type: TCon x TVar* -> TVar |
| 836 |
;; dynamic: TVar |
| 837 |
;; tvar-id: TVar -> Symbol |
| 838 |
;; tvar-def: TVar -> Type + Null |
| 839 |
;; tvar-show: TVar -> Symbol* |
| 840 |
;; |
| 841 |
;; set-def!: !TVar x TCon x TVar* -> Null |
| 842 |
;; equiv!: !TVar x !TVar -> Null |
| 843 |
;; |
| 844 |
;; |
| 845 |
;; type TCon (type constructors) |
| 846 |
;; |
| 847 |
;; ... |
| 848 |
;; |
| 849 |
;; type Type (types) |
| 850 |
;; |
| 851 |
;; gen-type: TCon x TVar* -> Type |
| 852 |
;; type-con: Type -> TCon |
| 853 |
;; type-args: Type -> TVar* |
| 854 |
;; |
| 855 |
;; boolean: TVar |
| 856 |
;; character: TVar |
| 857 |
;; null: TVar |
| 858 |
;; pair: TVar x TVar -> TVar |
| 859 |
;; procedure: TVar x TVar* -> TVar |
| 860 |
;; charseq: TVar |
| 861 |
;; symbol: TVar |
| 862 |
;; array: TVar -> TVar |
| 863 |
|
| 864 |
|
| 865 |
; Needed packages: union/find |
| 866 |
|
| 867 |
;(load "union-fi.so") |
| 868 |
|
| 869 |
; TVar |
| 870 |
|
| 871 |
(define counter 0) |
| 872 |
; counter for generating tvar id's |
| 873 |
|
| 874 |
(define (gen-id) |
| 875 |
; generates a new id (for printing purposes) |
| 876 |
(set! counter (+ counter 1)) |
| 877 |
counter) |
| 878 |
|
| 879 |
(define (gen-tvar) |
| 880 |
; generates a new type variable from a new symbol |
| 881 |
; uses union/find elements with two info fields |
| 882 |
; a type variable has exactly four fields: |
| 883 |
; car: TVar (the parent field; initially null) |
| 884 |
; cadr: Number (the rank field; is always nonnegative) |
| 885 |
; caddr: Symbol (the type variable identifier; used only for printing) |
| 886 |
; cdddr: Type (the leq field; initially null) |
| 887 |
(gen-element (cons (gen-id) '()))) |
| 888 |
|
| 889 |
(define (gen-type tcon targs) |
| 890 |
; generates a new type variable with an associated type definition |
| 891 |
(gen-element (cons (gen-id) (cons tcon targs)))) |
| 892 |
|
| 893 |
(define dynamic (gen-element (cons 0 '()))) |
| 894 |
; the special type variable dynamic |
| 895 |
; Generic operations |
| 896 |
|
| 897 |
(define (tvar-id tvar) |
| 898 |
; returns the (printable) symbol representing the type variable |
| 899 |
(car (info tvar))) |
| 900 |
|
| 901 |
(define (tvar-def tvar) |
| 902 |
; returns the type definition (if any) of the type variable |
| 903 |
(cdr (info tvar))) |
| 904 |
|
| 905 |
(define (set-def! tvar tcon targs) |
| 906 |
; sets the type definition part of tvar to type |
| 907 |
(set-cdr! (info tvar) (cons tcon targs)) |
| 908 |
'()) |
| 909 |
|
| 910 |
(define (reset-def! tvar) |
| 911 |
; resets the type definition part of tvar to nil |
| 912 |
(set-cdr! (info tvar) '())) |
| 913 |
|
| 914 |
(define type-con (lambda (l) (car l))) |
| 915 |
; returns the type constructor of a type definition |
| 916 |
|
| 917 |
(define type-args (lambda (l) (cdr l))) |
| 918 |
; returns the type variables of a type definition |
| 919 |
|
| 920 |
(define (tvar->string tvar) |
| 921 |
; converts a tvar's id to a string |
| 922 |
(if (eqv? (tvar-id tvar) 0) |
| 923 |
"Dynamic" |
| 924 |
(string-append "t#" (number->string (tvar-id tvar) 10)))) |
| 925 |
|
| 926 |
(define (tvar-show tv) |
| 927 |
; returns a printable list representation of type variable tv |
| 928 |
(let* ((tv-rep (find! tv)) |
| 929 |
(tv-def (tvar-def tv-rep))) |
| 930 |
(cons (tvar->string tv-rep) |
| 931 |
(if (null? tv-def) |
| 932 |
'() |
| 933 |
(cons 'is (type-show tv-def)))))) |
| 934 |
|
| 935 |
(define (type-show type) |
| 936 |
; returns a printable list representation of type definition type |
| 937 |
(cond |
| 938 |
((eqv? (type-con type) ptype-con) |
| 939 |
(let ((new-tvar (gen-tvar))) |
| 940 |
(cons ptype-con |
| 941 |
(cons (tvar-show new-tvar) |
| 942 |
(tvar-show ((type-args type) new-tvar)))))) |
| 943 |
(else |
| 944 |
(cons (type-con type) |
| 945 |
(map (lambda (tv) |
| 946 |
(tvar->string (find! tv))) |
| 947 |
(type-args type)))))) |
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
; Special type operations |
| 952 |
|
| 953 |
; type constructor literals |
| 954 |
|
| 955 |
(define boolean-con 'boolean) |
| 956 |
(define char-con 'char) |
| 957 |
(define null-con 'null) |
| 958 |
(define number-con 'number) |
| 959 |
(define pair-con 'pair) |
| 960 |
(define procedure-con 'procedure) |
| 961 |
(define string-con 'string) |
| 962 |
(define symbol-con 'symbol) |
| 963 |
(define vector-con 'vector) |
| 964 |
|
| 965 |
; type constants and type constructors |
| 966 |
|
| 967 |
(define (null) |
| 968 |
; ***Note***: Temporarily changed to be a pair! |
| 969 |
; (gen-type null-con '()) |
| 970 |
(pair (gen-tvar) (gen-tvar))) |
| 971 |
(define (boolean) |
| 972 |
(gen-type boolean-con '())) |
| 973 |
(define (character) |
| 974 |
(gen-type char-con '())) |
| 975 |
(define (number) |
| 976 |
(gen-type number-con '())) |
| 977 |
(define (charseq) |
| 978 |
(gen-type string-con '())) |
| 979 |
(define (symbol) |
| 980 |
(gen-type symbol-con '())) |
| 981 |
(define (pair tvar-1 tvar-2) |
| 982 |
(gen-type pair-con (list tvar-1 tvar-2))) |
| 983 |
(define (array tvar) |
| 984 |
(gen-type vector-con (list tvar))) |
| 985 |
(define (procedure arg-tvar res-tvar) |
| 986 |
(gen-type procedure-con (list arg-tvar res-tvar))) |
| 987 |
|
| 988 |
|
| 989 |
; equivalencing of type variables |
| 990 |
|
| 991 |
(define (equiv! tv1 tv2) |
| 992 |
(let* ((tv1-rep (find! tv1)) |
| 993 |
(tv2-rep (find! tv2)) |
| 994 |
(tv1-def (tvar-def tv1-rep)) |
| 995 |
(tv2-def (tvar-def tv2-rep))) |
| 996 |
(cond |
| 997 |
((eqv? tv1-rep tv2-rep) |
| 998 |
'()) |
| 999 |
((eqv? tv2-rep dynamic) |
| 1000 |
(equiv-with-dynamic! tv1-rep)) |
| 1001 |
((eqv? tv1-rep dynamic) |
| 1002 |
(equiv-with-dynamic! tv2-rep)) |
| 1003 |
((null? tv1-def) |
| 1004 |
(if (null? tv2-def) |
| 1005 |
; both tv1 and tv2 are distinct type variables |
| 1006 |
(link! tv1-rep tv2-rep) |
| 1007 |
; tv1 is a type variable, tv2 is a (nondynamic) type |
| 1008 |
(asymm-link! tv1-rep tv2-rep))) |
| 1009 |
((null? tv2-def) |
| 1010 |
; tv1 is a (nondynamic) type, tv2 is a type variable |
| 1011 |
(asymm-link! tv2-rep tv1-rep)) |
| 1012 |
((eqv? (type-con tv1-def) (type-con tv2-def)) |
| 1013 |
; both tv1 and tv2 are (nondynamic) types with equal numbers of |
| 1014 |
; arguments |
| 1015 |
(link! tv1-rep tv2-rep) |
| 1016 |
(map equiv! (type-args tv1-def) (type-args tv2-def))) |
| 1017 |
(else |
| 1018 |
; tv1 and tv2 are types with distinct type constructors or different |
| 1019 |
; numbers of arguments |
| 1020 |
(equiv-with-dynamic! tv1-rep) |
| 1021 |
(equiv-with-dynamic! tv2-rep)))) |
| 1022 |
'()) |
| 1023 |
|
| 1024 |
(define (equiv-with-dynamic! tv) |
| 1025 |
(let ((tv-rep (find! tv))) |
| 1026 |
(if (not (eqv? tv-rep dynamic)) |
| 1027 |
(let ((tv-def (tvar-def tv-rep))) |
| 1028 |
(asymm-link! tv-rep dynamic) |
| 1029 |
(if (not (null? tv-def)) |
| 1030 |
(map equiv-with-dynamic! (type-args tv-def)))))) |
| 1031 |
'()) |
| 1032 |
;---------------------------------------------------------------------------- |
| 1033 |
; Polymorphic type management |
| 1034 |
;---------------------------------------------------------------------------- |
| 1035 |
|
| 1036 |
; introduces parametric polymorphic types |
| 1037 |
|
| 1038 |
|
| 1039 |
;; forall: (Tvar -> Tvar) -> TVar |
| 1040 |
;; fix: (Tvar -> Tvar) -> Tvar |
| 1041 |
;; |
| 1042 |
;; instantiate-type: TVar -> TVar |
| 1043 |
|
| 1044 |
; type constructor literal for polymorphic types |
| 1045 |
|
| 1046 |
(define ptype-con 'forall) |
| 1047 |
|
| 1048 |
(define (forall tv-func) |
| 1049 |
(gen-type ptype-con tv-func)) |
| 1050 |
|
| 1051 |
(define (forall2 tv-func2) |
| 1052 |
(forall (lambda (tv1) |
| 1053 |
(forall (lambda (tv2) |
| 1054 |
(tv-func2 tv1 tv2)))))) |
| 1055 |
|
| 1056 |
(define (forall3 tv-func3) |
| 1057 |
(forall (lambda (tv1) |
| 1058 |
(forall2 (lambda (tv2 tv3) |
| 1059 |
(tv-func3 tv1 tv2 tv3)))))) |
| 1060 |
|
| 1061 |
(define (forall4 tv-func4) |
| 1062 |
(forall (lambda (tv1) |
| 1063 |
(forall3 (lambda (tv2 tv3 tv4) |
| 1064 |
(tv-func4 tv1 tv2 tv3 tv4)))))) |
| 1065 |
|
| 1066 |
(define (forall5 tv-func5) |
| 1067 |
(forall (lambda (tv1) |
| 1068 |
(forall4 (lambda (tv2 tv3 tv4 tv5) |
| 1069 |
(tv-func5 tv1 tv2 tv3 tv4 tv5)))))) |
| 1070 |
|
| 1071 |
|
| 1072 |
; (polymorphic) instantiation |
| 1073 |
|
| 1074 |
(define (instantiate-type tv) |
| 1075 |
; instantiates type tv and returns a generic instance |
| 1076 |
(let* ((tv-rep (find! tv)) |
| 1077 |
(tv-def (tvar-def tv-rep))) |
| 1078 |
(cond |
| 1079 |
((null? tv-def) |
| 1080 |
tv-rep) |
| 1081 |
((eqv? (type-con tv-def) ptype-con) |
| 1082 |
(instantiate-type ((type-args tv-def) (gen-tvar)))) |
| 1083 |
(else |
| 1084 |
tv-rep)))) |
| 1085 |
|
| 1086 |
(define (fix tv-func) |
| 1087 |
; forms a recursive type: the fixed point of type mapping tv-func |
| 1088 |
(let* ((new-tvar (gen-tvar)) |
| 1089 |
(inst-tvar (tv-func new-tvar)) |
| 1090 |
(inst-def (tvar-def inst-tvar))) |
| 1091 |
(if (null? inst-def) |
| 1092 |
(fatal-error 'fix "Illegal recursive type: ~s" |
| 1093 |
(list (tvar-show new-tvar) '= (tvar-show inst-tvar))) |
| 1094 |
(begin |
| 1095 |
(set-def! new-tvar |
| 1096 |
(type-con inst-def) |
| 1097 |
(type-args inst-def)) |
| 1098 |
new-tvar)))) |
| 1099 |
|
| 1100 |
|
| 1101 |
;---------------------------------------------------------------------------- |
| 1102 |
; Constraint management |
| 1103 |
;---------------------------------------------------------------------------- |
| 1104 |
|
| 1105 |
|
| 1106 |
; constraints |
| 1107 |
|
| 1108 |
(define gen-constr (lambda (a b) (cons a b))) |
| 1109 |
; generates an equality between tvar1 and tvar2 |
| 1110 |
|
| 1111 |
(define constr-lhs (lambda (c) (car c))) |
| 1112 |
; returns the left-hand side of a constraint |
| 1113 |
|
| 1114 |
(define constr-rhs (lambda (c) (cdr c))) |
| 1115 |
; returns the right-hand side of a constraint |
| 1116 |
|
| 1117 |
(define (constr-show c) |
| 1118 |
(cons (tvar-show (car c)) |
| 1119 |
(cons '= |
| 1120 |
(cons (tvar-show (cdr c)) '())))) |
| 1121 |
|
| 1122 |
|
| 1123 |
; constraint set management |
| 1124 |
|
| 1125 |
(define global-constraints '()) |
| 1126 |
|
| 1127 |
(define (init-global-constraints!) |
| 1128 |
(set! global-constraints '())) |
| 1129 |
|
| 1130 |
(define (add-constr! lhs rhs) |
| 1131 |
(set! global-constraints |
| 1132 |
(cons (gen-constr lhs rhs) global-constraints)) |
| 1133 |
'()) |
| 1134 |
|
| 1135 |
(define (glob-constr-show) |
| 1136 |
; returns printable version of global constraints |
| 1137 |
(map constr-show global-constraints)) |
| 1138 |
|
| 1139 |
|
| 1140 |
; constraint normalization |
| 1141 |
|
| 1142 |
; Needed packages: type management |
| 1143 |
|
| 1144 |
;(load "typ-mgmt.so") |
| 1145 |
|
| 1146 |
(define (normalize-global-constraints!) |
| 1147 |
(normalize! global-constraints) |
| 1148 |
(init-global-constraints!)) |
| 1149 |
|
| 1150 |
(define (normalize! constraints) |
| 1151 |
(map (lambda (c) |
| 1152 |
(equiv! (constr-lhs c) (constr-rhs c))) constraints)) |
| 1153 |
; ---------------------------------------------------------------------------- |
| 1154 |
; Abstract syntax definition and parse actions |
| 1155 |
; ---------------------------------------------------------------------------- |
| 1156 |
|
| 1157 |
; Needed packages: ast-gen.ss |
| 1158 |
;(load "ast-gen.ss") |
| 1159 |
|
| 1160 |
;; Abstract syntax |
| 1161 |
;; |
| 1162 |
;; VarDef |
| 1163 |
;; |
| 1164 |
;; Identifier = Symbol - SyntacticKeywords |
| 1165 |
;; SyntacticKeywords = { ... } (see Section 7.1, IEEE Scheme Standard) |
| 1166 |
;; |
| 1167 |
;; Datum |
| 1168 |
;; |
| 1169 |
;; null-const: Null -> Datum |
| 1170 |
;; boolean-const: Bool -> Datum |
| 1171 |
;; char-const: Char -> Datum |
| 1172 |
;; number-const: Number -> Datum |
| 1173 |
;; string-const: String -> Datum |
| 1174 |
;; vector-const: Datum* -> Datum |
| 1175 |
;; pair-const: Datum x Datum -> Datum |
| 1176 |
;; |
| 1177 |
;; Expr |
| 1178 |
;; |
| 1179 |
;; Datum < Expr |
| 1180 |
;; |
| 1181 |
;; var-def: Identifier -> VarDef |
| 1182 |
;; variable: VarDef -> Expr |
| 1183 |
;; identifier: Identifier -> Expr |
| 1184 |
;; procedure-call: Expr x Expr* -> Expr |
| 1185 |
;; lambda-expression: Formals x Body -> Expr |
| 1186 |
;; conditional: Expr x Expr x Expr -> Expr |
| 1187 |
;; assignment: Variable x Expr -> Expr |
| 1188 |
;; cond-expression: CondClause+ -> Expr |
| 1189 |
;; case-expression: Expr x CaseClause* -> Expr |
| 1190 |
;; and-expression: Expr* -> Expr |
| 1191 |
;; or-expression: Expr* -> Expr |
| 1192 |
;; let-expression: (VarDef* x Expr*) x Body -> Expr |
| 1193 |
;; named-let-expression: VarDef x (VarDef* x Expr*) x Body -> Expr |
| 1194 |
;; let*-expression: (VarDef* x Expr*) x Body -> Expr |
| 1195 |
;; letrec-expression: (VarDef* x Expr*) x Body -> Expr |
| 1196 |
;; begin-expression: Expr+ -> Expr |
| 1197 |
;; do-expression: IterDef* x CondClause x Expr* -> Expr |
| 1198 |
;; empty: -> Expr |
| 1199 |
;; |
| 1200 |
;; VarDef* < Formals |
| 1201 |
;; |
| 1202 |
;; simple-formal: VarDef -> Formals |
| 1203 |
;; dotted-formals: VarDef* x VarDef -> Formals |
| 1204 |
;; |
| 1205 |
;; Body = Definition* x Expr+ (reversed) |
| 1206 |
;; CondClause = Expr x Expr+ |
| 1207 |
;; CaseClause = Datum* x Expr+ |
| 1208 |
;; IterDef = VarDef x Expr x Expr |
| 1209 |
;; |
| 1210 |
;; Definition |
| 1211 |
;; |
| 1212 |
;; definition: Identifier x Expr -> Definition |
| 1213 |
;; function-definition: Identifier x Formals x Body -> Definition |
| 1214 |
;; begin-command: Definition* -> Definition |
| 1215 |
;; |
| 1216 |
;; Expr < Command |
| 1217 |
;; Definition < Command |
| 1218 |
;; |
| 1219 |
;; Program = Command* |
| 1220 |
|
| 1221 |
|
| 1222 |
;; Abstract syntax operators |
| 1223 |
|
| 1224 |
; Datum |
| 1225 |
|
| 1226 |
(define null-const 0) |
| 1227 |
(define boolean-const 1) |
| 1228 |
(define char-const 2) |
| 1229 |
(define number-const 3) |
| 1230 |
(define string-const 4) |
| 1231 |
(define symbol-const 5) |
| 1232 |
(define vector-const 6) |
| 1233 |
(define pair-const 7) |
| 1234 |
|
| 1235 |
; Bindings |
| 1236 |
|
| 1237 |
(define var-def 8) |
| 1238 |
(define null-def 29) |
| 1239 |
(define pair-def 30) |
| 1240 |
|
| 1241 |
; Expr |
| 1242 |
|
| 1243 |
(define variable 9) |
| 1244 |
(define identifier 10) |
| 1245 |
(define procedure-call 11) |
| 1246 |
(define lambda-expression 12) |
| 1247 |
(define conditional 13) |
| 1248 |
(define assignment 14) |
| 1249 |
(define cond-expression 15) |
| 1250 |
(define case-expression 16) |
| 1251 |
(define and-expression 17) |
| 1252 |
(define or-expression 18) |
| 1253 |
(define let-expression 19) |
| 1254 |
(define named-let-expression 20) |
| 1255 |
(define let*-expression 21) |
| 1256 |
(define letrec-expression 22) |
| 1257 |
(define begin-expression 23) |
| 1258 |
(define do-expression 24) |
| 1259 |
(define empty 25) |
| 1260 |
(define null-arg 31) |
| 1261 |
(define pair-arg 32) |
| 1262 |
|
| 1263 |
; Command |
| 1264 |
|
| 1265 |
(define definition 26) |
| 1266 |
(define function-definition 27) |
| 1267 |
(define begin-command 28) |
| 1268 |
|
| 1269 |
|
| 1270 |
;; Parse actions for abstract syntax construction |
| 1271 |
|
| 1272 |
(define (dynamic-parse-action-null-const) |
| 1273 |
;; dynamic-parse-action for '() |
| 1274 |
(ast-gen null-const '())) |
| 1275 |
|
| 1276 |
(define (dynamic-parse-action-boolean-const e) |
| 1277 |
;; dynamic-parse-action for #f and #t |
| 1278 |
(ast-gen boolean-const e)) |
| 1279 |
|
| 1280 |
(define (dynamic-parse-action-char-const e) |
| 1281 |
;; dynamic-parse-action for character constants |
| 1282 |
(ast-gen char-const e)) |
| 1283 |
|
| 1284 |
(define (dynamic-parse-action-number-const e) |
| 1285 |
;; dynamic-parse-action for number constants |
| 1286 |
(ast-gen number-const e)) |
| 1287 |
|
| 1288 |
(define (dynamic-parse-action-string-const e) |
| 1289 |
;; dynamic-parse-action for string literals |
| 1290 |
(ast-gen string-const e)) |
| 1291 |
|
| 1292 |
(define (dynamic-parse-action-symbol-const e) |
| 1293 |
;; dynamic-parse-action for symbol constants |
| 1294 |
(ast-gen symbol-const e)) |
| 1295 |
|
| 1296 |
(define (dynamic-parse-action-vector-const e) |
| 1297 |
;; dynamic-parse-action for vector literals |
| 1298 |
(ast-gen vector-const e)) |
| 1299 |
|
| 1300 |
(define (dynamic-parse-action-pair-const e1 e2) |
| 1301 |
;; dynamic-parse-action for pairs |
| 1302 |
(ast-gen pair-const (cons e1 e2))) |
| 1303 |
|
| 1304 |
(define (dynamic-parse-action-var-def e) |
| 1305 |
;; dynamic-parse-action for defining occurrences of variables; |
| 1306 |
;; e is a symbol |
| 1307 |
(ast-gen var-def e)) |
| 1308 |
|
| 1309 |
(define (dynamic-parse-action-null-formal) |
| 1310 |
;; dynamic-parse-action for null-list of formals |
| 1311 |
(ast-gen null-def '())) |
| 1312 |
|
| 1313 |
(define (dynamic-parse-action-pair-formal d1 d2) |
| 1314 |
;; dynamic-parse-action for non-null list of formals; |
| 1315 |
;; d1 is the result of parsing the first formal, |
| 1316 |
;; d2 the result of parsing the remaining formals |
| 1317 |
(ast-gen pair-def (cons d1 d2))) |
| 1318 |
|
| 1319 |
(define (dynamic-parse-action-variable e) |
| 1320 |
;; dynamic-parse-action for applied occurrences of variables |
| 1321 |
;; ***Note***: e is the result of a dynamic-parse-action on the |
| 1322 |
;; corresponding variable definition! |
| 1323 |
(ast-gen variable e)) |
| 1324 |
|
| 1325 |
(define (dynamic-parse-action-identifier e) |
| 1326 |
;; dynamic-parse-action for undeclared identifiers (free variable |
| 1327 |
;; occurrences) |
| 1328 |
;; ***Note***: e is a symbol (legal identifier) |
| 1329 |
(ast-gen identifier e)) |
| 1330 |
|
| 1331 |
(define (dynamic-parse-action-null-arg) |
| 1332 |
;; dynamic-parse-action for a null list of arguments in a procedure call |
| 1333 |
(ast-gen null-arg '())) |
| 1334 |
|
| 1335 |
(define (dynamic-parse-action-pair-arg a1 a2) |
| 1336 |
;; dynamic-parse-action for a non-null list of arguments in a procedure call |
| 1337 |
;; a1 is the result of parsing the first argument, |
| 1338 |
;; a2 the result of parsing the remaining arguments |
| 1339 |
(ast-gen pair-arg (cons a1 a2))) |
| 1340 |
|
| 1341 |
(define (dynamic-parse-action-procedure-call op args) |
| 1342 |
;; dynamic-parse-action for procedure calls: op function, args list of arguments |
| 1343 |
(ast-gen procedure-call (cons op args))) |
| 1344 |
|
| 1345 |
(define (dynamic-parse-action-lambda-expression formals body) |
| 1346 |
;; dynamic-parse-action for lambda-abstractions |
| 1347 |
(ast-gen lambda-expression (cons formals body))) |
| 1348 |
|
| 1349 |
(define (dynamic-parse-action-conditional test then-branch else-branch) |
| 1350 |
;; dynamic-parse-action for conditionals (if-then-else expressions) |
| 1351 |
(ast-gen conditional (cons test (cons then-branch else-branch)))) |
| 1352 |
|
| 1353 |
(define (dynamic-parse-action-empty) |
| 1354 |
;; dynamic-parse-action for missing or empty field |
| 1355 |
(ast-gen empty '())) |
| 1356 |
|
| 1357 |
(define (dynamic-parse-action-assignment lhs rhs) |
| 1358 |
;; dynamic-parse-action for assignment |
| 1359 |
(ast-gen assignment (cons lhs rhs))) |
| 1360 |
|
| 1361 |
(define (dynamic-parse-action-begin-expression body) |
| 1362 |
;; dynamic-parse-action for begin-expression |
| 1363 |
(ast-gen begin-expression body)) |
| 1364 |
|
| 1365 |
(define (dynamic-parse-action-cond-expression clauses) |
| 1366 |
;; dynamic-parse-action for cond-expressions |
| 1367 |
(ast-gen cond-expression clauses)) |
| 1368 |
|
| 1369 |
(define (dynamic-parse-action-and-expression args) |
| 1370 |
;; dynamic-parse-action for and-expressions |
| 1371 |
(ast-gen and-expression args)) |
| 1372 |
|
| 1373 |
(define (dynamic-parse-action-or-expression args) |
| 1374 |
;; dynamic-parse-action for or-expressions |
| 1375 |
(ast-gen or-expression args)) |
| 1376 |
|
| 1377 |
(define (dynamic-parse-action-case-expression key clauses) |
| 1378 |
;; dynamic-parse-action for case-expressions |
| 1379 |
(ast-gen case-expression (cons key clauses))) |
| 1380 |
|
| 1381 |
(define (dynamic-parse-action-let-expression bindings body) |
| 1382 |
;; dynamic-parse-action for let-expressions |
| 1383 |
(ast-gen let-expression (cons bindings body))) |
| 1384 |
|
| 1385 |
(define (dynamic-parse-action-named-let-expression variable bindings body) |
| 1386 |
;; dynamic-parse-action for named-let expressions |
| 1387 |
(ast-gen named-let-expression (cons variable (cons bindings body)))) |
| 1388 |
|
| 1389 |
(define (dynamic-parse-action-let*-expression bindings body) |
| 1390 |
;; dynamic-parse-action for let-expressions |
| 1391 |
(ast-gen let*-expression (cons bindings body))) |
| 1392 |
|
| 1393 |
(define (dynamic-parse-action-letrec-expression bindings body) |
| 1394 |
;; dynamic-parse-action for let-expressions |
| 1395 |
(ast-gen letrec-expression (cons bindings body))) |
| 1396 |
|
| 1397 |
(define (dynamic-parse-action-definition variable expr) |
| 1398 |
;; dynamic-parse-action for simple definitions |
| 1399 |
(ast-gen definition (cons variable expr))) |
| 1400 |
|
| 1401 |
(define (dynamic-parse-action-function-definition variable formals body) |
| 1402 |
;; dynamic-parse-action for function definitions |
| 1403 |
(ast-gen function-definition (cons variable (cons formals body)))) |
| 1404 |
|
| 1405 |
|
| 1406 |
(define dynamic-parse-action-commands (lambda (a b) (cons a b))) |
| 1407 |
;; dynamic-parse-action for processing a command result followed by a the |
| 1408 |
;; result of processing the remaining commands |
| 1409 |
|
| 1410 |
|
| 1411 |
;; Pretty-printing abstract syntax trees |
| 1412 |
|
| 1413 |
(define (ast-show ast) |
| 1414 |
;; converts abstract syntax tree to list representation (Scheme program) |
| 1415 |
;; ***Note***: check translation of constructors to numbers at the top of the file |
| 1416 |
(let ((syntax-op (ast-con ast)) |
| 1417 |
(syntax-arg (ast-arg ast))) |
| 1418 |
(case syntax-op |
| 1419 |
((0 1 2 3 4 8 10) syntax-arg) |
| 1420 |
((29 31) '()) |
| 1421 |
((30 32) (cons (ast-show (car syntax-arg)) (ast-show (cdr syntax-arg)))) |
| 1422 |
((5) (list 'quote syntax-arg)) |
| 1423 |
((6) (list->vector (map ast-show syntax-arg))) |
| 1424 |
((7) (list 'cons (ast-show (car syntax-arg)) (ast-show (cdr syntax-arg)))) |
| 1425 |
((9) (ast-arg syntax-arg)) |
| 1426 |
((11) (cons (ast-show (car syntax-arg)) (ast-show (cdr syntax-arg)))) |
| 1427 |
((12) (cons 'lambda (cons (ast-show (car syntax-arg)) |
| 1428 |
(map ast-show (cdr syntax-arg))))) |
| 1429 |
((13) (cons 'if (cons (ast-show (car syntax-arg)) |
| 1430 |
(cons (ast-show (cadr syntax-arg)) |
| 1431 |
(let ((alt (cddr syntax-arg))) |
| 1432 |
(if (eqv? (ast-con alt) empty) |
| 1433 |
'() |
| 1434 |
(list (ast-show alt)))))))) |
| 1435 |
((14) (list 'set! (ast-show (car syntax-arg)) (ast-show (cdr syntax-arg)))) |
| 1436 |
((15) (cons 'cond |
| 1437 |
(map (lambda (cc) |
| 1438 |
(let ((guard (car cc)) |
| 1439 |
(body (cdr cc))) |
| 1440 |
(cons |
| 1441 |
(if (eqv? (ast-con guard) empty) |
| 1442 |
'else |
| 1443 |
(ast-show guard)) |
| 1444 |
(map ast-show body)))) |
| 1445 |
syntax-arg))) |
| 1446 |
((16) (cons 'case |
| 1447 |
(cons (ast-show (car syntax-arg)) |
| 1448 |
(map (lambda (cc) |
| 1449 |
(let ((data (car cc))) |
| 1450 |
(if (and (pair? data) |
| 1451 |
(eqv? (ast-con (car data)) empty)) |
| 1452 |
(cons 'else |
| 1453 |
(map ast-show (cdr cc))) |
| 1454 |
(cons (map datum-show data) |
| 1455 |
(map ast-show (cdr cc)))))) |
| 1456 |
(cdr syntax-arg))))) |
| 1457 |
((17) (cons 'and (map ast-show syntax-arg))) |
| 1458 |
((18) (cons 'or (map ast-show syntax-arg))) |
| 1459 |
((19) (cons 'let |
| 1460 |
(cons (map |
| 1461 |
(lambda (vd e) |
| 1462 |
(list (ast-show vd) (ast-show e))) |
| 1463 |
(caar syntax-arg) |
| 1464 |
(cdar syntax-arg)) |
| 1465 |
(map ast-show (cdr syntax-arg))))) |
| 1466 |
((20) (cons 'let |
| 1467 |
(cons (ast-show (car syntax-arg)) |
| 1468 |
(cons (map |
| 1469 |
(lambda (vd e) |
| 1470 |
(list (ast-show vd) (ast-show e))) |
| 1471 |
(caadr syntax-arg) |
| 1472 |
(cdadr syntax-arg)) |
| 1473 |
(map ast-show (cddr syntax-arg)))))) |
| 1474 |
((21) (cons 'let* |
| 1475 |
(cons (map |
| 1476 |
(lambda (vd e) |
| 1477 |
(list (ast-show vd) (ast-show e))) |
| 1478 |
(caar syntax-arg) |
| 1479 |
(cdar syntax-arg)) |
| 1480 |
(map ast-show (cdr syntax-arg))))) |
| 1481 |
((22) (cons 'letrec |
| 1482 |
(cons (map |
| 1483 |
(lambda (vd e) |
| 1484 |
(list (ast-show vd) (ast-show e))) |
| 1485 |
(caar syntax-arg) |
| 1486 |
(cdar syntax-arg)) |
| 1487 |
(map ast-show (cdr syntax-arg))))) |
| 1488 |
((23) (cons 'begin |
| 1489 |
(map ast-show syntax-arg))) |
| 1490 |
((24) (fatal-error 'ast-show "Do expressions not handled! (~s)" syntax-arg)) |
| 1491 |
((25) (fatal-error 'ast-show "This can't happen: empty encountered!")) |
| 1492 |
((26) (list 'define |
| 1493 |
(ast-show (car syntax-arg)) |
| 1494 |
(ast-show (cdr syntax-arg)))) |
| 1495 |
((27) (cons 'define |
| 1496 |
(cons |
| 1497 |
(cons (ast-show (car syntax-arg)) |
| 1498 |
(ast-show (cadr syntax-arg))) |
| 1499 |
(map ast-show (cddr syntax-arg))))) |
| 1500 |
((28) (cons 'begin |
| 1501 |
(map ast-show syntax-arg))) |
| 1502 |
(else (fatal-error 'ast-show "Unknown abstract syntax operator: ~s" |
| 1503 |
syntax-op))))) |
| 1504 |
|
| 1505 |
|
| 1506 |
;; ast*-show |
| 1507 |
|
| 1508 |
(define (ast*-show p) |
| 1509 |
;; shows a list of abstract syntax trees |
| 1510 |
(map ast-show p)) |
| 1511 |
|
| 1512 |
|
| 1513 |
;; datum-show |
| 1514 |
|
| 1515 |
(define (datum-show ast) |
| 1516 |
;; prints an abstract syntax tree as a datum |
| 1517 |
(case (ast-con ast) |
| 1518 |
((0 1 2 3 4 5) (ast-arg ast)) |
| 1519 |
((6) (list->vector (map datum-show (ast-arg ast)))) |
| 1520 |
((7) (cons (datum-show (car (ast-arg ast))) (datum-show (cdr (ast-arg ast))))) |
| 1521 |
(else (fatal-error 'datum-show "This should not happen!")))) |
| 1522 |
|
| 1523 |
; write-to-port |
| 1524 |
|
| 1525 |
(define (write-to-port prog port) |
| 1526 |
; writes a program to a port |
| 1527 |
(for-each |
| 1528 |
(lambda (command) |
| 1529 |
(write command port) |
| 1530 |
(newline port)) |
| 1531 |
prog) |
| 1532 |
'()) |
| 1533 |
|
| 1534 |
; write-file |
| 1535 |
|
| 1536 |
(define (write-to-file prog filename) |
| 1537 |
; write a program to a file |
| 1538 |
(let ((port (open-output-file filename))) |
| 1539 |
(write-to-port prog port) |
| 1540 |
(close-output-port port) |
| 1541 |
'())) |
| 1542 |
|
| 1543 |
; ---------------------------------------------------------------------------- |
| 1544 |
; Typed abstract syntax tree management: constraint generation, display, etc. |
| 1545 |
; ---------------------------------------------------------------------------- |
| 1546 |
|
| 1547 |
|
| 1548 |
;; Abstract syntax operations, incl. constraint generation |
| 1549 |
|
| 1550 |
(define (ast-gen syntax-op arg) |
| 1551 |
; generates all attributes and performs semantic side effects |
| 1552 |
(let ((ntvar |
| 1553 |
(case syntax-op |
| 1554 |
((0 29 31) (null)) |
| 1555 |
((1) (boolean)) |
| 1556 |
((2) (character)) |
| 1557 |
((3) (number)) |
| 1558 |
((4) (charseq)) |
| 1559 |
((5) (symbol)) |
| 1560 |
((6) (let ((aux-tvar (gen-tvar))) |
| 1561 |
(for-each (lambda (t) |
| 1562 |
(add-constr! t aux-tvar)) |
| 1563 |
(map ast-tvar arg)) |
| 1564 |
(array aux-tvar))) |
| 1565 |
((7 30 32) (let ((t1 (ast-tvar (car arg))) |
| 1566 |
(t2 (ast-tvar (cdr arg)))) |
| 1567 |
(pair t1 t2))) |
| 1568 |
((8) (gen-tvar)) |
| 1569 |
((9) (ast-tvar arg)) |
| 1570 |
((10) (let ((in-env (dynamic-lookup arg dynamic-top-level-env))) |
| 1571 |
(if in-env |
| 1572 |
(instantiate-type (binding-value in-env)) |
| 1573 |
(let ((new-tvar (gen-tvar))) |
| 1574 |
(set! dynamic-top-level-env (extend-env-with-binding |
| 1575 |
dynamic-top-level-env |
| 1576 |
(gen-binding arg new-tvar))) |
| 1577 |
new-tvar)))) |
| 1578 |
((11) (let ((new-tvar (gen-tvar))) |
| 1579 |
(add-constr! (procedure (ast-tvar (cdr arg)) new-tvar) |
| 1580 |
(ast-tvar (car arg))) |
| 1581 |
new-tvar)) |
| 1582 |
((12) (procedure (ast-tvar (car arg)) |
| 1583 |
(ast-tvar (tail (cdr arg))))) |
| 1584 |
((13) (let ((t-test (ast-tvar (car arg))) |
| 1585 |
(t-consequent (ast-tvar (cadr arg))) |
| 1586 |
(t-alternate (ast-tvar (cddr arg)))) |
| 1587 |
(add-constr! (boolean) t-test) |
| 1588 |
(add-constr! t-consequent t-alternate) |
| 1589 |
t-consequent)) |
| 1590 |
((14) (let ((var-tvar (ast-tvar (car arg))) |
| 1591 |
(exp-tvar (ast-tvar (cdr arg)))) |
| 1592 |
(add-constr! var-tvar exp-tvar) |
| 1593 |
var-tvar)) |
| 1594 |
((15) (let ((new-tvar (gen-tvar))) |
| 1595 |
(for-each (lambda (body) |
| 1596 |
(add-constr! (ast-tvar (tail body)) new-tvar)) |
| 1597 |
(map cdr arg)) |
| 1598 |
(for-each (lambda (e) |
| 1599 |
(add-constr! (boolean) (ast-tvar e))) |
| 1600 |
(map car arg)) |
| 1601 |
new-tvar)) |
| 1602 |
((16) (let* ((new-tvar (gen-tvar)) |
| 1603 |
(t-key (ast-tvar (car arg))) |
| 1604 |
(case-clauses (cdr arg))) |
| 1605 |
(for-each (lambda (exprs) |
| 1606 |
(for-each (lambda (e) |
| 1607 |
(add-constr! (ast-tvar e) t-key)) |
| 1608 |
exprs)) |
| 1609 |
(map car case-clauses)) |
| 1610 |
(for-each (lambda (body) |
| 1611 |
(add-constr! (ast-tvar (tail body)) new-tvar)) |
| 1612 |
(map cdr case-clauses)) |
| 1613 |
new-tvar)) |
| 1614 |
((17 18) (for-each (lambda (e) |
| 1615 |
(add-constr! (boolean) (ast-tvar e))) |
| 1616 |
arg) |
| 1617 |
(boolean)) |
| 1618 |
((19 21 22) (let ((var-def-tvars (map ast-tvar (caar arg))) |
| 1619 |
(def-expr-types (map ast-tvar (cdar arg))) |
| 1620 |
(body-type (ast-tvar (tail (cdr arg))))) |
| 1621 |
(for-each add-constr! var-def-tvars def-expr-types) |
| 1622 |
body-type)) |
| 1623 |
((20) (let ((var-def-tvars (map ast-tvar (caadr arg))) |
| 1624 |
(def-expr-types (map ast-tvar (cdadr arg))) |
| 1625 |
(body-type (ast-tvar (tail (cddr arg)))) |
| 1626 |
(named-var-type (ast-tvar (car arg)))) |
| 1627 |
(for-each add-constr! var-def-tvars def-expr-types) |
| 1628 |
(add-constr! (procedure (convert-tvars var-def-tvars) body-type) |
| 1629 |
named-var-type) |
| 1630 |
body-type)) |
| 1631 |
((23) (ast-tvar (tail arg))) |
| 1632 |
((24) (fatal-error 'ast-gen |
| 1633 |
"Do-expressions not handled! (Argument: ~s) arg")) |
| 1634 |
((25) (gen-tvar)) |
| 1635 |
((26) (let ((t-var (ast-tvar (car arg))) |
| 1636 |
(t-exp (ast-tvar (cdr arg)))) |
| 1637 |
(add-constr! t-var t-exp) |
| 1638 |
t-var)) |
| 1639 |
((27) (let ((t-var (ast-tvar (car arg))) |
| 1640 |
(t-formals (ast-tvar (cadr arg))) |
| 1641 |
(t-body (ast-tvar (tail (cddr arg))))) |
| 1642 |
(add-constr! (procedure t-formals t-body) t-var) |
| 1643 |
t-var)) |
| 1644 |
((28) (gen-tvar)) |
| 1645 |
(else (fatal-error 'ast-gen "Can't handle syntax operator: ~s" syntax-op))))) |
| 1646 |
(cons syntax-op (cons ntvar arg)))) |
| 1647 |
|
| 1648 |
(define ast-con car) |
| 1649 |
;; extracts the ast-constructor from an abstract syntax tree |
| 1650 |
|
| 1651 |
(define ast-arg cddr) |
| 1652 |
;; extracts the ast-argument from an abstract syntax tree |
| 1653 |
|
| 1654 |
(define ast-tvar cadr) |
| 1655 |
;; extracts the tvar from an abstract syntax tree |
| 1656 |
|
| 1657 |
|
| 1658 |
;; tail |
| 1659 |
|
| 1660 |
(define (tail l) |
| 1661 |
;; returns the tail of a nonempty list |
| 1662 |
(if (null? (cdr l)) |
| 1663 |
(car l) |
| 1664 |
(tail (cdr l)))) |
| 1665 |
|
| 1666 |
; convert-tvars |
| 1667 |
|
| 1668 |
(define (convert-tvars tvar-list) |
| 1669 |
;; converts a list of tvars to a single tvar |
| 1670 |
(cond |
| 1671 |
((null? tvar-list) (null)) |
| 1672 |
((pair? tvar-list) (pair (car tvar-list) |
| 1673 |
(convert-tvars (cdr tvar-list)))) |
| 1674 |
(else (fatal-error 'convert-tvars "Not a list of tvars: ~s" tvar-list)))) |
| 1675 |
|
| 1676 |
|
| 1677 |
;; Pretty-printing abstract syntax trees |
| 1678 |
|
| 1679 |
(define (tast-show ast) |
| 1680 |
;; converts abstract syntax tree to list representation (Scheme program) |
| 1681 |
(let ((syntax-op (ast-con ast)) |
| 1682 |
(syntax-tvar (tvar-show (ast-tvar ast))) |
| 1683 |
(syntax-arg (ast-arg ast))) |
| 1684 |
(cons |
| 1685 |
(case syntax-op |
| 1686 |
((0 1 2 3 4 8 10) syntax-arg) |
| 1687 |
((29 31) '()) |
| 1688 |
((30 32) (cons (tast-show (car syntax-arg)) |
| 1689 |
(tast-show (cdr syntax-arg)))) |
| 1690 |
((5) (list 'quote syntax-arg)) |
| 1691 |
((6) (list->vector (map tast-show syntax-arg))) |
| 1692 |
((7) (list 'cons (tast-show (car syntax-arg)) |
| 1693 |
(tast-show (cdr syntax-arg)))) |
| 1694 |
((9) (ast-arg syntax-arg)) |
| 1695 |
((11) (cons (tast-show (car syntax-arg)) (tast-show (cdr syntax-arg)))) |
| 1696 |
((12) (cons 'lambda (cons (tast-show (car syntax-arg)) |
| 1697 |
(map tast-show (cdr syntax-arg))))) |
| 1698 |
((13) (cons 'if (cons (tast-show (car syntax-arg)) |
| 1699 |
(cons (tast-show (cadr syntax-arg)) |
| 1700 |
(let ((alt (cddr syntax-arg))) |
| 1701 |
(if (eqv? (ast-con alt) empty) |
| 1702 |
'() |
| 1703 |
(list (tast-show alt)))))))) |
| 1704 |
((14) (list 'set! (tast-show (car syntax-arg)) |
| 1705 |
(tast-show (cdr syntax-arg)))) |
| 1706 |
((15) (cons 'cond |
| 1707 |
(map (lambda (cc) |
| 1708 |
(let ((guard (car cc)) |
| 1709 |
(body (cdr cc))) |
| 1710 |
(cons |
| 1711 |
(if (eqv? (ast-con guard) empty) |
| 1712 |
'else |
| 1713 |
(tast-show guard)) |
| 1714 |
(map tast-show body)))) |
| 1715 |
syntax-arg))) |
| 1716 |
((16) (cons 'case |
| 1717 |
(cons (tast-show (car syntax-arg)) |
| 1718 |
(map (lambda (cc) |
| 1719 |
(let ((data (car cc))) |
| 1720 |
(if (and (pair? data) |
| 1721 |
(eqv? (ast-con (car data)) empty)) |
| 1722 |
(cons 'else |
| 1723 |
(map tast-show (cdr cc))) |
| 1724 |
(cons (map datum-show data) |
| 1725 |
(map tast-show (cdr cc)))))) |
| 1726 |
(cdr syntax-arg))))) |
| 1727 |
((17) (cons 'and (map tast-show syntax-arg))) |
| 1728 |
((18) (cons 'or (map tast-show syntax-arg))) |
| 1729 |
((19) (cons 'let |
| 1730 |
(cons (map |
| 1731 |
(lambda (vd e) |
| 1732 |
(list (tast-show vd) (tast-show e))) |
| 1733 |
(caar syntax-arg) |
| 1734 |
(cdar syntax-arg)) |
| 1735 |
(map tast-show (cdr syntax-arg))))) |
| 1736 |
((20) (cons 'let |
| 1737 |
(cons (tast-show (car syntax-arg)) |
| 1738 |
(cons (map |
| 1739 |
(lambda (vd e) |
| 1740 |
(list (tast-show vd) (tast-show e))) |
| 1741 |
(caadr syntax-arg) |
| 1742 |
(cdadr syntax-arg)) |
| 1743 |
(map tast-show (cddr syntax-arg)))))) |
| 1744 |
((21) (cons 'let* |
| 1745 |
(cons (map |
| 1746 |
(lambda (vd e) |
| 1747 |
(list (tast-show vd) (tast-show e))) |
| 1748 |
(caar syntax-arg) |
| 1749 |
(cdar syntax-arg)) |
| 1750 |
(map tast-show (cdr syntax-arg))))) |
| 1751 |
((22) (cons 'letrec |
| 1752 |
(cons (map |
| 1753 |
(lambda (vd e) |
| 1754 |
(list (tast-show vd) (tast-show e))) |
| 1755 |
(caar syntax-arg) |
| 1756 |
(cdar syntax-arg)) |
| 1757 |
(map tast-show (cdr syntax-arg))))) |
| 1758 |
((23) (cons 'begin |
| 1759 |
(map tast-show syntax-arg))) |
| 1760 |
((24) (fatal-error 'tast-show "Do expressions not handled! (~s)" syntax-arg)) |
| 1761 |
((25) (fatal-error 'tast-show "This can't happen: empty encountered!")) |
| 1762 |
((26) (list 'define |
| 1763 |
(tast-show (car syntax-arg)) |
| 1764 |
(tast-show (cdr syntax-arg)))) |
| 1765 |
((27) (cons 'define |
| 1766 |
(cons |
| 1767 |
(cons (tast-show (car syntax-arg)) |
| 1768 |
(tast-show (cadr syntax-arg))) |
| 1769 |
(map tast-show (cddr syntax-arg))))) |
| 1770 |
((28) (cons 'begin |
| 1771 |
(map tast-show syntax-arg))) |
| 1772 |
(else (fatal-error 'tast-show "Unknown abstract syntax operator: ~s" |
| 1773 |
syntax-op))) |
| 1774 |
syntax-tvar))) |
| 1775 |
|
| 1776 |
;; tast*-show |
| 1777 |
|
| 1778 |
(define (tast*-show p) |
| 1779 |
;; shows a list of abstract syntax trees |
| 1780 |
(map tast-show p)) |
| 1781 |
|
| 1782 |
|
| 1783 |
;; counters for tagging/untagging |
| 1784 |
|
| 1785 |
(define untag-counter 0) |
| 1786 |
(define no-untag-counter 0) |
| 1787 |
(define tag-counter 0) |
| 1788 |
(define no-tag-counter 0) |
| 1789 |
(define may-untag-counter 0) |
| 1790 |
(define no-may-untag-counter 0) |
| 1791 |
|
| 1792 |
(define (reset-counters!) |
| 1793 |
(set! untag-counter 0) |
| 1794 |
(set! no-untag-counter 0) |
| 1795 |
(set! tag-counter 0) |
| 1796 |
(set! no-tag-counter 0) |
| 1797 |
(set! may-untag-counter 0) |
| 1798 |
(set! no-may-untag-counter 0)) |
| 1799 |
|
| 1800 |
(define (counters-show) |
| 1801 |
(list |
| 1802 |
(cons tag-counter no-tag-counter) |
| 1803 |
(cons untag-counter no-untag-counter) |
| 1804 |
(cons may-untag-counter no-may-untag-counter))) |
| 1805 |
|
| 1806 |
|
| 1807 |
;; tag-show |
| 1808 |
|
| 1809 |
(define (tag-show tvar-rep prog) |
| 1810 |
; display prog with tagging operation |
| 1811 |
(if (eqv? tvar-rep dynamic) |
| 1812 |
(begin |
| 1813 |
(set! tag-counter (+ tag-counter 1)) |
| 1814 |
(list 'tag prog)) |
| 1815 |
(begin |
| 1816 |
(set! no-tag-counter (+ no-tag-counter 1)) |
| 1817 |
(list 'no-tag prog)))) |
| 1818 |
|
| 1819 |
|
| 1820 |
;; untag-show |
| 1821 |
|
| 1822 |
(define (untag-show tvar-rep prog) |
| 1823 |
; display prog with untagging operation |
| 1824 |
(if (eqv? tvar-rep dynamic) |
| 1825 |
(begin |
| 1826 |
(set! untag-counter (+ untag-counter 1)) |
| 1827 |
(list 'untag prog)) |
| 1828 |
(begin |
| 1829 |
(set! no-untag-counter (+ no-untag-counter 1)) |
| 1830 |
(list 'no-untag prog)))) |
| 1831 |
|
| 1832 |
(define (may-untag-show tvar-rep prog) |
| 1833 |
; display possible untagging in actual arguments |
| 1834 |
(if (eqv? tvar-rep dynamic) |
| 1835 |
(begin |
| 1836 |
(set! may-untag-counter (+ may-untag-counter 1)) |
| 1837 |
(list 'may-untag prog)) |
| 1838 |
(begin |
| 1839 |
(set! no-may-untag-counter (+ no-may-untag-counter 1)) |
| 1840 |
(list 'no-may-untag prog)))) |
| 1841 |
|
| 1842 |
|
| 1843 |
;; tag-ast-show |
| 1844 |
|
| 1845 |
(define (tag-ast-show ast) |
| 1846 |
;; converts typed and normalized abstract syntax tree to |
| 1847 |
;; a Scheme program with explicit tagging and untagging operations |
| 1848 |
(let ((syntax-op (ast-con ast)) |
| 1849 |
(syntax-tvar (find! (ast-tvar ast))) |
| 1850 |
(syntax-arg (ast-arg ast))) |
| 1851 |
(case syntax-op |
| 1852 |
((0 1 2 3 4) |
| 1853 |
(tag-show syntax-tvar syntax-arg)) |
| 1854 |
((8 10) syntax-arg) |
| 1855 |
((29 31) '()) |
| 1856 |
((30) (cons (tag-ast-show (car syntax-arg)) |
| 1857 |
(tag-ast-show (cdr syntax-arg)))) |
| 1858 |
((32) (cons (may-untag-show (find! (ast-tvar (car syntax-arg))) |
| 1859 |
(tag-ast-show (car syntax-arg))) |
| 1860 |
(tag-ast-show (cdr syntax-arg)))) |
| 1861 |
((5) (tag-show syntax-tvar (list 'quote syntax-arg))) |
| 1862 |
((6) (tag-show syntax-tvar (list->vector (map tag-ast-show syntax-arg)))) |
| 1863 |
((7) (tag-show syntax-tvar (list 'cons (tag-ast-show (car syntax-arg)) |
| 1864 |
(tag-ast-show (cdr syntax-arg))))) |
| 1865 |
((9) (ast-arg syntax-arg)) |
| 1866 |
((11) (let ((proc-tvar (find! (ast-tvar (car syntax-arg))))) |
| 1867 |
(cons (untag-show proc-tvar |
| 1868 |
(tag-ast-show (car syntax-arg))) |
| 1869 |
(tag-ast-show (cdr syntax-arg))))) |
| 1870 |
((12) (tag-show syntax-tvar |
| 1871 |
(cons 'lambda (cons (tag-ast-show (car syntax-arg)) |
| 1872 |
(map tag-ast-show (cdr syntax-arg)))))) |
| 1873 |
((13) (let ((test-tvar (find! (ast-tvar (car syntax-arg))))) |
| 1874 |
(cons 'if (cons (untag-show test-tvar |
| 1875 |
(tag-ast-show (car syntax-arg))) |
| 1876 |
(cons (tag-ast-show (cadr syntax-arg)) |
| 1877 |
(let ((alt (cddr syntax-arg))) |
| 1878 |
(if (eqv? (ast-con alt) empty) |
| 1879 |
'() |
| 1880 |
(list (tag-ast-show alt))))))))) |
| 1881 |
((14) (list 'set! (tag-ast-show (car syntax-arg)) |
| 1882 |
(tag-ast-show (cdr syntax-arg)))) |
| 1883 |
((15) (cons 'cond |
| 1884 |
(map (lambda (cc) |
| 1885 |
(let ((guard (car cc)) |
| 1886 |
(body (cdr cc))) |
| 1887 |
(cons |
| 1888 |
(if (eqv? (ast-con guard) empty) |
| 1889 |
'else |
| 1890 |
(untag-show (find! (ast-tvar guard)) |
| 1891 |
(tag-ast-show guard))) |
| 1892 |
(map tag-ast-show body)))) |
| 1893 |
syntax-arg))) |
| 1894 |
((16) (cons 'case |
| 1895 |
(cons (tag-ast-show (car syntax-arg)) |
| 1896 |
(map (lambda (cc) |
| 1897 |
(let ((data (car cc))) |
| 1898 |
(if (and (pair? data) |
| 1899 |
(eqv? (ast-con (car data)) empty)) |
| 1900 |
(cons 'else |
| 1901 |
(map tag-ast-show (cdr cc))) |
| 1902 |
(cons (map datum-show data) |
| 1903 |
(map tag-ast-show (cdr cc)))))) |
| 1904 |
(cdr syntax-arg))))) |
| 1905 |
((17) (cons 'and (map |
| 1906 |
(lambda (ast) |
| 1907 |
(let ((bool-tvar (find! (ast-tvar ast)))) |
| 1908 |
(untag-show bool-tvar (tag-ast-show ast)))) |
| 1909 |
syntax-arg))) |
| 1910 |
((18) (cons 'or (map |
| 1911 |
(lambda (ast) |
| 1912 |
(let ((bool-tvar (find! (ast-tvar ast)))) |
| 1913 |
(untag-show bool-tvar (tag-ast-show ast)))) |
| 1914 |
syntax-arg))) |
| 1915 |
((19) (cons 'let |
| 1916 |
(cons (map |
| 1917 |
(lambda (vd e) |
| 1918 |
(list (tag-ast-show vd) (tag-ast-show e))) |
| 1919 |
(caar syntax-arg) |
| 1920 |
(cdar syntax-arg)) |
| 1921 |
(map tag-ast-show (cdr syntax-arg))))) |
| 1922 |
((20) (cons 'let |
| 1923 |
(cons (tag-ast-show (car syntax-arg)) |
| 1924 |
(cons (map |
| 1925 |
(lambda (vd e) |
| 1926 |
(list (tag-ast-show vd) (tag-ast-show e))) |
| 1927 |
(caadr syntax-arg) |
| 1928 |
(cdadr syntax-arg)) |
| 1929 |
(map tag-ast-show (cddr syntax-arg)))))) |
| 1930 |
((21) (cons 'let* |
| 1931 |
(cons (map |
| 1932 |
(lambda (vd e) |
| 1933 |
(list (tag-ast-show vd) (tag-ast-show e))) |
| 1934 |
(caar syntax-arg) |
| 1935 |
(cdar syntax-arg)) |
| 1936 |
(map tag-ast-show (cdr syntax-arg))))) |
| 1937 |
((22) (cons 'letrec |
| 1938 |
(cons (map |
| 1939 |
(lambda (vd e) |
| 1940 |
(list (tag-ast-show vd) (tag-ast-show e))) |
| 1941 |
(caar syntax-arg) |
| 1942 |
(cdar syntax-arg)) |
| 1943 |
(map tag-ast-show (cdr syntax-arg))))) |
| 1944 |
((23) (cons 'begin |
| 1945 |
(map tag-ast-show syntax-arg))) |
| 1946 |
((24) (fatal-error 'tag-ast-show "Do expressions not handled! (~s)" syntax-arg)) |
| 1947 |
((25) (fatal-error 'tag-ast-show "This can't happen: empty encountered!")) |
| 1948 |
((26) (list 'define |
| 1949 |
(tag-ast-show (car syntax-arg)) |
| 1950 |
(tag-ast-show (cdr syntax-arg)))) |
| 1951 |
((27) (let ((func-tvar (find! (ast-tvar (car syntax-arg))))) |
| 1952 |
(list 'define |
| 1953 |
(tag-ast-show (car syntax-arg)) |
| 1954 |
(tag-show func-tvar |
| 1955 |
(cons 'lambda |
| 1956 |
(cons (tag-ast-show (cadr syntax-arg)) |
| 1957 |
(map tag-ast-show (cddr syntax-arg)))))))) |
| 1958 |
((28) (cons 'begin |
| 1959 |
(map tag-ast-show syntax-arg))) |
| 1960 |
(else (fatal-error 'tag-ast-show "Unknown abstract syntax operator: ~s" |
| 1961 |
syntax-op))))) |
| 1962 |
|
| 1963 |
|
| 1964 |
; tag-ast*-show |
| 1965 |
|
| 1966 |
(define (tag-ast*-show p) |
| 1967 |
; display list of commands/expressions with tagging/untagging |
| 1968 |
; operations |
| 1969 |
(map tag-ast-show p)) |
| 1970 |
; ---------------------------------------------------------------------------- |
| 1971 |
; Top level type environment |
| 1972 |
; ---------------------------------------------------------------------------- |
| 1973 |
|
| 1974 |
|
| 1975 |
; Needed packages: type management (monomorphic and polymorphic) |
| 1976 |
|
| 1977 |
;(load "typ-mgmt.ss") |
| 1978 |
;(load "ptyp-mgm.ss") |
| 1979 |
|
| 1980 |
|
| 1981 |
; type environment for miscellaneous |
| 1982 |
|
| 1983 |
(define misc-env |
| 1984 |
(list |
| 1985 |
(cons 'quote (forall (lambda (tv) tv))) |
| 1986 |
(cons 'eqv? (forall (lambda (tv) (procedure (convert-tvars (list tv tv)) |
| 1987 |
(boolean))))) |
| 1988 |
(cons 'eq? (forall (lambda (tv) (procedure (convert-tvars (list tv tv)) |
| 1989 |
(boolean))))) |
| 1990 |
(cons 'equal? (forall (lambda (tv) (procedure (convert-tvars (list tv tv)) |
| 1991 |
(boolean))))) |
| 1992 |
)) |
| 1993 |
|
| 1994 |
; type environment for input/output |
| 1995 |
|
| 1996 |
(define io-env |
| 1997 |
(list |
| 1998 |
(cons 'open-input-file (procedure (convert-tvars (list (charseq))) dynamic)) |
| 1999 |
(cons 'eof-object? (procedure (convert-tvars (list dynamic)) (boolean))) |
| 2000 |
(cons 'read (forall (lambda (tv) |
| 2001 |
(procedure (convert-tvars (list tv)) dynamic)))) |
| 2002 |
(cons 'write (forall (lambda (tv) |
| 2003 |
(procedure (convert-tvars (list tv)) dynamic)))) |
| 2004 |
(cons 'display (forall (lambda (tv) |
| 2005 |
(procedure (convert-tvars (list tv)) dynamic)))) |
| 2006 |
(cons 'newline (procedure (null) dynamic)) |
| 2007 |
(cons 'pretty-print (forall (lambda (tv) |
| 2008 |
(procedure (convert-tvars (list tv)) dynamic)))))) |
| 2009 |
|
| 2010 |
|
| 2011 |
; type environment for Booleans |
| 2012 |
|
| 2013 |
(define boolean-env |
| 2014 |
(list |
| 2015 |
(cons 'boolean? (forall (lambda (tv) |
| 2016 |
(procedure (convert-tvars (list tv)) (boolean))))) |
| 2017 |
;(cons #f (boolean)) |
| 2018 |
; #f doesn't exist in Chez Scheme, but gets mapped to null! |
| 2019 |
(cons #t (boolean)) |
| 2020 |
(cons 'not (procedure (convert-tvars (list (boolean))) (boolean))) |
| 2021 |
)) |
| 2022 |
|
| 2023 |
|
| 2024 |
; type environment for pairs and lists |
| 2025 |
|
| 2026 |
(define (list-type tv) |
| 2027 |
(fix (lambda (tv2) (pair tv tv2)))) |
| 2028 |
|
| 2029 |
(define list-env |
| 2030 |
(list |
| 2031 |
(cons 'pair? (forall2 (lambda (tv1 tv2) |
| 2032 |
(procedure (convert-tvars (list (pair tv1 tv2))) |
| 2033 |
(boolean))))) |
| 2034 |
(cons 'null? (forall2 (lambda (tv1 tv2) |
| 2035 |
(procedure (convert-tvars (list (pair tv1 tv2))) |
| 2036 |
(boolean))))) |
| 2037 |
(cons 'list? (forall2 (lambda (tv1 tv2) |
| 2038 |
(procedure (convert-tvars (list (pair tv1 tv2))) |
| 2039 |
(boolean))))) |
| 2040 |
(cons 'cons (forall2 (lambda (tv1 tv2) |
| 2041 |
(procedure (convert-tvars (list tv1 tv2)) |
| 2042 |
(pair tv1 tv2))))) |
| 2043 |
(cons 'car (forall2 (lambda (tv1 tv2) |
| 2044 |
(procedure (convert-tvars (list (pair tv1 tv2))) |
| 2045 |
tv1)))) |
| 2046 |
(cons 'cdr (forall2 (lambda (tv1 tv2) |
| 2047 |
(procedure (convert-tvars (list (pair tv1 tv2))) |
| 2048 |
tv2)))) |
| 2049 |
(cons 'set-car! (forall2 (lambda (tv1 tv2) |
| 2050 |
(procedure (convert-tvars (list (pair tv1 tv2) |
| 2051 |
tv1)) |
| 2052 |
dynamic)))) |
| 2053 |
(cons 'set-cdr! (forall2 (lambda (tv1 tv2) |
| 2054 |
(procedure (convert-tvars (list (pair tv1 tv2) |
| 2055 |
tv2)) |
| 2056 |
dynamic)))) |
| 2057 |
(cons 'caar (forall3 (lambda (tv1 tv2 tv3) |
| 2058 |
(procedure (convert-tvars |
| 2059 |
(list (pair (pair tv1 tv2) tv3))) |
| 2060 |
tv1)))) |
| 2061 |
(cons 'cdar (forall3 (lambda (tv1 tv2 tv3) |
| 2062 |
(procedure (convert-tvars |
| 2063 |
(list (pair (pair tv1 tv2) tv3))) |
| 2064 |
tv2)))) |
| 2065 |
|
| 2066 |
(cons 'cadr (forall3 (lambda (tv1 tv2 tv3) |
| 2067 |
(procedure (convert-tvars |
| 2068 |
(list (pair tv1 (pair tv2 tv3)))) |
| 2069 |
tv2)))) |
| 2070 |
(cons 'cddr (forall3 (lambda (tv1 tv2 tv3) |
| 2071 |
(procedure (convert-tvars |
| 2072 |
(list (pair tv1 (pair tv2 tv3)))) |
| 2073 |
tv3)))) |
| 2074 |
(cons 'caaar (forall4 |
| 2075 |
(lambda (tv1 tv2 tv3 tv4) |
| 2076 |
(procedure (convert-tvars |
| 2077 |
(list (pair (pair (pair tv1 tv2) tv3) tv4))) |
| 2078 |
tv1)))) |
| 2079 |
(cons 'cdaar (forall4 |
| 2080 |
(lambda (tv1 tv2 tv3 tv4) |
| 2081 |
(procedure (convert-tvars |
| 2082 |
(list (pair (pair (pair tv1 tv2) tv3) tv4))) |
| 2083 |
tv2)))) |
| 2084 |
(cons 'cadar (forall4 |
| 2085 |
(lambda (tv1 tv2 tv3 tv4) |
| 2086 |
(procedure (convert-tvars |
| 2087 |
(list (pair (pair tv1 (pair tv2 tv3)) tv4))) |
| 2088 |
tv2)))) |
| 2089 |
(cons 'cddar (forall4 |
| 2090 |
(lambda (tv1 tv2 tv3 tv4) |
| 2091 |
(procedure (convert-tvars |
| 2092 |
(list (pair (pair tv1 (pair tv2 tv3)) tv4))) |
| 2093 |
tv3)))) |
| 2094 |
(cons 'caadr (forall4 |
| 2095 |
(lambda (tv1 tv2 tv3 tv4) |
| 2096 |
(procedure (convert-tvars |
| 2097 |
(list (pair tv1 (pair (pair tv2 tv3) tv4)))) |
| 2098 |
tv2)))) |
| 2099 |
(cons 'cdadr (forall4 |
| 2100 |
(lambda (tv1 tv2 tv3 tv4) |
| 2101 |
(procedure (convert-tvars |
| 2102 |
(list (pair tv1 (pair (pair tv2 tv3) tv4)))) |
| 2103 |
tv3)))) |
| 2104 |
(cons 'caddr (forall4 |
| 2105 |
(lambda (tv1 tv2 tv3 tv4) |
| 2106 |
(procedure (convert-tvars |
| 2107 |
(list (pair tv1 (pair tv2 (pair tv3 tv4))))) |
| 2108 |
tv3)))) |
| 2109 |
(cons 'cdddr (forall4 |
| 2110 |
(lambda (tv1 tv2 tv3 tv4) |
| 2111 |
(procedure (convert-tvars |
| 2112 |
(list (pair tv1 (pair tv2 (pair tv3 tv4))))) |
| 2113 |
tv4)))) |
| 2114 |
(cons 'cadddr |
| 2115 |
(forall5 (lambda (tv1 tv2 tv3 tv4 tv5) |
| 2116 |
(procedure (convert-tvars |
| 2117 |
(list (pair tv1 |
| 2118 |
(pair tv2 |
| 2119 |
(pair tv3 |
| 2120 |
(pair tv4 tv5)))))) |
| 2121 |
tv4)))) |
| 2122 |
(cons 'cddddr |
| 2123 |
(forall5 (lambda (tv1 tv2 tv3 tv4 tv5) |
| 2124 |
(procedure (convert-tvars |
| 2125 |
(list (pair tv1 |
| 2126 |
(pair tv2 |
| 2127 |
(pair tv3 |
| 2128 |
(pair tv4 tv5)))))) |
| 2129 |
tv5)))) |
| 2130 |
(cons 'list (forall (lambda (tv) |
| 2131 |
(procedure tv tv)))) |
| 2132 |
(cons 'length (forall (lambda (tv) |
| 2133 |
(procedure (convert-tvars (list (list-type tv))) |
| 2134 |
(number))))) |
| 2135 |
(cons 'append (forall (lambda (tv) |
| 2136 |
(procedure (convert-tvars (list (list-type tv) |
| 2137 |
(list-type tv))) |
| 2138 |
(list-type tv))))) |
| 2139 |
(cons 'reverse (forall (lambda (tv) |
| 2140 |
(procedure (convert-tvars (list (list-type tv))) |
| 2141 |
(list-type tv))))) |
| 2142 |
(cons 'list-ref (forall (lambda (tv) |
| 2143 |
(procedure (convert-tvars (list (list-type tv) |
| 2144 |
(number))) |
| 2145 |
tv)))) |
| 2146 |
(cons 'memq (forall (lambda (tv) |
| 2147 |
(procedure (convert-tvars (list tv |
| 2148 |
(list-type tv))) |
| 2149 |
(boolean))))) |
| 2150 |
(cons 'memv (forall (lambda (tv) |
| 2151 |
(procedure (convert-tvars (list tv |
| 2152 |
(list-type tv))) |
| 2153 |
(boolean))))) |
| 2154 |
(cons 'member (forall (lambda (tv) |
| 2155 |
(procedure (convert-tvars (list tv |
| 2156 |
(list-type tv))) |
| 2157 |
(boolean))))) |
| 2158 |
(cons 'assq (forall2 (lambda (tv1 tv2) |
| 2159 |
(procedure (convert-tvars |
| 2160 |
(list tv1 |
| 2161 |
(list-type (pair tv1 tv2)))) |
| 2162 |
(pair tv1 tv2))))) |
| 2163 |
(cons 'assv (forall2 (lambda (tv1 tv2) |
| 2164 |
(procedure (convert-tvars |
| 2165 |
(list tv1 |
| 2166 |
(list-type (pair tv1 tv2)))) |
| 2167 |
(pair tv1 tv2))))) |
| 2168 |
(cons 'assoc (forall2 (lambda (tv1 tv2) |
| 2169 |
(procedure (convert-tvars |
| 2170 |
(list tv1 |
| 2171 |
(list-type (pair tv1 tv2)))) |
| 2172 |
(pair tv1 tv2))))) |
| 2173 |
)) |
| 2174 |
|
| 2175 |
|
| 2176 |
(define symbol-env |
| 2177 |
(list |
| 2178 |
(cons 'symbol? (forall (lambda (tv) |
| 2179 |
(procedure (convert-tvars (list tv)) (boolean))))) |
| 2180 |
(cons 'symbol->string (procedure (convert-tvars (list (symbol))) (charseq))) |
| 2181 |
(cons 'string->symbol (procedure (convert-tvars (list (charseq))) (symbol))) |
| 2182 |
)) |
| 2183 |
|
| 2184 |
(define number-env |
| 2185 |
(list |
| 2186 |
(cons 'number? (forall (lambda (tv) |
| 2187 |
(procedure (convert-tvars (list tv)) (boolean))))) |
| 2188 |
(cons '+ (procedure (convert-tvars (list (number) (number))) (number))) |
| 2189 |
(cons '- (procedure (convert-tvars (list (number) (number))) (number))) |
| 2190 |
(cons '* (procedure (convert-tvars (list (number) (number))) (number))) |
| 2191 |
(cons '/ (procedure (convert-tvars (list (number) (number))) (number))) |
| 2192 |
(cons 'number->string (procedure (convert-tvars (list (number))) (charseq))) |
| 2193 |
(cons 'string->number (procedure (convert-tvars (list (charseq))) (number))) |
| 2194 |
)) |
| 2195 |
|
| 2196 |
(define char-env |
| 2197 |
(list |
| 2198 |
(cons 'char? (forall (lambda (tv) |
| 2199 |
(procedure (convert-tvars (list tv)) (boolean))))) |
| 2200 |
(cons 'char->integer (procedure (convert-tvars (list (character))) |
| 2201 |
(number))) |
| 2202 |
(cons 'integer->char (procedure (convert-tvars (list (number))) |
| 2203 |
(character))) |
| 2204 |
)) |
| 2205 |
|
| 2206 |
(define string-env |
| 2207 |
(list |
| 2208 |
(cons 'string? (forall (lambda (tv) |
| 2209 |
(procedure (convert-tvars (list tv)) (boolean))))) |
| 2210 |
)) |
| 2211 |
|
| 2212 |
(define vector-env |
| 2213 |
(list |
| 2214 |
(cons 'vector? (forall (lambda (tv) |
| 2215 |
(procedure (convert-tvars (list tv)) (boolean))))) |
| 2216 |
(cons 'make-vector (forall (lambda (tv) |
| 2217 |
(procedure (convert-tvars (list (number))) |
| 2218 |
(array tv))))) |
| 2219 |
(cons 'vector-length (forall (lambda (tv) |
| 2220 |
(procedure (convert-tvars (list (array tv))) |
| 2221 |
(number))))) |
| 2222 |
(cons 'vector-ref (forall (lambda (tv) |
| 2223 |
(procedure (convert-tvars (list (array tv) |
| 2224 |
(number))) |
| 2225 |
tv)))) |
| 2226 |
(cons 'vector-set! (forall (lambda (tv) |
| 2227 |
(procedure (convert-tvars (list (array tv) |
| 2228 |
(number) |
| 2229 |
tv)) |
| 2230 |
dynamic)))) |
| 2231 |
)) |
| 2232 |
|
| 2233 |
(define procedure-env |
| 2234 |
(list |
| 2235 |
(cons 'procedure? (forall (lambda (tv) |
| 2236 |
(procedure (convert-tvars (list tv)) (boolean))))) |
| 2237 |
(cons 'map (forall2 (lambda (tv1 tv2) |
| 2238 |
(procedure (convert-tvars |
| 2239 |
(list (procedure (convert-tvars |
| 2240 |
(list tv1)) tv2) |
| 2241 |
(list-type tv1))) |
| 2242 |
(list-type tv2))))) |
| 2243 |
(cons 'foreach (forall2 (lambda (tv1 tv2) |
| 2244 |
(procedure (convert-tvars |
| 2245 |
(list (procedure (convert-tvars |
| 2246 |
(list tv1)) tv2) |
| 2247 |
(list-type tv1))) |
| 2248 |
(list-type tv2))))) |
| 2249 |
(cons 'call-with-current-continuation |
| 2250 |
(forall2 (lambda (tv1 tv2) |
| 2251 |
(procedure (convert-tvars |
| 2252 |
(list (procedure |
| 2253 |
(convert-tvars |
| 2254 |
(list (procedure (convert-tvars |
| 2255 |
(list tv1)) tv2))) |
| 2256 |
tv2))) |
| 2257 |
tv2)))) |
| 2258 |
)) |
| 2259 |
|
| 2260 |
|
| 2261 |
; global top level environment |
| 2262 |
|
| 2263 |
(define (global-env) |
| 2264 |
(append misc-env |
| 2265 |
io-env |
| 2266 |
boolean-env |
| 2267 |
symbol-env |
| 2268 |
number-env |
| 2269 |
char-env |
| 2270 |
string-env |
| 2271 |
vector-env |
| 2272 |
procedure-env |
| 2273 |
list-env)) |
| 2274 |
|
| 2275 |
(define dynamic-top-level-env (global-env)) |
| 2276 |
|
| 2277 |
(define (init-dynamic-top-level-env!) |
| 2278 |
(set! dynamic-top-level-env (global-env)) |
| 2279 |
'()) |
| 2280 |
|
| 2281 |
(define (dynamic-top-level-env-show) |
| 2282 |
; displays the top level environment |
| 2283 |
(map (lambda (binding) |
| 2284 |
(cons (key-show (binding-key binding)) |
| 2285 |
(cons ': (tvar-show (binding-value binding))))) |
| 2286 |
(env->list dynamic-top-level-env))) |
| 2287 |
; ---------------------------------------------------------------------------- |
| 2288 |
; Dynamic type inference for Scheme |
| 2289 |
; ---------------------------------------------------------------------------- |
| 2290 |
|
| 2291 |
; Needed packages: |
| 2292 |
|
| 2293 |
(define (ic!) (init-global-constraints!)) |
| 2294 |
(define (pc) (glob-constr-show)) |
| 2295 |
(define (lc) (length global-constraints)) |
| 2296 |
(define (n!) (normalize-global-constraints!)) |
| 2297 |
(define (pt) (dynamic-top-level-env-show)) |
| 2298 |
(define (it!) (init-dynamic-top-level-env!)) |
| 2299 |
(define (io!) (set! tag-ops 0) (set! no-ops 0)) |
| 2300 |
(define (i!) (ic!) (it!) (io!) '()) |
| 2301 |
|
| 2302 |
(define tag-ops 0) |
| 2303 |
(define no-ops 0) |
| 2304 |
|
| 2305 |
|
| 2306 |
(define doit |
| 2307 |
(lambda () |
| 2308 |
(i!) |
| 2309 |
(let ((foo (dynamic-parse-file "rnrs-benchmarks/dynamic.src.ss"))) |
| 2310 |
(normalize-global-constraints!) |
| 2311 |
(reset-counters!) |
| 2312 |
(tag-ast*-show foo) |
| 2313 |
(counters-show)))) |
| 2314 |
|
| 2315 |
(define (main . args) |
| 2316 |
(run-benchmark |
| 2317 |
"dynamic" |
| 2318 |
dynamic-iters |
| 2319 |
(lambda (result) (equal? result '((218 . 455) (6 . 1892) (2204 . 446)))) |
| 2320 |
(lambda () (lambda () (doit))))) |