This repository has been archived by the owner on Oct 26, 2023. It is now read-only.
forked from iambrj/imin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreveal-functions.rkt
49 lines (45 loc) · 2.01 KB
/
reveal-functions.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#lang racket
(require "utilities.rkt")
(provide reveal-functions)
; Transform function references from just f to (FunRefArity f arity)
(define (reveal-functions p)
(match p
[(ProgramDefs info def*)
(let ([fun->arity (for/list ([def def*])
(cons (Def-name def) (length (Def-param* def))))])
(ProgramDefs info (map (lambda (d)
(struct-copy Def d
[body ((rf-exp fun->arity) (Def-body d))]))
def*)))]))
(define ((rf-exp fun->arity) e)
(match e
[(Void) (Void)]
[(Int i) (Int i)]
[(Bool b) (Bool b)]
[(Var v) (if (member v (dict-keys fun->arity))
(FunRefArity v (dict-ref fun->arity v))
(Var v))]
[(HasType e t) (HasType ((rf-exp fun->arity) e) t)]
[(Let v e b) (Let v ((rf-exp fun->arity) e)
((rf-exp (dict-remove fun->arity v)) b))]
[(If c t e) (If ((rf-exp fun->arity) c)
((rf-exp fun->arity) t)
((rf-exp fun->arity) e))]
[(Prim op es) #:when (member op '(read - + not < vector vector-length
procedure-arity))
(Prim op (map (rf-exp fun->arity) es))]
[(Prim 'vector-ref `(,e ,(Int i)))
(Prim 'vector-ref `(,((rf-exp fun->arity) e) ,(Int i)))]
[(Prim 'vector-set! `(,e1 ,(Int i) ,e2))
(Prim 'vector-set! `(,((rf-exp fun->arity) e1)
,(Int i) ,((rf-exp fun->arity) e2)))]
; Invariant : only top level function definitions are allowed, no clause
; needed for Def
[(Apply fun arg*)
(Apply ((rf-exp fun->arity) fun) (map (rf-exp fun->arity) arg*))]
[(Lambda arg* rtype body)
(let ([fun->arity (foldr (lambda (arg a)
(dict-remove a arg))
fun->arity
arg*)])
(Lambda arg* rtype ((rf-exp fun->arity) body)))]))