hvm-core 0.2.26

HVM-Core is a massively parallel Interaction Combinator evaluator.
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#![cfg_attr(feature = "trace", feature(const_type_name))]

use clap::{Args, Parser, Subcommand};
use hvmc::{
  ast::{Book, Net, Tree},
  host::Host,
  run::{DynNet, Mode, Trg},
  stdlib::{create_host, insert_stdlib},
  transform::{TransformOpts, TransformPass, TransformPasses},
  *,
};

use parking_lot::Mutex;
use std::{
  env::consts::{DLL_PREFIX, DLL_SUFFIX},
  ffi::OsStr,
  fmt::Write,
  fs::{self, File},
  io::{self, BufRead},
  path::{Path, PathBuf},
  process::{self, Stdio},
  str::FromStr,
  sync::Arc,
  time::{Duration, Instant},
};

fn main() {
  if cfg!(feature = "trace") {
    trace::set_hook();
  }
  if cfg!(feature = "_full_cli") {
    let cli = FullCli::parse();

    match cli.mode {
      CliMode::Compile { file, dylib, transform_args, output } => {
        let output = if let Some(output) = output {
          output
        } else if let Some("hvmc") = file.extension().and_then(OsStr::to_str) {
          file.with_extension("")
        } else {
          eprintln!("file missing `.hvmc` extension; explicitly specify an output path with `--output`.");

          process::exit(1);
        };

        let host = create_host(&load_book(&[file], &transform_args));
        create_temp_hvm(host).unwrap();

        if dylib {
          prepare_temp_hvm_dylib().unwrap();
          compile_temp_hvm(&["--lib"]).unwrap();

          fs::copy(format!(".hvm/target/release/{DLL_PREFIX}hvmc{DLL_SUFFIX}"), output).unwrap();
        } else {
          compile_temp_hvm(&[]).unwrap();

          fs::copy(".hvm/target/release/hvmc", output).unwrap();
        }
      }
      CliMode::Run { run_opts, mut transform_args, file, args } => {
        // Don't pre-reduce or prune the entry point
        transform_args.transform_opts.pre_reduce_skip.push(args.entry_point.clone());
        transform_args.transform_opts.prune_entrypoints.push(args.entry_point.clone());

        let host: Arc<Mutex<Host>> = Default::default();
        load_dylibs(host.clone(), &run_opts.include);
        insert_stdlib(host.clone());
        host.lock().insert_book(&load_book(&[file], &transform_args));

        run(host, run_opts, args);
      }
      CliMode::Reduce { run_opts, transform_args, files, exprs } => {
        let host = load_host(&files, &transform_args, &run_opts.include);
        let exprs: Vec<_> = exprs.iter().map(|x| Net::from_str(x).unwrap()).collect();
        reduce_exprs(host, &exprs, &run_opts);
      }
      CliMode::Transform { transform_args, files } => {
        let book = load_book(&files, &transform_args);
        println!("{}", book);
      }
    }
  } else {
    let cli = BareCli::parse();
    let host = create_host(&Book::default());
    gen::insert_into_host(&mut host.lock());
    run(host, cli.opts, cli.args);
  }
  if cfg!(feature = "trace") {
    hvmc::trace::_read_traces(usize::MAX);
  }
}

#[derive(Parser, Debug)]
#[command(
  author,
  version,
  about = "A massively parallel Interaction Combinator evaluator",
  long_about = r##"
A massively parallel Interaction Combinator evaluator

Examples: 
$ hvmc run examples/church_encoding/church.hvm
$ hvmc run examples/addition.hvmc "#16" "#3"
$ hvmc compile examples/addition.hvmc
$ hvmc reduce examples/addition.hvmc -- "a & @mul ~ (#3 (#4 a))"
$ hvmc reduce -- "a & #3 ~ <* #4 a>""##
)]
struct FullCli {
  #[command(subcommand)]
  pub mode: CliMode,
}

#[derive(Parser, Debug)]
#[command(author, version)]
struct BareCli {
  #[command(flatten)]
  pub opts: RuntimeOpts,
  #[command(flatten)]
  pub args: RunArgs,
}

#[derive(Subcommand, Clone, Debug)]
#[command(author, version)]
enum CliMode {
  /// Compile a hvm-core program into a Rust crate.
  Compile {
    /// hvm-core file to compile.
    file: PathBuf,
    /// Compile this hvm-core file to a dynamic library.
    ///
    /// These can be included when running with the `--include` option.
    #[arg(short, long)]
    dylib: bool,
    /// Output path; defaults to the input file with `.hvmc` stripped.
    #[arg(short, long)]
    output: Option<PathBuf>,
    #[command(flatten)]
    transform_args: TransformArgs,
  },
  /// Run a program, optionally passing a list of arguments to it.
  Run {
    /// Name of the file to load.
    file: PathBuf,
    #[command(flatten)]
    args: RunArgs,
    #[command(flatten)]
    run_opts: RuntimeOpts,
    #[command(flatten)]
    transform_args: TransformArgs,
  },
  /// Reduce hvm-core expressions to their normal form.
  ///
  /// The expressions are passed as command-line arguments.
  /// It is also possible to load files before reducing the expression,
  /// which makes it possible to reference definitions from the file
  /// in the expression.
  Reduce {
    /// Files to load before reducing the expressions.
    ///
    /// Multiple files will act as if they're concatenated together.
    #[arg(required = false)]
    files: Vec<PathBuf>,
    /// Expressions to reduce.
    ///
    /// The normal form of each expression will be
    /// printed on a new line. This list must be separated from the file list
    /// with a double dash ('--').
    #[arg(required = false, last = true)]
    exprs: Vec<String>,
    #[command(flatten)]
    run_opts: RuntimeOpts,
    #[command(flatten)]
    transform_args: TransformArgs,
  },
  /// Transform a hvm-core program using one of the optimization passes.
  Transform {
    /// Files to load before reducing the expressions.
    ///
    /// Multiple files will act as if they're concatenated together.
    #[arg(required = true)]
    files: Vec<PathBuf>,
    #[command(flatten)]
    transform_args: TransformArgs,
  },
}

#[derive(Args, Clone, Debug)]
struct TransformArgs {
  /// Enables or disables transformation passes.
  #[arg(short = 'O', value_delimiter = ' ', action = clap::ArgAction::Append)]
  transform_passes: Vec<TransformPass>,
  #[command(flatten)]
  transform_opts: TransformOpts,
}

#[derive(Args, Clone, Debug)]
struct RuntimeOpts {
  /// Show performance statistics.
  #[arg(short, long = "stats")]
  show_stats: bool,
  /// Single-core mode (no parallelism).
  #[arg(short = '1', long = "single")]
  single_core: bool,
  /// Lazy mode.
  ///
  /// Lazy mode only expands references that are reachable
  /// by a walk from the root of the net. This leads to a dramatic slowdown,
  /// but allows running programs that would expand indefinitely otherwise.
  #[arg(short, long = "lazy")]
  lazy_mode: bool,
  /// How much memory to allocate on startup.
  ///
  /// Supports abbreviations such as '4G' or '400M'.
  #[arg(short, long, value_parser = util::parse_abbrev_number::<usize>)]
  memory: Option<usize>,
  /// Dynamic library hvm-core files to include.
  ///
  /// hvm-core files can be compiled as dylibs with the `--dylib` option.
  #[arg(short, long, value_delimiter = ' ', action = clap::ArgAction::Append)]
  include: Vec<PathBuf>,
}

#[derive(Args, Clone, Debug)]
struct RunArgs {
  /// Name of the definition that will get reduced.
  #[arg(short, default_value = "main")]
  entry_point: String,
  /// List of arguments to pass to the program.
  ///
  /// Arguments are passed using the lambda-calculus interpretation
  /// of interaction combinators. So, for example, if the arguments are
  /// "#1" "#2" "#3", then the expression that will get reduced is
  /// `r & @main ~ (#1 (#2 (#3 r)))`.
  args: Vec<String>,
}

fn run(host: Arc<Mutex<Host>>, opts: RuntimeOpts, args: RunArgs) {
  let mut net = Net { root: Tree::Ref { nam: args.entry_point }, redexes: vec![] };
  for arg in args.args {
    let arg: Net = Net::from_str(&arg).unwrap();
    net.redexes.extend(arg.redexes);
    net.apply_tree(arg.root);
  }

  reduce_exprs(host, &[net], &opts);
}

fn load_host(
  files: &[PathBuf],
  transform_args: &TransformArgs,
  include: &[PathBuf],
) -> Arc<parking_lot::lock_api::Mutex<parking_lot::RawMutex, Host>> {
  let host: Arc<Mutex<Host>> = Default::default();
  load_dylibs(host.clone(), include);
  insert_stdlib(host.clone());
  host.lock().insert_book(&load_book(files, transform_args));
  host
}

fn load_book(files: &[PathBuf], transform_args: &TransformArgs) -> Book {
  let mut book = files
    .iter()
    .map(|name| {
      let contents = fs::read_to_string(name).unwrap_or_else(|_| {
        eprintln!("Input file {:?} not found", name);
        process::exit(1);
      });
      contents.parse::<Book>().unwrap_or_else(|e| {
        eprintln!("Parsing error {e}");
        process::exit(1);
      })
    })
    .fold(Book::default(), |mut acc, i| {
      acc.nets.extend(i.nets);
      acc
    });

  let transform_passes = TransformPasses::from(&transform_args.transform_passes[..]);
  book.transform(transform_passes, &transform_args.transform_opts).unwrap();

  book
}

fn load_dylibs(host: Arc<Mutex<Host>>, include: &[PathBuf]) {
  let current_dir = std::env::current_dir().unwrap();

  for file in include {
    unsafe {
      let lib = if file.is_absolute() {
        libloading::Library::new(file)
      } else {
        libloading::Library::new(current_dir.join(file))
      }
      .expect("failed to load dylib");

      let rust_version =
        lib.get::<fn() -> &'static str>(b"hvmc_dylib_v0__rust_version").expect("failed to load rust version");
      let rust_version = rust_version();
      if rust_version != env!("RUSTC_VERSION") {
        eprintln!(
          "warning: dylib {file:?} was compiled with rust version {rust_version}, but is being run with rust version {}",
          env!("RUSTC_VERSION")
        );
      }

      let hvmc_version =
        lib.get::<fn() -> &'static str>(b"hvmc_dylib_v0__hvmc_version").expect("failed to load hvmc version");
      let hvmc_version = hvmc_version();
      if hvmc_version != env!("CARGO_PKG_VERSION") {
        eprintln!(
          "warning: dylib {file:?} was compiled with hvmc version {hvmc_version}, but is being run with hvmc version {}",
          env!("CARGO_PKG_VERSION")
        );
      }

      let insert_into_host =
        lib.get::<fn(&mut Host)>(b"hvmc_dylib_v0__insert_host").expect("failed to load insert_host");
      insert_into_host(&mut host.lock());

      std::mem::forget(lib);
    }
  }
}

fn reduce_exprs(host: Arc<Mutex<Host>>, exprs: &[Net], opts: &RuntimeOpts) {
  let heap = run::Heap::new(opts.memory).expect("memory allocation failed");
  for expr in exprs {
    let mut net = DynNet::new(&heap, opts.lazy_mode);
    dispatch_dyn_net!(&mut net => {
      host.lock().encode_net(net, Trg::port(run::Port::new_var(net.root.addr())), expr);
      let start_time = Instant::now();
      if opts.single_core {
        net.normal();
      } else {
        net.parallel_normal();
      }
      let elapsed = start_time.elapsed();
      println!("{}", host.lock().readback(net));
      if opts.show_stats {
        print_stats(net, elapsed);
      }
    });
  }
}

fn print_stats<M: Mode>(net: &run::Net<M>, elapsed: Duration) {
  eprintln!("RWTS   : {:>15}", pretty_num(net.rwts.total()));
  eprintln!("- ANNI : {:>15}", pretty_num(net.rwts.anni));
  eprintln!("- COMM : {:>15}", pretty_num(net.rwts.comm));
  eprintln!("- ERAS : {:>15}", pretty_num(net.rwts.eras));
  eprintln!("- DREF : {:>15}", pretty_num(net.rwts.dref));
  eprintln!("- OPER : {:>15}", pretty_num(net.rwts.oper));
  eprintln!("TIME   : {:.3?}", elapsed);
  eprintln!("RPS    : {:.3} M", (net.rwts.total() as f64) / (elapsed.as_millis() as f64) / 1000.0);
}

fn pretty_num(n: u64) -> String {
  n.to_string()
    .as_bytes()
    .rchunks(3)
    .rev()
    .map(|x| std::str::from_utf8(x).unwrap())
    .flat_map(|x| ["_", x])
    .skip(1)
    .collect()
}

/// Copies the `hvm-core` source to a temporary `.hvm` directory.
/// Only a subset of `Cargo.toml` is included.
fn create_temp_hvm(host: Arc<Mutex<host::Host>>) -> Result<(), io::Error> {
  let gen = compile::compile_host(&host.lock());
  let outdir = ".hvm";
  if Path::new(&outdir).exists() {
    fs::remove_dir_all(outdir)?;
  }
  let cargo_toml = include_str!("../Cargo.toml");
  let mut cargo_toml = cargo_toml.split_once("##--COMPILER-CUTOFF--##").unwrap().0.to_owned();
  cargo_toml.push_str("[features]\ndefault = ['cli']\ncli = ['std', 'dep:clap']\nstd = []");

  macro_rules! include_files {
    ($([$($prefix:ident)*])? $mod:ident {$($sub:tt)*} $($rest:tt)*) => {
      fs::create_dir_all(concat!(".hvm/src/", $($(stringify!($prefix), "/",)*)? stringify!($mod)))?;
      include_files!([$($($prefix)* $mod)?] $($sub)*);
      include_files!([$($($prefix)*)?] $mod $($rest)*);
    };
    ($([$($prefix:ident)*])? $file:ident $($rest:tt)*) => {
      fs::write(
        concat!(".hvm/src/", $($(stringify!($prefix), "/",)*)* stringify!($file), ".rs"),
        include_str!(concat!($($(stringify!($prefix), "/",)*)* stringify!($file), ".rs")),
      )?;
      include_files!([$($($prefix)*)?] $($rest)*);
    };
    ($([$($prefix:ident)*])?) => {};
  }

  fs::create_dir_all(".hvm/src")?;
  fs::write(".hvm/Cargo.toml", cargo_toml)?;
  fs::write(".hvm/src/gen.rs", gen)?;

  include_files! {
    ast
    compile
    fuzz
    host {
      calc_labels
      encode
      readback
    }
    lib
    main
    ops {
      num
      word
    }
    prelude
    run {
      addr
      allocator
      def
      dyn_net
      instruction
      interact
      linker
      net
      node
      parallel
      port
      wire
    }
    stdlib
    trace
    transform {
      coalesce_ctrs
      encode_adts
      eta_reduce
      inline
      pre_reduce
      prune
    }
    util {
      apply_tree
      array_vec
      bi_enum
      create_var
      deref
      maybe_grow
      parse_abbrev_number
      stats
    }
  }

  Ok(())
}

/// Appends a function to `lib.rs` that will be dynamically loaded
/// by hvm-core when the generated dylib is included.
fn prepare_temp_hvm_dylib() -> Result<(), io::Error> {
  insert_crate_type_cargo_toml()?;

  let mut lib = fs::read_to_string(".hvm/src/lib.rs")?;

  writeln!(lib).unwrap();
  writeln!(
    lib,
    r#"
#[no_mangle]
pub fn hvmc_dylib_v0__insert_host(host: &mut host::Host) {{
  gen::insert_into_host(host)
}}

#[no_mangle]
pub fn hvmc_dylib_v0__hvmc_version() -> &'static str {{
  {hvmc_version:?}
}}

#[no_mangle]
pub fn hvmc_dylib_v0__rust_version() -> &'static str {{
  {rust_version:?}
}}
  "#,
    hvmc_version = env!("CARGO_PKG_VERSION"),
    rust_version = env!("RUSTC_VERSION"),
  )
  .unwrap();

  fs::write(".hvm/src/lib.rs", lib)
}

/// Adds `crate_type = ["dylib"]` under the `[lib]` section of `Cargo.toml`.
fn insert_crate_type_cargo_toml() -> Result<(), io::Error> {
  let mut cargo_toml = String::new();

  let file = File::open(".hvm/Cargo.toml")?;
  for line in io::BufReader::new(file).lines() {
    let line = line?;
    writeln!(cargo_toml, "{line}").unwrap();

    if line == "[lib]" {
      writeln!(cargo_toml, r#"crate_type = ["dylib"]"#).unwrap();
    }
  }

  fs::write(".hvm/Cargo.toml", cargo_toml)
}

/// Compiles the `.hvm` directory, appending the provided `args` to `cargo`.
fn compile_temp_hvm(args: &[&'static str]) -> Result<(), io::Error> {
  let output = process::Command::new("cargo")
    .current_dir(".hvm")
    .arg("build")
    .arg("--release")
    .args(args)
    .stderr(Stdio::inherit())
    .output()?;

  if !output.status.success() {
    process::exit(1);
  }

  Ok(())
}