forked from HigherOrderCO/HVM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
486 lines (387 loc) · 10.7 KB
/
lib.rs
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#![feature(atomic_from_mut)]
#![feature(atomic_mut_ptr)]
#![allow(unused_variables)]
#![allow(dead_code)]
#![allow(non_snake_case)]
#![allow(unused_macros)]
#![allow(unused_parens)]
#![allow(unused_labels)]
#![allow(non_upper_case_globals)]
pub mod language;
pub mod runtime;
pub use language::{*};
pub use runtime::{*};
pub use runtime::{Ptr,
DP0, DP1, VAR, ARG,
ERA, LAM, APP, SUP,
CTR, FUN, OP2, U60, F60,
ADD, SUB, MUL, DIV,
MOD, AND, OR , XOR,
SHL, SHR, LTN, LTE,
EQL, GTE, GTN, NEQ,
get_tag,
get_ext,
get_val,
get_num,
get_loc,
CELLS_PER_KB,
CELLS_PER_MB,
CELLS_PER_GB,
};
pub use language::syntax::{
Term,
Term::Var, // TODO: add `global: bool`
Term::Dup,
Term::Let,
Term::Lam,
Term::App,
Term::Ctr,
Term::U6O,
Term::F6O,
Term::Op2,
};
//// Helps with WASM debugging
//macro_rules! log {
//( $( $t:tt )* ) => {
//web_sys::console::log_1(&format!( $( $t )* ).into());
//}
//}
//#[wasm_bindgen]
//pub fn make_call(func: &str, args: &[&str]) -> Result<language::syntax::Term, String> {
//// TODO: redundant with `make_main_call`
//let args = args.iter().map(|par| language::syntax::read_term(par).unwrap()).collect();
//let name = func.to_string();
//Ok(language::syntax::Term::Ctr { name, args })
//}
////#[cfg(test)]
////mod tests {
////use crate::eval_code;
////use crate::make_call;
////#[test]
////fn test() {
////let code = "
////(Fn 0) = 0
////(Fn 1) = 1
////(Fn n) = (+ (Fn (- n 1)) (Fn (- n 2)))
////(Main) = (Fn 20)
////";
////let (norm, _cost, _size, _time) = eval_code(&make_call("Main", &[]).unwrap(), code, false, 4 << 30).unwrap();
////assert_eq!(norm, "6765");
////}
////}
//#[wasm_bindgen]
//#[derive(Clone, Debug)]
//pub struct Reduced {
//norm: String,
//cost: u64,
//size: u64,
//time: u64,
//}
//#[wasm_bindgen]
//impl Reduced {
//pub fn get_norm(&self) -> String {
//return self.norm.clone();
//}
//pub fn get_cost(&self) -> u64 {
//return self.cost;
//}
//pub fn get_size(&self) -> u64 {
//return self.size;
//}
//pub fn get_time(&self) -> u64 {
//return self.time;
//}
//}
//#[wasm_bindgen]
//impl Runtime {
///// Creates a new, empty runtime
//pub fn new(size: usize) -> Runtime {
//Runtime {
//heap: runtime::new_heap(size, runtime::available_parallelism()),
//prog: runtime::new_program(),
//book: language::rulebook::new_rulebook(),
//}
//}
///// Creates a runtime from source code, given a max number of nodes
//pub fn from_code_with_size(code: &str, size: usize) -> Result<Runtime, String> {
//let file = language::syntax::read_file(code)?;
//let heap = runtime::new_heap(size, runtime::available_parallelism());
//let prog = runtime::new_program();
//let book = language::rulebook::gen_rulebook(&file);
//return Ok(Runtime { heap, prog, book });
//}
////fn get_area(&mut self) -> runtime::Area {
////return runtime::get_area(&mut self.heap, 0)
////}
///// Creates a runtime from a source code
//#[cfg(not(target_arch = "wasm32"))]
//pub fn from_code(code: &str) -> Result<Runtime, String> {
//Runtime::from_code_with_size(code, 4 * CELLS_PER_GB)
//}
//#[cfg(target_arch = "wasm32")]
//pub fn from_code(code: &str) -> Result<Runtime, String> {
//Runtime::from_code_with_size(code, 256 * CELLS_PER_MB)
//}
///// Extends a runtime with new definitions
//pub fn define(&mut self, _code: &str) {
//todo!()
//}
///// Allocates a new term, returns its location
//pub fn alloc_code(&mut self, code: &str) -> Result<u64, String> {
//Ok(self.alloc_term(&*language::syntax::read_term(code)?))
//}
///// Given a location, returns the pointer stored on it
//pub fn load_ptr(&self, host: u64) -> Ptr {
//runtime::load_ptr(&self.heap, host)
//}
///// Given a location, evaluates a term to head normal form
//pub fn reduce(&mut self, host: u64) {
//runtime::reduce(&self.heap, &self.prog, &[0], host); // FIXME: add parallelism
//}
///// Given a location, evaluates a term to full normal form
//pub fn normalize(&mut self, host: u64) {
//runtime::normalize(&self.heap, &self.prog, &[0], host, false);
//}
///// Evaluates a code, allocs and evaluates to full normal form. Returns its location.
//pub fn normalize_code(&mut self, code: &str) -> u64 {
//let host = self.alloc_code(code).ok().unwrap();
//self.normalize(host);
//return host;
//}
///// Evaluates a code to normal form. Returns its location.
//pub fn eval_to_loc(&mut self, code: &str) -> u64 {
//return self.normalize_code(code);
//}
///// Evaluates a code to normal form.
//pub fn eval(&mut self, code: &str) -> String {
//let host = self.normalize_code(code);
//return self.show(host);
//}
//// /// Given a location, runs side-effective actions
////#[cfg(not(target_arch = "wasm32"))]
////pub fn run_io(&mut self, host: u64) {
////runtime::run_io(&mut self.heap, &self.prog, &[0], host)
////}
///// Given a location, recovers the lambda Term stored on it, as code
//pub fn show(&self, host: u64) -> String {
//language::readback::as_code(&self.heap, &self.prog, host)
//}
///// Given a location, recovers the linear Term stored on it, as code
//pub fn show_linear(&self, host: u64) -> String {
//language::readback::as_linear_code(&self.heap, &self.prog, host)
//}
///// Return the total number of graph rewrites computed
//pub fn get_rewrites(&self) -> u64 {
//runtime::get_cost(&self.heap)
//}
///// Returns the name of a given id
//pub fn get_name(&self, id: u64) -> String {
//self.book.id_to_name.get(&id).unwrap_or(&"?".to_string()).clone()
//}
///// Returns the arity of a given id
//pub fn get_arity(&self, id: u64) -> u64 {
//*self.book.id_to_arit.get(&id).unwrap_or(&u64::MAX)
//}
///// Returns the name of a given id
//pub fn get_id(&self, name: &str) -> u64 {
//*self.book.name_to_id.get(name).unwrap_or(&u64::MAX)
//}
//// WASM re-exports
//pub fn DP0() -> u64 {
//return DP0;
//}
//pub fn DP1() -> u64 {
//return DP1;
//}
//pub fn VAR() -> u64 {
//return VAR;
//}
//pub fn ARG() -> u64 {
//return ARG;
//}
//pub fn ERA() -> u64 {
//return ERA;
//}
//pub fn LAM() -> u64 {
//return LAM;
//}
//pub fn APP() -> u64 {
//return APP;
//}
//pub fn SUP() -> u64 {
//return SUP;
//}
//pub fn CTR() -> u64 {
//return CTR;
//}
//pub fn FUN() -> u64 {
//return FUN;
//}
//pub fn OP2() -> u64 {
//return OP2;
//}
//pub fn NUM() -> u64 {
//return NUM;
//}
//pub fn ADD() -> u64 {
//return ADD;
//}
//pub fn SUB() -> u64 {
//return SUB;
//}
//pub fn MUL() -> u64 {
//return MUL;
//}
//pub fn DIV() -> u64 {
//return DIV;
//}
//pub fn MOD() -> u64 {
//return MOD;
//}
//pub fn AND() -> u64 {
//return AND;
//}
//pub fn OR() -> u64 {
//return OR;
//}
//pub fn XOR() -> u64 {
//return XOR;
//}
//pub fn SHL() -> u64 {
//return SHL;
//}
//pub fn SHR() -> u64 {
//return SHR;
//}
//pub fn LTN() -> u64 {
//return LTN;
//}
//pub fn LTE() -> u64 {
//return LTE;
//}
//pub fn EQL() -> u64 {
//return EQL;
//}
//pub fn GTE() -> u64 {
//return GTE;
//}
//pub fn GTN() -> u64 {
//return GTN;
//}
//pub fn NEQ() -> u64 {
//return NEQ;
//}
//pub fn CELLS_PER_KB() -> usize {
//return CELLS_PER_KB;
//}
//pub fn CELLS_PER_MB() -> usize {
//return CELLS_PER_MB;
//}
//pub fn CELLS_PER_GB() -> usize {
//return CELLS_PER_GB;
//}
//pub fn get_tag(lnk: Ptr) -> u64 {
//return get_tag(lnk);
//}
//pub fn get_ext(lnk: Ptr) -> u64 {
//return get_ext(lnk);
//}
//pub fn get_val(lnk: Ptr) -> u64 {
//return get_val(lnk);
//}
//pub fn get_num(lnk: Ptr) -> u64 {
//return get_num(lnk);
//}
//pub fn get_loc(lnk: Ptr, arg: u64) -> u64 {
//return get_loc(lnk, arg);
//}
//pub fn Var(pos: u64) -> Ptr {
//return runtime::Var(pos);
//}
//pub fn Dp0(col: u64, pos: u64) -> Ptr {
//return runtime::Dp0(col, pos);
//}
//pub fn Dp1(col: u64, pos: u64) -> Ptr {
//return runtime::Dp1(col, pos);
//}
//pub fn Arg(pos: u64) -> Ptr {
//return runtime::Arg(pos);
//}
//pub fn Era() -> Ptr {
//return runtime::Era();
//}
//pub fn Lam(pos: u64) -> Ptr {
//return runtime::Lam(pos);
//}
//pub fn App(pos: u64) -> Ptr {
//return runtime::App(pos);
//}
//pub fn Sup(col: u64, pos: u64) -> Ptr {
//return runtime::Sup(col, pos);
//}
//pub fn Op2(ope: u64, pos: u64) -> Ptr {
//return runtime::Op2(ope, pos);
//}
//pub fn Num(val: u64) -> Ptr {
//return runtime::Num(val);
//}
//pub fn Ctr(fun: u64, pos: u64) -> Ptr {
//return runtime::Ctr(fun, pos);
//}
//pub fn Fun(fun: u64, pos: u64) -> Ptr {
//return runtime::Fun(fun, pos);
//}
//pub fn link(&mut self, loc: u64, lnk: Ptr) -> Ptr {
//return runtime::link(&self.heap, loc, lnk);
//}
//pub fn alloc(&mut self, size: u64) -> u64 {
//return runtime::alloc(&self.heap, 0, size); // FIXME tid?
//}
//pub fn free(&mut self, loc: u64, size: u64) {
//return runtime::free(&self.heap, 0, loc, size); // FIXME tid?
//}
//pub fn collect(&mut self, term: Ptr) {
//return runtime::collect(&self.heap, &self.prog.arit, 0, term); // FIXME tid?
//}
//}
// Methods that aren't compiled to JS
//impl Runtime {
///// Allocates a new term, returns its location
//pub fn alloc_term(&mut self, term: &language::syntax::Term) -> u64 {
//runtime::alloc_term(&self.heap, 0, &self.book, term) // FIXME tid?
//}
///// Given a location, recovers the Term stored on it
//pub fn readback(&self, host: u64) -> Box<Term> {
//language::readback::as_term(&self.heap, &self.prog, host)
//}
///// Given a location, recovers the Term stored on it
//pub fn linear_readback(&self, host: u64) -> Box<Term> {
//language::readback::as_linear_term(&self.heap, &self.prog, host)
//}
//}
//pub fn example() -> Result<(), String> {
//let mut rt = Runtime::from_code_with_size("
//(Double Zero) = Zero
//(Double (Succ x)) = (Succ (Succ (Double x)))
//", 10000).unwrap();
//let loc = rt.normalize_code("(Double (Succ (Succ Zero)))");
//println!("{}", rt.show(loc));
//return Ok(());
//}
////#[cfg(test)]
////mod tests {
////use crate::eval_code;
////use crate::make_call;
////#[test]
////fn test() {
////let code = "
////(Fn 0) = 0
////(Fn 1) = 1
////(Fn n) = (+ (Fn (- n 1)) (Fn (- n 2)))
////(Main) = (Fn 20)
////";
////let (norm, _cost, _size, _time) = language::rulebook::eval_code(&make_call("Main", &[]).unwrap(), code, false, 32 << 20).unwrap();
////assert_eq!(norm, "6765");
////}
////}
////