hvm1/lib.rs
1#![feature(atomic_from_mut)]
2
3#![allow(unused_variables)]
4#![allow(dead_code)]
5#![allow(non_snake_case)]
6#![allow(unused_macros)]
7#![allow(unused_parens)]
8#![allow(unused_labels)]
9#![allow(non_upper_case_globals)]
10
11pub mod language;
12pub mod runtime;
13
14pub use language::{*};
15pub use runtime::{*};
16
17pub use runtime::{Ptr,
18 DP0, DP1, VAR, ARG,
19 ERA, LAM, APP, SUP,
20 CTR, FUN, OP2, U60, F60,
21 ADD, SUB, MUL, DIV,
22 MOD, AND, OR , XOR,
23 SHL, SHR, LTN, LTE,
24 EQL, GTE, GTN, NEQ,
25 get_tag,
26 get_ext,
27 get_val,
28 get_num,
29 get_loc,
30 CELLS_PER_KB,
31 CELLS_PER_MB,
32 CELLS_PER_GB,
33};
34
35pub use language::syntax::{
36 Term,
37 Term::Var, // TODO: add `global: bool`
38 Term::Dup,
39 Term::Let,
40 Term::Lam,
41 Term::App,
42 Term::Ctr,
43 Term::U6O,
44 Term::F6O,
45 Term::Op2,
46};
47
48//// Helps with WASM debugging
49//macro_rules! log {
50 //( $( $t:tt )* ) => {
51 //web_sys::console::log_1(&format!( $( $t )* ).into());
52 //}
53//}
54
55//#[wasm_bindgen]
56//pub fn make_call(func: &str, args: &[&str]) -> Result<language::syntax::Term, String> {
57 //// TODO: redundant with `make_main_call`
58 //let args = args.iter().map(|par| language::syntax::read_term(par).unwrap()).collect();
59 //let name = func.to_string();
60 //Ok(language::syntax::Term::Ctr { name, args })
61//}
62
63////#[cfg(test)]
64////mod tests {
65 ////use crate::eval_code;
66 ////use crate::make_call;
67
68 ////#[test]
69 ////fn test() {
70 ////let code = "
71 ////(Fn 0) = 0
72 ////(Fn 1) = 1
73 ////(Fn n) = (+ (Fn (- n 1)) (Fn (- n 2)))
74 ////(Main) = (Fn 20)
75 ////";
76
77 ////let (norm, _cost, _size, _time) = eval_code(&make_call("Main", &[]).unwrap(), code, false, 4 << 30).unwrap();
78 ////assert_eq!(norm, "6765");
79 ////}
80////}
81
82//#[wasm_bindgen]
83//#[derive(Clone, Debug)]
84//pub struct Reduced {
85 //norm: String,
86 //cost: u64,
87 //size: u64,
88 //time: u64,
89//}
90
91//#[wasm_bindgen]
92//impl Reduced {
93 //pub fn get_norm(&self) -> String {
94 //return self.norm.clone();
95 //}
96
97 //pub fn get_cost(&self) -> u64 {
98 //return self.cost;
99 //}
100
101 //pub fn get_size(&self) -> u64 {
102 //return self.size;
103 //}
104
105 //pub fn get_time(&self) -> u64 {
106 //return self.time;
107 //}
108//}
109
110//#[wasm_bindgen]
111//impl Runtime {
112
113 ///// Creates a new, empty runtime
114 //pub fn new(size: usize) -> Runtime {
115 //Runtime {
116 //heap: runtime::new_heap(size, runtime::available_parallelism()),
117 //prog: runtime::new_program(),
118 //book: language::rulebook::new_rulebook(),
119 //}
120 //}
121
122 ///// Creates a runtime from source code, given a max number of nodes
123 //pub fn from_code_with_size(code: &str, size: usize) -> Result<Runtime, String> {
124 //let file = language::syntax::read_file(code)?;
125 //let heap = runtime::new_heap(size, runtime::available_parallelism());
126 //let prog = runtime::new_program();
127 //let book = language::rulebook::gen_rulebook(&file);
128 //return Ok(Runtime { heap, prog, book });
129 //}
130
131 ////fn get_area(&mut self) -> runtime::Area {
132 ////return runtime::get_area(&mut self.heap, 0)
133 ////}
134
135 ///// Creates a runtime from a source code
136 //#[cfg(not(target_arch = "wasm32"))]
137 //pub fn from_code(code: &str) -> Result<Runtime, String> {
138 //Runtime::from_code_with_size(code, 4 * CELLS_PER_GB)
139 //}
140
141 //#[cfg(target_arch = "wasm32")]
142 //pub fn from_code(code: &str) -> Result<Runtime, String> {
143 //Runtime::from_code_with_size(code, 256 * CELLS_PER_MB)
144 //}
145
146 ///// Extends a runtime with new definitions
147 //pub fn define(&mut self, _code: &str) {
148 //todo!()
149 //}
150
151 ///// Allocates a new term, returns its location
152 //pub fn alloc_code(&mut self, code: &str) -> Result<u64, String> {
153 //Ok(self.alloc_term(&*language::syntax::read_term(code)?))
154 //}
155
156 ///// Given a location, returns the pointer stored on it
157 //pub fn load_ptr(&self, host: u64) -> Ptr {
158 //runtime::load_ptr(&self.heap, host)
159 //}
160
161 ///// Given a location, evaluates a term to head normal form
162 //pub fn reduce(&mut self, host: u64) {
163 //runtime::reduce(&self.heap, &self.prog, &[0], host); // FIXME: add parallelism
164 //}
165
166 ///// Given a location, evaluates a term to full normal form
167 //pub fn normalize(&mut self, host: u64) {
168 //runtime::normalize(&self.heap, &self.prog, &[0], host, false);
169 //}
170
171 ///// Evaluates a code, allocs and evaluates to full normal form. Returns its location.
172 //pub fn normalize_code(&mut self, code: &str) -> u64 {
173 //let host = self.alloc_code(code).ok().unwrap();
174 //self.normalize(host);
175 //return host;
176 //}
177
178 ///// Evaluates a code to normal form. Returns its location.
179 //pub fn eval_to_loc(&mut self, code: &str) -> u64 {
180 //return self.normalize_code(code);
181 //}
182
183 ///// Evaluates a code to normal form.
184 //pub fn eval(&mut self, code: &str) -> String {
185 //let host = self.normalize_code(code);
186 //return self.show(host);
187 //}
188
189 //// /// Given a location, runs side-effective actions
190 ////#[cfg(not(target_arch = "wasm32"))]
191 ////pub fn run_io(&mut self, host: u64) {
192 ////runtime::run_io(&mut self.heap, &self.prog, &[0], host)
193 ////}
194
195 ///// Given a location, recovers the lambda Term stored on it, as code
196 //pub fn show(&self, host: u64) -> String {
197 //language::readback::as_code(&self.heap, &self.prog, host)
198 //}
199
200 ///// Given a location, recovers the linear Term stored on it, as code
201 //pub fn show_linear(&self, host: u64) -> String {
202 //language::readback::as_linear_code(&self.heap, &self.prog, host)
203 //}
204
205 ///// Return the total number of graph rewrites computed
206 //pub fn get_rewrites(&self) -> u64 {
207 //runtime::get_cost(&self.heap)
208 //}
209
210 ///// Returns the name of a given id
211 //pub fn get_name(&self, id: u64) -> String {
212 //self.book.id_to_name.get(&id).unwrap_or(&"?".to_string()).clone()
213 //}
214
215 ///// Returns the arity of a given id
216 //pub fn get_arity(&self, id: u64) -> u64 {
217 //*self.book.id_to_arit.get(&id).unwrap_or(&u64::MAX)
218 //}
219
220 ///// Returns the name of a given id
221 //pub fn get_id(&self, name: &str) -> u64 {
222 //*self.book.name_to_id.get(name).unwrap_or(&u64::MAX)
223 //}
224
225 //// WASM re-exports
226
227 //pub fn DP0() -> u64 {
228 //return DP0;
229 //}
230
231 //pub fn DP1() -> u64 {
232 //return DP1;
233 //}
234
235 //pub fn VAR() -> u64 {
236 //return VAR;
237 //}
238
239 //pub fn ARG() -> u64 {
240 //return ARG;
241 //}
242
243 //pub fn ERA() -> u64 {
244 //return ERA;
245 //}
246
247 //pub fn LAM() -> u64 {
248 //return LAM;
249 //}
250
251 //pub fn APP() -> u64 {
252 //return APP;
253 //}
254
255 //pub fn SUP() -> u64 {
256 //return SUP;
257 //}
258
259 //pub fn CTR() -> u64 {
260 //return CTR;
261 //}
262
263 //pub fn FUN() -> u64 {
264 //return FUN;
265 //}
266
267 //pub fn OP2() -> u64 {
268 //return OP2;
269 //}
270
271 //pub fn NUM() -> u64 {
272 //return NUM;
273 //}
274
275 //pub fn ADD() -> u64 {
276 //return ADD;
277 //}
278
279 //pub fn SUB() -> u64 {
280 //return SUB;
281 //}
282
283 //pub fn MUL() -> u64 {
284 //return MUL;
285 //}
286
287 //pub fn DIV() -> u64 {
288 //return DIV;
289 //}
290
291 //pub fn MOD() -> u64 {
292 //return MOD;
293 //}
294
295 //pub fn AND() -> u64 {
296 //return AND;
297 //}
298
299 //pub fn OR() -> u64 {
300 //return OR;
301 //}
302
303 //pub fn XOR() -> u64 {
304 //return XOR;
305 //}
306
307 //pub fn SHL() -> u64 {
308 //return SHL;
309 //}
310
311 //pub fn SHR() -> u64 {
312 //return SHR;
313 //}
314
315 //pub fn LTN() -> u64 {
316 //return LTN;
317 //}
318
319 //pub fn LTE() -> u64 {
320 //return LTE;
321 //}
322
323 //pub fn EQL() -> u64 {
324 //return EQL;
325 //}
326
327 //pub fn GTE() -> u64 {
328 //return GTE;
329 //}
330
331 //pub fn GTN() -> u64 {
332 //return GTN;
333 //}
334
335 //pub fn NEQ() -> u64 {
336 //return NEQ;
337 //}
338
339 //pub fn CELLS_PER_KB() -> usize {
340 //return CELLS_PER_KB;
341 //}
342
343 //pub fn CELLS_PER_MB() -> usize {
344 //return CELLS_PER_MB;
345 //}
346
347 //pub fn CELLS_PER_GB() -> usize {
348 //return CELLS_PER_GB;
349 //}
350
351 //pub fn get_tag(lnk: Ptr) -> u64 {
352 //return get_tag(lnk);
353 //}
354
355 //pub fn get_ext(lnk: Ptr) -> u64 {
356 //return get_ext(lnk);
357 //}
358
359 //pub fn get_val(lnk: Ptr) -> u64 {
360 //return get_val(lnk);
361 //}
362
363 //pub fn get_num(lnk: Ptr) -> u64 {
364 //return get_num(lnk);
365 //}
366
367 //pub fn get_loc(lnk: Ptr, arg: u64) -> u64 {
368 //return get_loc(lnk, arg);
369 //}
370
371 //pub fn Var(pos: u64) -> Ptr {
372 //return runtime::Var(pos);
373 //}
374
375 //pub fn Dp0(col: u64, pos: u64) -> Ptr {
376 //return runtime::Dp0(col, pos);
377 //}
378
379 //pub fn Dp1(col: u64, pos: u64) -> Ptr {
380 //return runtime::Dp1(col, pos);
381 //}
382
383 //pub fn Arg(pos: u64) -> Ptr {
384 //return runtime::Arg(pos);
385 //}
386
387 //pub fn Era() -> Ptr {
388 //return runtime::Era();
389 //}
390
391 //pub fn Lam(pos: u64) -> Ptr {
392 //return runtime::Lam(pos);
393 //}
394
395 //pub fn App(pos: u64) -> Ptr {
396 //return runtime::App(pos);
397 //}
398
399 //pub fn Sup(col: u64, pos: u64) -> Ptr {
400 //return runtime::Sup(col, pos);
401 //}
402
403 //pub fn Op2(ope: u64, pos: u64) -> Ptr {
404 //return runtime::Op2(ope, pos);
405 //}
406
407 //pub fn Num(val: u64) -> Ptr {
408 //return runtime::Num(val);
409 //}
410
411 //pub fn Ctr(fun: u64, pos: u64) -> Ptr {
412 //return runtime::Ctr(fun, pos);
413 //}
414
415 //pub fn Fun(fun: u64, pos: u64) -> Ptr {
416 //return runtime::Fun(fun, pos);
417 //}
418
419 //pub fn link(&mut self, loc: u64, lnk: Ptr) -> Ptr {
420 //return runtime::link(&self.heap, loc, lnk);
421 //}
422
423 //pub fn alloc(&mut self, size: u64) -> u64 {
424 //return runtime::alloc(&self.heap, 0, size); // FIXME tid?
425 //}
426
427 //pub fn free(&mut self, loc: u64, size: u64) {
428 //return runtime::free(&self.heap, 0, loc, size); // FIXME tid?
429 //}
430
431 //pub fn collect(&mut self, term: Ptr) {
432 //return runtime::collect(&self.heap, &self.prog.arit, 0, term); // FIXME tid?
433 //}
434
435//}
436
437// Methods that aren't compiled to JS
438//impl Runtime {
439 ///// Allocates a new term, returns its location
440 //pub fn alloc_term(&mut self, term: &language::syntax::Term) -> u64 {
441 //runtime::alloc_term(&self.heap, 0, &self.book, term) // FIXME tid?
442 //}
443
444 ///// Given a location, recovers the Term stored on it
445 //pub fn readback(&self, host: u64) -> Box<Term> {
446 //language::readback::as_term(&self.heap, &self.prog, host)
447 //}
448
449 ///// Given a location, recovers the Term stored on it
450 //pub fn linear_readback(&self, host: u64) -> Box<Term> {
451 //language::readback::as_linear_term(&self.heap, &self.prog, host)
452 //}
453//}
454
455//pub fn example() -> Result<(), String> {
456 //let mut rt = Runtime::from_code_with_size("
457 //(Double Zero) = Zero
458 //(Double (Succ x)) = (Succ (Succ (Double x)))
459 //", 10000).unwrap();
460 //let loc = rt.normalize_code("(Double (Succ (Succ Zero)))");
461 //println!("{}", rt.show(loc));
462 //return Ok(());
463//}
464
465////#[cfg(test)]
466////mod tests {
467 ////use crate::eval_code;
468 ////use crate::make_call;
469
470 ////#[test]
471 ////fn test() {
472 ////let code = "
473 ////(Fn 0) = 0
474 ////(Fn 1) = 1
475 ////(Fn n) = (+ (Fn (- n 1)) (Fn (- n 2)))
476 ////(Main) = (Fn 20)
477 ////";
478
479 ////let (norm, _cost, _size, _time) = language::rulebook::eval_code(&make_call("Main", &[]).unwrap(), code, false, 32 << 20).unwrap();
480 ////assert_eq!(norm, "6765");
481 ////}
482////}
483////
484
485