Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read terms directly from hvmc nets #274

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ hvm-core = { git = "https://github.com/HigherOrderCO/hvm-core.git" }
indexmap = "2.2.3"
interner = "0.2.1"
itertools = "0.11.0"
loaned = "0.1.0"
loaned = "0.1.1"
parking_lot = "0.12.1"
stacker = "0.1"

Expand Down
12 changes: 6 additions & 6 deletions src/builtins/fs.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{
builtins::util::{AsDefFunction, FunctionLike, FunctionLikeHosted},
readback_hvmc,
readback_with_errors,
term::{
term_to_net::{term_to_net, Labels},
encoding::{encode_term, Labels},
AdtEncoding, Book, Term,
},
};
Expand Down Expand Up @@ -46,7 +46,7 @@ pub(crate) fn add_fs_defs(
let slf = self.clone();
hvmc::stdlib::readback(net, slf.readback_data.host.clone(), Trg::wire(input), move |net, tree| {
dispatch_dyn_net!(net => {
let (term, _errs) = readback_hvmc(&ast::Net { root: tree,redexes: vec![]} , &slf.readback_data.book, &slf.readback_data.labels, false, slf.readback_data.adt_encoding);
let (term, _errs) = readback_with_errors(&ast::Net { root: tree,redexes: vec![]} , &slf.readback_data.book, &slf.readback_data.labels, false, slf.readback_data.adt_encoding);
let filename = if let Term::Str { ref val } = term {
Some(val.to_string())
} else {
Expand All @@ -72,7 +72,7 @@ pub(crate) fn add_fs_defs(
let host = self.readback_data.host.clone();
hvmc::stdlib::readback(net, self.readback_data.host.clone(), Trg::wire(input), move |net, tree| {
dispatch_dyn_net!(net => {
let (term, _errs) = readback_hvmc(&ast::Net { root: tree,redexes: vec![]} , &slf.readback_data.book, &slf.readback_data.labels, false, slf.readback_data.adt_encoding);
let (term, _errs) = readback_with_errors(&ast::Net { root: tree,redexes: vec![]} , &slf.readback_data.book, &slf.readback_data.labels, false, slf.readback_data.adt_encoding);
let contents = if let Term::Str { ref val } = term {
Some(val.to_string())
} else {
Expand All @@ -93,7 +93,7 @@ pub(crate) fn add_fs_defs(
}
},
};
match term_to_net(&result, &mut labels) {
match encode_term(&result, &mut labels) {
Ok(result) => {
// Return λx (x result)
let app = net.create_node(hvmc::run::Tag::Ctr, 0);
Expand Down Expand Up @@ -129,7 +129,7 @@ pub(crate) fn add_fs_defs(
Err(s) => Term::encode_err(Term::encode_str(&s)),
};
let mut labels = (*self.readback_data.labels).clone();
if let Ok(result) = term_to_net(&result, &mut labels) {
if let Ok(result) = encode_term(&result, &mut labels) {
self.readback_data.host.lock().encode_net(net, Trg::port(app.p1), &result);
} else {
eprintln!("{VICIOUS_CIRCLE_MSG}");
Expand Down
8 changes: 4 additions & 4 deletions src/builtins/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use self::query::make_query_def;
use crate::{
readback_hvmc,
readback_with_errors,
term::{
builtins::{RESULT_ERR, RESULT_OK, SCONS, SNIL},
term_to_net::Labels,
encoding::Labels,
AdtEncoding, Book, Term,
},
};
Expand Down Expand Up @@ -36,7 +36,7 @@ pub fn create_host(book: Arc<Book>, labels: Arc<Labels>, adt_encoding: AdtEncodi
let labels = labels.clone();
move |tree| {
let net = hvmc::ast::Net { root: tree, redexes: vec![] };
let (term, errs) = readback_hvmc(&net, &book, &labels, false, adt_encoding);
let (term, errs) = readback_with_errors(&net, &book, &labels, false, adt_encoding);
println!("{}{}", errs.display_with_severity(crate::diagnostics::Severity::Error), term);
}
})
Expand All @@ -47,7 +47,7 @@ pub fn create_host(book: Arc<Book>, labels: Arc<Labels>, adt_encoding: AdtEncodi
let labels = labels.clone();
move |tree| {
let net = hvmc::ast::Net { root: tree, redexes: vec![] };
let (term, _errs) = readback_hvmc(&net, &book, &labels, false, adt_encoding);
let (term, _errs) = readback_with_errors(&net, &book, &labels, false, adt_encoding);
if let Term::Str { val } = &term {
println!("{val}");
}
Expand Down
4 changes: 2 additions & 2 deletions src/builtins/query.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
builtins::util::{AsDefFunction, FunctionLikeHosted},
term::{term_to_net::Labels, Term},
term::{encoding::Labels, Term},
};
use hvmc::{
host::{DefRef, Host},
Expand All @@ -25,7 +25,7 @@ pub(crate) fn make_query_def(host: Arc<Mutex<Host>>, labels: Arc<Labels>) -> Def
let text = Term::encode_ok(Term::encode_str(buf));
let mut labs = (*self.labels).clone();
net.link_wire_port(output, app_node.p2);
let Ok(text) = crate::term::term_to_net::term_to_net(&text, &mut labs) else {
let Ok(text) = crate::term::encoding::encode_term(&text, &mut labs) else {
net.link_port_port(Port::ERA, app_node.p1);
net.link_wire_port(input, app_node.p0);
return;
Expand Down
38 changes: 17 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![feature(let_chains)]

use builtins::{create_host, CORE_BUILTINS_USES};
use diagnostics::{DiagnosticOrigin, Diagnostics, DiagnosticsConfig, Severity, WarningType};
use diagnostics::{Diagnostics, DiagnosticsConfig, Severity, WarningType};
use hvmc::{
ast::Net,
dispatch_dyn_net,
Expand All @@ -13,19 +13,19 @@ use hvmc_net::{
mutual_recursion,
pre_reduce::{pre_reduce, MAX_REWRITES_DEFAULT},
};
use net::hvmc_to_net::hvmc_to_net;
use parking_lot::Mutex;
use std::{sync::Arc, time::Instant};
use term::{book_to_nets, net_to_term::net_to_term, term_to_net::Labels, AdtEncoding, Book, Ctx, Name, Term};
use term::{encode_book, encoding::Labels, AdtEncoding, Book, Ctx, Name, Term};

pub mod builtins;
pub mod diagnostics;
pub mod hvmc_net;
pub mod net;
pub mod term;

pub use term::load_book::load_file_to_book;

use crate::term::readback;

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

Expand All @@ -47,7 +47,7 @@ pub fn compile_book(
args: Option<Vec<Term>>,
) -> Result<CompileResult, Diagnostics> {
let mut diagnostics = desugar_book(book, opts.clone(), diagnostics_cfg, args)?;
let (mut core_book, labels) = book_to_nets(book, &mut diagnostics)?;
let (mut core_book, labels) = encode_book(book, &mut diagnostics)?;

if opts.eta {
core_book.values_mut().for_each(Net::eta_reduce);
Expand Down Expand Up @@ -181,15 +181,15 @@ pub fn run_book(
let book = Arc::new(book);
let labels = Arc::new(labels);

let debug_hook = run_opts.debug_hook(&book, &labels);
let debug_hook = run_opts.debug_hook(&book, &labels, compile_opts.adt_encoding);

let host = create_host(book.clone(), labels.clone(), compile_opts.adt_encoding);
host.lock().insert_book(&core_book);

let (res_lnet, stats) = run_compiled(host, max_memory, run_opts, debug_hook, book.hvmc_entrypoint());

let (res_term, diagnostics) =
readback_hvmc(&res_lnet, &book, &labels, run_opts.linear, compile_opts.adt_encoding);
readback_with_errors(&res_lnet, &book, &labels, run_opts.linear, compile_opts.adt_encoding);

let info = RunInfo { stats, diagnostics, net: res_lnet, book, labels };
Ok((res_term, info))
Expand Down Expand Up @@ -255,23 +255,15 @@ pub fn run_compiled(
})
}

pub fn readback_hvmc(
pub fn readback_with_errors(
net: &Net,
book: &Arc<Book>,
labels: &Arc<Labels>,
linear: bool,
adt_encoding: AdtEncoding,
) -> (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);

let resugar_errs = term.resugar_adts(book, adt_encoding);
term.resugar_builtins();

for err in resugar_errs {
diags.add_diagnostic(err, Severity::Warning, DiagnosticOrigin::Readback);
}
let term = readback(net, book, labels, linear, &mut diags, adt_encoding);

(term, diags)
}
Expand Down Expand Up @@ -338,12 +330,16 @@ impl RunOpts {
Self { lazy_mode: true, single_core: true, ..Self::default() }
}

fn debug_hook<'a>(&'a self, book: &'a Book, labels: &'a Labels) -> Option<impl FnMut(&Net) + 'a> {
fn debug_hook<'a>(
&'a self,
book: &'a Book,
labels: &'a Labels,
adt_encoding: AdtEncoding,
) -> Option<impl FnMut(&Net) + 'a> {
self.debug.then_some({
|net: &_| {
let net = hvmc_to_net(net);
move |net: &_| {
let mut diags = Diagnostics::default();
let res_term = net_to_term(&net, book, labels, self.linear, &mut diags);
let res_term = readback(net, book, labels, self.linear, &mut diags, adt_encoding);
eprint!("{diags}");
if self.pretty {
println!("{}\n---------------------------------------", res_term.display_pretty(0));
Expand Down
174 changes: 0 additions & 174 deletions src/net/hvmc_to_net.rs

This file was deleted.

Loading
Loading