FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
lambda/src/lambda.ts at main · clikuki/lambda · GitHub
clikuki
/
lambda
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
lambda
/
src
/
lambda.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
321 lines (285 loc) · 7.59 KB
Breadcrumbs
lambda
/
src
/
lambda.ts
Copy path
File metadata and controls
321 lines (285 loc) · 7.59 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import
{
getID
,
ID
}
from
"./utils.js"
;
const
id
=
getID
(
)
;
export
interface
Abstraction
{
type
:
"ABSTRACTION"
;
param
:
symbol
;
body
:
Term
;
id
:
ID
;
}
export
interface
Application
{
type
:
"APPLICATION"
;
left
:
Term
;
right
:
Term
;
id
:
ID
;
}
export
interface
Variable
{
type
:
"VARIABLE"
;
symbol
:
symbol
;
id
:
ID
;
}
export
type
Term
=
Application
|
Abstraction
|
Variable
;
export
interface
Replacer
{
by
?:
Term
;
at
:
Term
[
]
;
}
const
REDUCTION_STEP_LIMIT
=
10000
;
export
class
Lambda
{
tree
:
Term
;
constructor
(
code
:
string
)
{
this
.
tree
=
parseString
(
code
)
;
}
betaReduce
(
attemptNominal
=
false
)
{
let
reduced
:
Term
|
null
;
let
steps
=
REDUCTION_STEP_LIMIT
;
const
replaced
:
Replacer
=
{
at
:
[
]
}
;
do
{
reduced
=
attemptNominal
?
this
.
_greedyReductionStep
(
this
.
tree
)
:
this
.
_shallowReductionStep
(
this
.
tree
,
replaced
)
;
if
(
reduced
)
this
.
tree
=
reduced
;
}
while
(
attemptNominal
&&
reduced
&&
--
steps
>
0
)
;
return
replaced
;
}
/** Performs as much reduction as possible in a single step */
_greedyReductionStep
(
tree
:
Term
)
:
Term
|
null
{
switch
(
tree
.
type
)
{
case
"APPLICATION"
:
// Reduce application
const
{
left
,
right
}
=
tree
;
const
leftReduct
=
this
.
_greedyReductionStep
(
left
)
??
left
;
const
rightReduct
=
this
.
_greedyReductionStep
(
right
)
??
right
;
if
(
left
.
type
===
"ABSTRACTION"
)
{
return
this
.
_substitute
(
left
.
body
,
left
.
param
,
right
)
;
}
else
{
return
{
id
:
tree
.
id
,
type
:
"APPLICATION"
,
left
:
leftReduct
,
right
:
rightReduct
,
}
;
}
case
"ABSTRACTION"
:
// Reduce abstraction body
const
bodyReduct
=
this
.
_greedyReductionStep
(
tree
.
body
)
;
if
(
bodyReduct
)
{
return
{
id
:
tree
.
id
,
type
:
"ABSTRACTION"
,
param
:
tree
.
param
,
body
:
bodyReduct
,
}
;
}
}
return
null
;
}
/** Perform one step of beta reduction */
_shallowReductionStep
(
tree
:
Term
,
replaced
:
Replacer
)
:
Term
|
null
{
switch
(
tree
.
type
)
{
case
"APPLICATION"
:
// Reduce application
const
{
left
,
right
}
=
tree
;
if
(
left
.
type
===
"ABSTRACTION"
)
{
replaced
.
by
=
right
;
return
this
.
_substitute
(
left
.
body
,
left
.
param
,
right
,
replaced
)
;
}
const
leftReduct
=
this
.
_shallowReductionStep
(
left
,
replaced
)
;
if
(
leftReduct
)
{
return
{
id
:
tree
.
id
,
type
:
"APPLICATION"
,
left
:
leftReduct
,
right
,
}
;
}
const
rightReduct
=
this
.
_shallowReductionStep
(
right
,
replaced
)
;
if
(
rightReduct
)
{
return
{
id
:
tree
.
id
,
type
:
"APPLICATION"
,
left
,
right
:
rightReduct
,
}
;
}
break
;
case
"ABSTRACTION"
:
// Reduce abstraction body
const
bodyReduct
=
this
.
_shallowReductionStep
(
tree
.
body
,
replaced
)
;
if
(
bodyReduct
)
{
return
{
id
:
tree
.
id
,
type
:
"ABSTRACTION"
,
param
:
tree
.
param
,
body
:
bodyReduct
,
}
;
}
}
return
null
;
}
_substitute
(
tree
:
Term
,
from
:
symbol
,
to
:
Term
,
replaced
?:
Replacer
)
:
Term
{
// Quick escape for strings
if
(
tree
.
type
===
"VARIABLE"
)
{
if
(
tree
.
symbol
===
from
)
{
const
copy
=
this
.
copy
(
to
)
;
replaced
?.
at
!
.
push
(
copy
)
;
return
copy
;
}
return
tree
;
}
let
sub
:
Term
;
if
(
tree
.
type
===
"APPLICATION"
)
{
// Dealing with application
const
{
left
,
right
}
=
tree
;
sub
=
{
id
:
tree
.
id
,
type
:
"APPLICATION"
,
left
:
this
.
_substitute
(
left
,
from
,
to
,
replaced
)
,
right
:
this
.
_substitute
(
right
,
from
,
to
,
replaced
)
,
}
;
}
else
if
(
tree
.
param
!==
from
)
{
// Dealing with abstraction
sub
=
{
id
:
tree
.
id
,
type
:
"ABSTRACTION"
,
param
:
tree
.
param
,
body
:
this
.
_substitute
(
tree
.
body
,
from
,
to
,
replaced
)
,
}
;
}
else
{
// Is abstraction, but shadows the term that we are trying to substitute
sub
=
tree
;
}
return
sub
;
}
copy
(
tree
:
Term
,
changeID
=
true
)
:
Term
{
switch
(
tree
.
type
)
{
case
"VARIABLE"
:
return
{
id
:
changeID
?
id
.
next
(
)
.
value
:
tree
.
id
,
type
:
"VARIABLE"
,
symbol
:
tree
.
symbol
,
}
;
case
"APPLICATION"
:
return
{
id
:
changeID
?
id
.
next
(
)
.
value
:
tree
.
id
,
type
:
"APPLICATION"
,
left
:
this
.
copy
(
tree
.
left
,
changeID
)
,
right
:
this
.
copy
(
tree
.
right
,
changeID
)
,
}
;
case
"ABSTRACTION"
:
return
{
id
:
changeID
?
id
.
next
(
)
.
value
:
tree
.
id
,
type
:
"ABSTRACTION"
,
param
:
tree
.
param
,
body
:
this
.
copy
(
tree
.
body
,
changeID
)
,
}
;
}
}
toString
(
collectParameters
=
false
)
{
return
stringifyTree
(
this
.
tree
,
collectParameters
)
;
}
}
export
const
func_char
=
"@"
;
export
function
parseString
(
code
:
string
,
mapping
=
new
Map
<
string
,
symbol
>
(
)
)
:
Term
{
let
left
:
Term
|
null
=
null
;
let
right
:
Term
|
null
=
null
;
for
(
let
i
=
0
;
i
<
code
.
length
;
i
++
)
{
const
char
=
code
[
i
]
;
if
(
char
===
" "
)
throw
SyntaxError
(
"No spaces allowed in code string"
)
;
if
(
char
===
func_char
)
{
// abstraction declaration
const
start
=
i
+
3
;
const
end
=
code
.
length
;
const
paramChar
=
code
[
i
+
1
]
;
const
param
=
Symbol
(
paramChar
)
;
const
localMapping
=
new
Map
(
mapping
)
;
localMapping
.
set
(
paramChar
,
param
)
;
// All characters at this point must be consumed
const
body
=
parseString
(
code
.
slice
(
start
,
end
)
,
localMapping
)
;
const
abstraction
:
Abstraction
=
{
type
:
"ABSTRACTION"
,
param
,
body
,
id
:
id
.
next
(
)
.
value
,
}
;
if
(
!
left
)
left
=
abstraction
;
else
right
=
abstraction
;
i
=
end
;
}
else
if
(
char
===
"("
)
{
// Perform parse within bracket group, this usually occurs before abstraction declarations
const
start
=
i
+
1
;
const
end
=
findBracketPair
(
code
,
i
)
;
let
term
=
parseString
(
code
.
slice
(
start
,
end
)
,
mapping
)
;
if
(
!
left
)
left
=
term
;
else
right
=
term
;
i
=
end
;
}
else
{
// Get correspnding symbol of variable
const
sym
=
mapping
.
get
(
char
)
??
Symbol
(
char
)
;
if
(
!
mapping
.
has
(
char
)
)
mapping
.
set
(
char
,
sym
)
;
// Add single character as variable
const
variable
:
Term
=
{
type
:
"VARIABLE"
,
symbol
:
sym
,
id
:
id
.
next
(
)
.
value
,
}
;
if
(
!
left
)
left
=
variable
;
else
right
=
variable
;
}
if
(
right
)
{
// Group a and b into an application
left
=
{
type
:
"APPLICATION"
,
left
,
right
,
id
:
id
.
next
(
)
.
value
}
;
right
=
null
;
}
}
if
(
!
left
)
throw
"Cannot parse empty string"
;
return
left
;
}
export
function
stringifyTree
(
tree
:
Term
,
combineParameters
=
false
)
:
string
{
let
str
=
""
;
if
(
tree
.
type
===
"VARIABLE"
)
{
// Is this a dangerous assumption?
str
=
tree
.
symbol
.
description
!
;
}
else
if
(
tree
.
type
===
"APPLICATION"
)
{
// Dealing with application
const
{
left
,
right
}
=
tree
;
str
+=
stringifyTree
(
left
,
combineParameters
)
;
// If the second term is an application itself, then explicitly parenthesize
if
(
right
.
type
===
"APPLICATION"
)
str
+=
`(
${
stringifyTree
(
right
,
combineParameters
)
}
)`
;
else
str
+=
`
${
stringifyTree
(
right
,
combineParameters
)
}
`
;
}
else
{
// Dealing with abstraction
// Shorthand: Collect parameters of consecutively nested abstractions
let
node
:
Term
=
tree
.
body
;
let
parameters
=
tree
.
param
.
description
!
;
while
(
combineParameters
&&
node
.
type
===
"ABSTRACTION"
)
{
parameters
+=
node
.
param
.
description
;
node
=
node
.
body
;
}
str
=
`(
${
func_char
}
${
parameters
}
.
${
stringifyTree
(
node
,
combineParameters
)
}
)`
;
}
return
str
;
}
function
findBracketPair
(
str
:
string
,
at
:
number
)
:
number
{
let
count
=
1
;
for
(
let
i
=
at
+
1
;
i
<
str
.
length
;
i
++
)
{
const
char
=
str
[
i
]
;
if
(
char
===
"("
)
count
++
;
else
if
(
char
===
")"
&&
!
--
count
)
return
i
;
}
return
-
1
;
}
export
function
code
(
strings
:
TemplateStringsArray
,
...
values
:
string
[
]
)
:
string
{
let
str
=
""
;
for
(
let
i
=
0
;
i
<
values
.
length
;
i
++
)
{
str
+=
strings
[
i
]
;
str
+=
`(
${
values
[
i
]
}
)`
;
}
str
+=
strings
.
at
(
-
1
)
;
return
str
.
replaceAll
(
" "
,
""
)
;
}
Back
|
FazBrowse Home
|
New Git URL