L1.close

  1from functools import partial
  2from typing import Callable
  3
  4from L0 import syntax as L0
  5
  6from . import syntax as L1
  7
  8
  9def free_variables(statement: L1.Statement) -> set[L1.Identifier]:
 10    match statement:
 11        case L1.Copy(destination=destination, source=source, then=then):
 12            return ({source} | free_variables(then)) - {destination}
 13        case L1.Abstract(destination=destination, parameters=parameters, body=body, then=then):
 14            return (free_variables(body) - set(parameters) | free_variables(then)) - {destination}
 15        case L1.Apply(target=target, arguments=arguments):
 16            return {target} | set(arguments)
 17        case L1.Immediate(destination=destination, then=then):
 18            return free_variables(then) - {destination}
 19        case L1.Primitive(destination=destination, left=left, right=right, then=then):
 20            return ({left, right} | free_variables(then)) - {destination}
 21        case L1.Branch(left=left, right=right, then=then, otherwise=otherwise):
 22            return {left, right} | free_variables(then) | free_variables(otherwise)
 23        case L1.Allocate(destination=destination, then=then):
 24            return free_variables(then) - {destination}
 25        case L1.Load(destination=destination, base=base, then=then):
 26            return {base} | free_variables(then) - {destination}
 27        case L1.Store(base=base, value=value, then=then):
 28            return {base, value} | free_variables(then)
 29        case L1.Halt(value=value):
 30            return {value}
 31        case _:  # pragma: no cover
 32            return set()
 33
 34
 35def close_term(
 36    statement: L1.Statement,
 37    lift: Callable[[L0.Procedure], None],
 38    fresh: Callable[[str], str],
 39) -> L0.Statement:
 40    recur = partial(close_term, lift=lift, fresh=fresh)  # noqa: F841
 41
 42    match statement:
 43        case L1.Copy(destination=destination, source=source, then=then):
 44            return L0.Copy(
 45                destination=destination,
 46                source=source,
 47                then=recur(then),
 48            )
 49
 50        case L1.Abstract(destination=destination, parameters=parameters, body=body, then=then):
 51            # 1. Close the abstract / lift to top level
 52            name = fresh("proc")
 53            env_p = fresh("env")
 54
 55            fvs = list(free_variables(body) - set(parameters))
 56
 57            result = recur(body)
 58            for i, fv in enumerate(fvs):
 59                result = L0.Load(
 60                    destination=fv,
 61                    base=env_p,
 62                    index=i,
 63                    then=result,
 64                )
 65
 66            lift(
 67                L0.Procedure(
 68                    name=name,
 69                    parameters=[*parameters, env_p],
 70                    body=result,
 71                )
 72            )
 73
 74            # 2. Create the closure (tuple of code and environment)
 75            env = fresh("env")
 76            code = fresh("code")
 77            result = L0.Address(
 78                destination=code,
 79                name=name,
 80                then=L0.Allocate(
 81                    destination=destination,
 82                    count=2,
 83                    then=L0.Store(
 84                        base=destination,
 85                        index=0,
 86                        value=code,
 87                        then=L0.Store(
 88                            base=destination,
 89                            index=1,
 90                            value=env,
 91                            then=recur(then),
 92                        ),
 93                    ),
 94                ),
 95            )
 96
 97            for i, fv in enumerate(fvs):
 98                result = L0.Store(
 99                    base=env,
100                    index=i,
101                    value=fv,
102                    then=result,
103                )
104
105            return L0.Allocate(
106                destination=env,
107                count=len(fvs),
108                then=result,
109            )
110
111        case L1.Apply(target=target, arguments=arguments):
112            # 1. Seperate code and environment from the closure
113            # 2. Call the code with the argument and environment
114            code = fresh("code")
115            env = fresh("env")
116            return L0.Load(
117                destination=code,
118                base=target,
119                index=0,
120                then=L0.Load(
121                    destination=env,
122                    base=target,
123                    index=1,
124                    then=L0.Call(
125                        target=code,
126                        arguments=[*arguments, env],
127                    ),
128                ),
129            )
130
131        case L1.Immediate(destination=destination, value=value, then=then):
132            return L0.Immediate(
133                destination=destination,
134                value=value,
135                then=recur(then),
136            )
137
138        case L1.Primitive(destination=destination, left=left, right=right, operator=operator, then=then):
139            return L0.Primitive(
140                destination=destination,
141                left=left,
142                right=right,
143                operator=operator,
144                then=recur(then),
145            )
146
147        case L1.Branch(left=left, right=right, operator=operator, then=then, otherwise=otherwise):
148            return L0.Branch(
149                left=left,
150                right=right,
151                operator=operator,
152                then=recur(then),
153                otherwise=recur(otherwise),
154            )
155
156        case L1.Allocate(destination=destination, count=count, then=then):
157            return L0.Allocate(
158                destination=destination,
159                count=count,
160                then=recur(then),
161            )
162
163        case L1.Load(destination=destination, base=base, index=index, then=then):
164            return L0.Load(
165                destination=destination,
166                base=base,
167                index=index,
168                then=recur(then),
169            )
170
171        case L1.Store(base=base, index=index, value=value, then=then):
172            return L0.Store(
173                base=base,
174                index=index,
175                value=value,
176                then=recur(then),
177            )
178
179        case L1.Halt(value=value):  # pragma: no branch
180            return L0.Halt(value=value)
181
182
183def close_program(program: L1.Program, fresh: Callable[[str], str]) -> L0.Program:
184    match program:
185        case L1.Program(parameters=parameters, body=body):  # pragma: no branch
186            procedures = list[L0.Procedure]()
187
188            body = close_term(
189                body,
190                procedures.append,
191                fresh,
192            )
193            return L0.Program(
194                procedures=[
195                    *procedures,
196                    L0.Procedure(
197                        name="l0",
198                        parameters=parameters,
199                        body=body,
200                    ),
201                ],
202            )
def free_variables(statement: Statement) -> set[Identifier]:
10def free_variables(statement: L1.Statement) -> set[L1.Identifier]:
11    match statement:
12        case L1.Copy(destination=destination, source=source, then=then):
13            return ({source} | free_variables(then)) - {destination}
14        case L1.Abstract(destination=destination, parameters=parameters, body=body, then=then):
15            return (free_variables(body) - set(parameters) | free_variables(then)) - {destination}
16        case L1.Apply(target=target, arguments=arguments):
17            return {target} | set(arguments)
18        case L1.Immediate(destination=destination, then=then):
19            return free_variables(then) - {destination}
20        case L1.Primitive(destination=destination, left=left, right=right, then=then):
21            return ({left, right} | free_variables(then)) - {destination}
22        case L1.Branch(left=left, right=right, then=then, otherwise=otherwise):
23            return {left, right} | free_variables(then) | free_variables(otherwise)
24        case L1.Allocate(destination=destination, then=then):
25            return free_variables(then) - {destination}
26        case L1.Load(destination=destination, base=base, then=then):
27            return {base} | free_variables(then) - {destination}
28        case L1.Store(base=base, value=value, then=then):
29            return {base, value} | free_variables(then)
30        case L1.Halt(value=value):
31            return {value}
32        case _:  # pragma: no cover
33            return set()
def close_term( statement: Statement, lift: Callable[[L0.syntax.Procedure], NoneType], fresh: Callable[[str], str]) -> Statement:
 36def close_term(
 37    statement: L1.Statement,
 38    lift: Callable[[L0.Procedure], None],
 39    fresh: Callable[[str], str],
 40) -> L0.Statement:
 41    recur = partial(close_term, lift=lift, fresh=fresh)  # noqa: F841
 42
 43    match statement:
 44        case L1.Copy(destination=destination, source=source, then=then):
 45            return L0.Copy(
 46                destination=destination,
 47                source=source,
 48                then=recur(then),
 49            )
 50
 51        case L1.Abstract(destination=destination, parameters=parameters, body=body, then=then):
 52            # 1. Close the abstract / lift to top level
 53            name = fresh("proc")
 54            env_p = fresh("env")
 55
 56            fvs = list(free_variables(body) - set(parameters))
 57
 58            result = recur(body)
 59            for i, fv in enumerate(fvs):
 60                result = L0.Load(
 61                    destination=fv,
 62                    base=env_p,
 63                    index=i,
 64                    then=result,
 65                )
 66
 67            lift(
 68                L0.Procedure(
 69                    name=name,
 70                    parameters=[*parameters, env_p],
 71                    body=result,
 72                )
 73            )
 74
 75            # 2. Create the closure (tuple of code and environment)
 76            env = fresh("env")
 77            code = fresh("code")
 78            result = L0.Address(
 79                destination=code,
 80                name=name,
 81                then=L0.Allocate(
 82                    destination=destination,
 83                    count=2,
 84                    then=L0.Store(
 85                        base=destination,
 86                        index=0,
 87                        value=code,
 88                        then=L0.Store(
 89                            base=destination,
 90                            index=1,
 91                            value=env,
 92                            then=recur(then),
 93                        ),
 94                    ),
 95                ),
 96            )
 97
 98            for i, fv in enumerate(fvs):
 99                result = L0.Store(
100                    base=env,
101                    index=i,
102                    value=fv,
103                    then=result,
104                )
105
106            return L0.Allocate(
107                destination=env,
108                count=len(fvs),
109                then=result,
110            )
111
112        case L1.Apply(target=target, arguments=arguments):
113            # 1. Seperate code and environment from the closure
114            # 2. Call the code with the argument and environment
115            code = fresh("code")
116            env = fresh("env")
117            return L0.Load(
118                destination=code,
119                base=target,
120                index=0,
121                then=L0.Load(
122                    destination=env,
123                    base=target,
124                    index=1,
125                    then=L0.Call(
126                        target=code,
127                        arguments=[*arguments, env],
128                    ),
129                ),
130            )
131
132        case L1.Immediate(destination=destination, value=value, then=then):
133            return L0.Immediate(
134                destination=destination,
135                value=value,
136                then=recur(then),
137            )
138
139        case L1.Primitive(destination=destination, left=left, right=right, operator=operator, then=then):
140            return L0.Primitive(
141                destination=destination,
142                left=left,
143                right=right,
144                operator=operator,
145                then=recur(then),
146            )
147
148        case L1.Branch(left=left, right=right, operator=operator, then=then, otherwise=otherwise):
149            return L0.Branch(
150                left=left,
151                right=right,
152                operator=operator,
153                then=recur(then),
154                otherwise=recur(otherwise),
155            )
156
157        case L1.Allocate(destination=destination, count=count, then=then):
158            return L0.Allocate(
159                destination=destination,
160                count=count,
161                then=recur(then),
162            )
163
164        case L1.Load(destination=destination, base=base, index=index, then=then):
165            return L0.Load(
166                destination=destination,
167                base=base,
168                index=index,
169                then=recur(then),
170            )
171
172        case L1.Store(base=base, index=index, value=value, then=then):
173            return L0.Store(
174                base=base,
175                index=index,
176                value=value,
177                then=recur(then),
178            )
179
180        case L1.Halt(value=value):  # pragma: no branch
181            return L0.Halt(value=value)
def close_program( program: L1.syntax.Program, fresh: Callable[[str], str]) -> L0.syntax.Program:
184def close_program(program: L1.Program, fresh: Callable[[str], str]) -> L0.Program:
185    match program:
186        case L1.Program(parameters=parameters, body=body):  # pragma: no branch
187            procedures = list[L0.Procedure]()
188
189            body = close_term(
190                body,
191                procedures.append,
192                fresh,
193            )
194            return L0.Program(
195                procedures=[
196                    *procedures,
197                    L0.Procedure(
198                        name="l0",
199                        parameters=parameters,
200                        body=body,
201                    ),
202                ],
203            )