bend/
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
use crate::{
  fun::{book_to_hvm, net_to_term::net_to_term, term_to_net::Labels, Book, Ctx, Term},
  hvm::{
    add_recursive_priority::add_recursive_priority,
    check_net_size::{check_net_sizes, MAX_NET_SIZE_CUDA},
    eta_reduce::eta_reduce_hvm_net,
    hvm_book_show_pretty,
    inline::inline_hvm_book,
    mutual_recursion,
    prune::prune_hvm_book,
  },
};
use diagnostics::{Diagnostics, DiagnosticsConfig, ERR_INDENT_SIZE};
use net::hvm_to_net::hvm_to_net;

pub mod diagnostics;
// `Name` triggers this warning, but it's safe because we're not using its internal mutability.
#[allow(clippy::mutable_key_type)]
pub mod fun;
pub mod hvm;
pub mod imp;
pub mod imports;
pub mod net;
mod utils;

pub use fun::load_book::{load_file_to_book, load_to_book};

pub const ENTRY_POINT: &str = "main";
pub const HVM1_ENTRY_POINT: &str = "Main";
pub const HVM_OUTPUT_END_MARKER: &str = "Result: ";

pub fn check_book(
  book: &mut Book,
  diagnostics_cfg: DiagnosticsConfig,
  compile_opts: CompileOpts,
) -> Result<Diagnostics, Diagnostics> {
  // TODO: Do the checks without having to do full compilation
  let res = compile_book(book, compile_opts, diagnostics_cfg, None)?;
  Ok(res.diagnostics)
}

pub fn compile_book(
  book: &mut Book,
  opts: CompileOpts,
  diagnostics_cfg: DiagnosticsConfig,
  args: Option<Vec<Term>>,
) -> Result<CompileResult, Diagnostics> {
  let mut diagnostics = desugar_book(book, opts.clone(), diagnostics_cfg, args)?;

  let (mut hvm_book, labels) = book_to_hvm(book, &mut diagnostics)?;

  if opts.eta {
    hvm_book.defs.values_mut().for_each(eta_reduce_hvm_net);
  }

  mutual_recursion::check_cycles(&hvm_book, &mut diagnostics)?;

  if opts.eta {
    hvm_book.defs.values_mut().for_each(eta_reduce_hvm_net);
  }

  if opts.inline {
    if let Err(e) = inline_hvm_book(&mut hvm_book) {
      diagnostics.add_book_error(format!("During inlining:\n{:ERR_INDENT_SIZE$}{}", "", e));
    }
    diagnostics.fatal(())?;
  }

  if opts.prune {
    let prune_entrypoints = vec![book.hvm_entrypoint().to_string()];
    prune_hvm_book(&mut hvm_book, &prune_entrypoints);
  }

  if opts.check_net_size {
    check_net_sizes(&hvm_book, &mut diagnostics, &opts.target_architecture)?;
  }

  add_recursive_priority(&mut hvm_book);

  Ok(CompileResult { hvm_book, labels, diagnostics })
}

pub fn desugar_book(
  book: &mut Book,
  opts: CompileOpts,
  diagnostics_cfg: DiagnosticsConfig,
  args: Option<Vec<Term>>,
) -> Result<Diagnostics, Diagnostics> {
  let mut ctx = Ctx::new(book, diagnostics_cfg);

  ctx.check_shared_names();

  ctx.set_entrypoint();

  ctx.book.encode_adts(opts.adt_encoding);

  ctx.fix_match_defs()?;

  ctx.apply_args(args)?;

  ctx.desugar_open()?;

  ctx.book.encode_builtins();

  ctx.resolve_refs()?;

  ctx.desugar_match_defs()?;

  ctx.fix_match_terms()?;

  ctx.book.lift_local_defs();

  ctx.desugar_bend()?;
  ctx.desugar_fold()?;
  ctx.desugar_with_blocks()?;

  ctx.check_unbound_vars()?;

  // Auto match linearization
  ctx.book.make_var_names_unique();
  ctx.book.desugar_use();

  match opts.linearize_matches {
    OptLevel::Disabled => (),
    OptLevel::Alt => ctx.book.linearize_match_binds(),
    OptLevel::Enabled => ctx.book.linearize_matches(),
  }
  // Manual match linearization
  ctx.book.linearize_match_with();

  if opts.type_check {
    type_check_book(&mut ctx)?;
  }

  ctx.book.encode_matches(opts.adt_encoding);

  // sanity check
  ctx.check_unbound_vars()?;

  ctx.book.make_var_names_unique();
  ctx.book.desugar_use();

  ctx.book.make_var_names_unique();
  ctx.book.linearize_vars();

  // sanity check
  ctx.check_unbound_vars()?;

  if opts.float_combinators {
    ctx.book.float_combinators(MAX_NET_SIZE_CUDA);
  }
  // sanity check
  ctx.check_unbound_refs()?;

  // Optimizing passes
  ctx.prune(opts.prune);
  if opts.merge {
    ctx.book.merge_definitions();
  }

  ctx.book.expand_main();

  ctx.book.make_var_names_unique();

  if !ctx.info.has_errors() {
    Ok(ctx.info)
  } else {
    Err(ctx.info)
  }
}

pub fn type_check_book(ctx: &mut Ctx) -> Result<(), Diagnostics> {
  ctx.check_untyped_terms()?;
  ctx.resolve_type_ctrs()?;
  ctx.type_check()?;
  Ok(())
}

pub fn run_book(
  mut book: Book,
  run_opts: RunOpts,
  compile_opts: CompileOpts,
  diagnostics_cfg: DiagnosticsConfig,
  args: Option<Vec<Term>>,
  cmd: &str,
) -> Result<Option<(Term, String, Diagnostics)>, Diagnostics> {
  let CompileResult { hvm_book: core_book, labels, diagnostics } =
    compile_book(&mut book, compile_opts.clone(), diagnostics_cfg, args)?;

  // TODO: Printing should be taken care by the cli module, but we'd
  // like to print any warnings before running so that the user can
  // cancel the run if a problem is detected.
  eprint!("{diagnostics}");

  let out = run_hvm(&core_book, cmd, &run_opts)?;
  let (net, stats) = parse_hvm_output(&out)?;
  let (term, diags) =
    readback_hvm_net(&net, &book, &labels, run_opts.linear_readback, compile_opts.adt_encoding);

  Ok(Some((term, stats, diags)))
}

pub fn readback_hvm_net(
  net: &::hvm::ast::Net,
  book: &Book,
  labels: &Labels,
  linear: bool,
  adt_encoding: AdtEncoding,
) -> (Term, Diagnostics) {
  let mut diags = Diagnostics::default();
  let net = hvm_to_net(net);
  let mut term = net_to_term(&net, book, labels, linear, &mut diags);
  #[allow(clippy::mutable_key_type)] // Safe to allow, we know how `Name` works.
  let recursive_defs = book.recursive_defs();
  term.expand_generated(book, &recursive_defs);
  term.resugar_strings(adt_encoding);
  term.resugar_lists(adt_encoding);
  (term, diags)
}

/// Runs an HVM book by invoking HVM as a subprocess.
fn run_hvm(book: &::hvm::ast::Book, cmd: &str, run_opts: &RunOpts) -> Result<String, String> {
  let out_path = ".out.hvm";
  std::fs::write(out_path, hvm_book_show_pretty(book)).map_err(|x| x.to_string())?;
  let mut process = std::process::Command::new(run_opts.hvm_path.clone())
    .arg(cmd)
    .arg(out_path)
    .stdout(std::process::Stdio::piped())
    .stderr(std::process::Stdio::inherit())
    .spawn()
    .map_err(|e| format!("Failed to start hvm process.\n{e}"))?;

  let child_out = std::mem::take(&mut process.stdout).expect("Failed to attach to hvm output");
  let thread_out = std::thread::spawn(move || filter_hvm_output(child_out, std::io::stdout()));

  let _ = process.wait().expect("Failed to wait on hvm subprocess");
  if let Err(e) = std::fs::remove_file(out_path) {
    eprintln!("Error removing HVM output file. {e}");
  }

  let result = thread_out.join().map_err(|_| "HVM output thread panicked.".to_string())??;
  Ok(result)
}

/// Reads the final output from HVM and separates the extra information.
fn parse_hvm_output(out: &str) -> Result<(::hvm::ast::Net, String), String> {
  let Some((result, stats)) = out.split_once('\n') else {
    return Err(format!(
      "Failed to parse result from HVM (unterminated result).\nOutput from HVM was:\n{:?}",
      out
    ));
  };
  let mut p = ::hvm::ast::CoreParser::new(result);
  let Ok(net) = p.parse_net() else {
    return Err(format!("Failed to parse result from HVM (invalid net).\nOutput from HVM was:\n{:?}", out));
  };
  Ok((net, stats.to_string()))
}

/// Filters the output from HVM, separating user output from the
/// result, used for readback and displaying stats.
///
/// Buffers the output from HVM to try to parse it.
fn filter_hvm_output(
  mut stream: impl std::io::Read + Send,
  mut output: impl std::io::Write + Send,
) -> Result<String, String> {
  let mut capturing = false;
  let mut result = String::new();
  let mut buf = [0u8; 1024];
  loop {
    let num_read = match stream.read(&mut buf) {
      Ok(n) => n,
      Err(e) => {
        eprintln!("{e}");
        break;
      }
    };
    if num_read == 0 {
      break;
    }
    let new_buf = &buf[..num_read];
    // TODO: Does this lead to broken characters if printing too much at once?
    let new_str = String::from_utf8_lossy(new_buf);
    if capturing {
      // Store the result
      result.push_str(&new_str);
    } else if let Some((before, after)) = new_str.split_once(HVM_OUTPUT_END_MARKER) {
      // If result started in the middle of the buffer, print what came before and start capturing.
      if let Err(e) = output.write_all(before.as_bytes()) {
        eprintln!("Error writing HVM output. {e}");
      };
      result.push_str(after);
      capturing = true;
    } else {
      // Otherwise, don't capture anything
      if let Err(e) = output.write_all(new_buf) {
        eprintln!("Error writing HVM output. {e}");
      }
    }
  }

  if capturing {
    Ok(result)
  } else {
    output.flush().map_err(|e| format!("Error flushing HVM output. {e}"))?;
    let msg = "HVM output had no result (An error likely occurred)".to_string();
    Err(msg)
  }
}

#[derive(Clone, Debug)]
pub struct RunOpts {
  pub linear_readback: bool,
  pub pretty: bool,
  pub hvm_path: String,
}

impl Default for RunOpts {
  fn default() -> Self {
    RunOpts { linear_readback: false, pretty: false, hvm_path: "hvm".to_string() }
  }
}

#[derive(Clone, Copy, Debug, Default)]
pub enum OptLevel {
  Disabled,
  #[default]
  Enabled,
  Alt,
}

impl OptLevel {
  pub fn enabled(&self) -> bool {
    !matches!(self, OptLevel::Disabled)
  }

  pub fn is_extra(&self) -> bool {
    matches!(self, OptLevel::Enabled)
  }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CompilerTarget {
  C,
  Cuda,
  Unknown,
}

#[derive(Clone, Debug)]
pub struct CompileOpts {
  /// The Compiler target architecture
  pub target_architecture: CompilerTarget,

  /// Enables [hvm::eta_reduce].
  pub eta: bool,

  /// Enables [fun::transform::definition_pruning] and [hvm::prune].
  pub prune: bool,

  /// Enables [fun::transform::linearize_matches].
  pub linearize_matches: OptLevel,

  /// Enables [fun::transform::float_combinators].
  pub float_combinators: bool,

  /// Enables [fun::transform::definition_merge]
  pub merge: bool,

  /// Enables [hvm::inline].
  pub inline: bool,

  /// Enables [hvm::check_net_size].
  pub check_net_size: bool,

  /// Enables [type_check_book].
  pub type_check: bool,

  /// Determines the encoding of constructors and matches.
  pub adt_encoding: AdtEncoding,
}

impl CompileOpts {
  /// Set all optimizing options as true
  #[must_use]
  pub fn set_all(self) -> Self {
    Self {
      target_architecture: self.target_architecture,
      eta: true,
      prune: true,
      float_combinators: true,
      merge: true,
      linearize_matches: OptLevel::Enabled,
      type_check: true,
      inline: true,
      check_net_size: self.check_net_size,
      adt_encoding: self.adt_encoding,
    }
  }

  /// Set all optimizing options as false
  #[must_use]
  pub fn set_no_all(self) -> Self {
    Self {
      target_architecture: self.target_architecture,
      eta: false,
      prune: false,
      linearize_matches: OptLevel::Disabled,
      float_combinators: false,
      merge: false,
      inline: false,
      type_check: self.type_check,
      check_net_size: self.check_net_size,
      adt_encoding: self.adt_encoding,
    }
  }

  pub fn check_for_strict(&self) {
    if !self.float_combinators {
      println!(
        "Warning: Running in strict mode without enabling the float_combinators pass can lead to some functions expanding infinitely."
      );
    }
    if !self.linearize_matches.enabled() {
      println!(
        "Warning: Running in strict mode without enabling the linearize_matches pass can lead to some functions expanding infinitely."
      );
    }
  }
}

impl Default for CompileOpts {
  /// Enables eta, linearize_matches, float_combinators.
  /// Uses num-scott ADT encoding.
  fn default() -> Self {
    Self {
      target_architecture: CompilerTarget::Unknown,
      eta: true,
      prune: false,
      linearize_matches: OptLevel::Enabled,
      float_combinators: true,
      merge: false,
      inline: false,
      check_net_size: true,
      type_check: true,
      adt_encoding: AdtEncoding::NumScott,
    }
  }
}

#[derive(Clone, Copy, Debug)]
pub enum AdtEncoding {
  Scott,
  NumScott,
}

impl std::fmt::Display for AdtEncoding {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      AdtEncoding::Scott => write!(f, "Scott"),
      AdtEncoding::NumScott => write!(f, "NumScott"),
    }
  }
}

pub struct CompileResult {
  pub diagnostics: Diagnostics,
  pub hvm_book: ::hvm::ast::Book,
  pub labels: Labels,
}

fn maybe_grow<R, F>(f: F) -> R
where
  F: FnOnce() -> R,
{
  stacker::maybe_grow(1024 * 32, 1024 * 1024, f)
}