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
#![feature(box_patterns)]
#![feature(let_chains)]

use crate::fun::{book_to_nets, net_to_term::net_to_term, term_to_net::Labels, Book, Ctx, Term};
use diagnostics::{Diagnostics, DiagnosticsConfig, ERR_INDENT_SIZE};
use hvm::{
  add_recursive_priority::add_recursive_priority,
  check_net_size::{check_net_sizes, MAX_NET_SIZE},
  mutual_recursion,
};
use hvmc::ast::Net;
use net::hvmc_to_net::hvmc_to_net;
use std::{process::Output, str::FromStr};

pub mod diagnostics;
pub mod fun;
pub mod hvm;
pub mod imp;
pub mod net;

pub use fun::load_book::load_file_to_book;

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

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_nets(book, &mut diagnostics)?;

  if opts.eta {
    hvm_book.values_mut().for_each(Net::eta_reduce);
  }

  mutual_recursion::check_cycles(&hvm_book, &mut diagnostics)?;
  if opts.eta {
    hvm_book.values_mut().for_each(Net::eta_reduce);
  }

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

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

  check_net_sizes(&hvm_book, &mut diagnostics)?;

  add_recursive_priority(&mut hvm_book);

  Ok(CompileResult { core_book: 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();

  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.desugar_bend()?;
  ctx.desugar_fold()?;
  ctx.desugar_do_blocks()?;

  ctx.check_unbound_vars()?;

  ctx.book.make_var_names_unique();

  // Auto match linearization
  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();

  ctx.book.encode_matches();

  // sanity check
  ctx.check_unbound_vars()?;

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

  // sanity check
  ctx.check_unbound_vars()?;

  // Optimizing passes
  if opts.float_combinators {
    ctx.book.float_combinators(MAX_NET_SIZE);
  }

  ctx.prune(opts.prune);

  if opts.merge {
    ctx.book.merge_definitions();
  }

  ctx.book.make_var_names_unique();

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

pub fn run_book_with_fn(
  mut book: Book,
  run_opts: RunOpts,
  compile_opts: CompileOpts,
  diagnostics_cfg: DiagnosticsConfig,
  args: Option<Vec<Term>>,
  cmd: &str,
  arg_io: bool,
) -> Result<Option<(Term, String, Diagnostics)>, Diagnostics> {
  let CompileResult { 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_path = ".out.hvm";
  std::fs::write(out_path, core_book.to_string()).map_err(|x| x.to_string())?;
  let run_fn = |out_path: &str| {
    let mut process = std::process::Command::new("hvm");
    process.arg(cmd).arg(out_path);
    if arg_io {
      process.arg("--io");
      process.stdout(std::process::Stdio::inherit());
      process.spawn()?.wait_with_output()
    } else {
      process.output()
    }
  };
  let Output { status, stdout, stderr } = run_fn(out_path).map_err(|e| format!("While running hvm: {e}"))?;

  let out = String::from_utf8_lossy(&stdout);
  let err = String::from_utf8_lossy(&stderr);
  let status = if !status.success() { status.to_string() } else { String::new() };

  if arg_io {
    return Ok(None);
  }

  let Some((_, result)) = out.split_once("Result: ") else {
    return Err(format!("Error reading result from hvm. Output :\n{}{}{}", err, status, out).into());
  };
  let Some((result, stats)) = result.split_once('\n') else {
    return Err(format!("Error reading result from hvm. Output :\n{}{}{}", err, status, out).into());
  };
  let Ok(net) = hvmc::ast::Net::from_str(result) else {
    return Err(format!("Error reading result from hvm. Output :\n{}{}{}", err, status, out).into());
  };

  let (term, diags) = readback_hvm_net(&net, &book, &labels, run_opts.linear_readback);
  Ok(Some((term, stats.to_string(), diags)))
}

pub fn run_book(
  book: Book,
  run_opts: RunOpts,
  compile_opts: CompileOpts,
  diagnostics_cfg: DiagnosticsConfig,
  args: Option<Vec<Term>>,
) -> Result<(Term, String, Diagnostics), Diagnostics> {
  run_book_with_fn(book, run_opts, compile_opts, diagnostics_cfg, args, "run", false).map(Option::unwrap)
}

pub fn readback_hvm_net(net: &Net, book: &Book, labels: &Labels, linear: bool) -> (Term, Diagnostics) {
  let mut diags = Diagnostics::default();
  let net = hvmc_to_net(net);
  let mut term = net_to_term(&net, book, labels, linear, &mut diags);
  term.expand_generated(book);
  term.resugar_strings();
  term.resugar_lists();
  (term, diags)
}

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

#[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)]
pub struct CompileOpts {
  /// Enables [fun::transform::eta_reduction].
  pub eta: bool,

  /// Enables [fun::transform::definition_pruning] and [hvmc_net::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 [fun::transform::inline].
  pub inline: bool,
}

impl CompileOpts {
  /// Set all opts as true and keep the current adt encoding.
  #[must_use]
  pub fn set_all(self) -> Self {
    Self {
      eta: true,
      prune: true,
      float_combinators: true,
      merge: true,
      inline: true,
      linearize_matches: OptLevel::Enabled,
    }
  }

  /// Set all opts as false and keep the current adt encoding.
  #[must_use]
  pub fn set_no_all(self) -> Self {
    Self {
      eta: false,
      prune: false,
      linearize_matches: OptLevel::Disabled,
      float_combinators: false,
      merge: false,
      inline: false,
    }
  }

  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 and reorder_redexes_recursive_last.
  fn default() -> Self {
    Self {
      eta: true,
      prune: false,
      linearize_matches: OptLevel::Enabled,
      float_combinators: true,
      merge: false,
      inline: false,
    }
  }
}

pub struct CompileResult {
  pub diagnostics: Diagnostics,
  pub core_book: hvmc::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)
}