Repository: HigherOrderCO/Bend
File: src/lib.rs (Rust)

Code before:
}

pub fn compile_book(book: &mut Book, opt_level: OptimizationLevel) -> Result<CompileResult, String> {
  let main = desugar_book(book, opt_level)?;
  let (nets, hvmc_names, labels) = book_to_nets(book, main);
  let mut core_book = nets_to_hvmc(nets, &hvmc_names)?;
  pre_reduce_book(&mut core_book, opt_level >= OptimizationLevel::Heavy)?;
  if opt_level >= OptimizationLevel::Heavy {
    prune_defs(&mut core_book);
  }
  Ok(CompileResult { core_book, hvmc_names, labels, warnings: vec![] })
}

pub fn desugar_book(book: &mut Book, opt_level: OptimizationLevel) -> Result<DefId, String> {
  let main = book.check_has_main()?;
  book.check_shared_names()?;
  book.encode_strs()?;
  book.encode_lists()?;
  book.generate_scott_adts();
  book.resolve_refs();
  encode_pattern_matching(book)?;
  book.check_unbound_vars()?;
  book.make_var_names_unique();
  book.linearize_vars();
  if opt_level >= OptimizationLevel::Heavy {
    book.eta_reduction(false);
  }
  book.detach_supercombinators();
  if opt_level >= OptimizationLevel::Heavy {
    book.simplify_ref_to_ref()?;
  }
  book.simplify_main_ref(main);
  if opt_level >= OptimizationLevel::Heavy {
    book.prune(main);
  }
  Ok(main)
}

pub fn encode_pattern_matching(book: &mut Book) -> Result<(), String> {
  book.resolve_ctrs_in_pats();
  book.check_unbound_pats()?;
  book.desugar_let_destructors();
  book.desugar_implicit_match_binds();
  book.extract_matches()?;
  book.flatten_rules();
  let def_types = book.infer_def_types()?;
  book.check_exhaustive_patterns(&def_types)?;
  book.encode_pattern_matching_functions(&def_types);
  Ok(())
}


Review comment (suggestion):
Actually, we probably want this true or false to be gated by the optimization level. `bool.eta_reduction(opt_level >= OptimizationLevel::Heavy)`.

Code after:
}

pub fn compile_book(book: &mut Book, opt_level: OptimizationLevel) -> Result<CompileResult, String> {
  let main = desugar_book(book, opt_level)?;
  let (nets, hvmc_names, labels) = book_to_nets(book, main);
  let mut core_book = nets_to_hvmc(nets, &hvmc_names)?;
  pre_reduce_book(&mut core_book, opt_level >= OptimizationLevel::Heavy)?;
  if opt_level >= OptimizationLevel::Heavy {
    prune_defs(&mut core_book);
  }
  Ok(CompileResult { core_book, hvmc_names, labels, warnings: vec![] })
}

pub fn desugar_book(book: &mut Book, opt_level: OptimizationLevel) -> Result<DefId, String> {
  let main = book.check_has_main()?;
  book.check_shared_names()?;
  book.encode_strs()?;
  book.encode_lists()?;
  book.generate_scott_adts();
  book.resolve_refs();
  encode_pattern_matching(book)?;
  book.check_unbound_vars()?;
  book.make_var_names_unique();
  book.linearize_vars();
  book.eta_reduction(opt_level >= OptimizationLevel::Heavy);
  book.detach_supercombinators(main);
  if opt_level >= OptimizationLevel::Heavy {
    book.simplify_ref_to_ref()?;
  }
  book.simplify_main_ref(main);
  if opt_level >= OptimizationLevel::Heavy {
    book.prune(main);
  }
  Ok(main)
}

pub fn encode_pattern_matching(book: &mut Book) -> Result<(), String> {
  book.resolve_ctrs_in_pats();
  book.check_unbound_pats()?;
  book.desugar_let_destructors();
  book.desugar_implicit_match_binds();
  book.extract_matches()?;
  book.flatten_rules();
  let def_types = book.infer_def_types()?;
  book.check_exhaustive_patterns(&def_types)?;
  book.encode_pattern_matching_functions(&def_types);
  Ok(())
}

pub fn run_book(
  mut book: Book,