Skip to content

Commit 6e30622

Browse files
committed
Merge #143: refactors to prepare for the new rust-simplicity version
760c51c move ProgNode into compile.rs as a private type (Andrew Poelstra) 142e63c move builtins from crate root to private module in compile (Andrew Poelstra) cbf7ce6 rename src/compile.rs to src/compile/mod.rs (Andrew Poelstra) 7f1d12a move type checking from lib.rs into compile.rs (Andrew Poelstra) 5fce3d4 replace Construct with Commit in CompiledProgram (Andrew Poelstra) 6d864bb named: add finalize_types function (Andrew Poelstra) 7150f0e named: replace ad-hoc ConstructNode/CommitNode/WitnessNode with generic ones (Andrew Poelstra) 60f65b4 remove Default impl from CompiledProgram (Andrew Poelstra) Pull request description: Since BlockstreamResearch/rust-simplicity#305 we are not allowed to have type inference contexts (and therefore `ConstructNode`s) that escape the lexical scope that defines them. We currently use `ConstructNode` inside `ProgramNode`. This PR changes this to `CommitNode`, which is more correct, as well as being necessary to avoid exposing bad `GhostCell` ergonomics to users of this library. ACKs for top commit: delta1: ACK 760c51c Tree-SHA512: 15d99c72c262ec88e873fb4e3cc6025c1ecf4ec848b22f4b45fb8e5f40c98d5aea3421f1fbd0f3d68f83fc04797495e1665d71d4d1b5b11b4b94c5c088df451b
2 parents 6650729 + 760c51c commit 6e30622

File tree

5 files changed

+219
-207
lines changed

5 files changed

+219
-207
lines changed

src/builtins.rs renamed to src/compile/builtins.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use std::num::NonZeroUsize;
22

33
use simplicity::node::CoreConstructible;
44

5-
use crate::{named::CoreExt, ProgNode};
5+
use super::ProgNode;
6+
use crate::named::CoreExt;
67

78
/// Fold an array of size `size` elements using function `f`.
89
///

src/compile.rs renamed to src/compile/mod.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
//! Compile the parsed ast into a simplicity program
22
3+
mod builtins;
4+
35
use std::sync::Arc;
46

57
use either::Either;
68
use simplicity::jet::Elements;
79
use simplicity::node::{CoreConstructible as _, JetConstructible as _};
810
use simplicity::{Cmr, FailEntropy};
911

12+
use self::builtins::array_fold;
1013
use crate::array::{BTreeSlice, Partition};
1114
use crate::ast::{
1215
Call, CallName, Expression, ExpressionInner, Match, Program, SingleExpression,
1316
SingleExpressionInner, Statement,
1417
};
15-
use crate::builtins::array_fold;
1618
use crate::debug::CallTracker;
1719
use crate::error::{Error, RichError, Span, WithSpan};
18-
use crate::named::{CoreExt, PairBuilder};
20+
use crate::named::{self, CoreExt, PairBuilder};
1921
use crate::num::{NonZeroPow2Usize, Pow2Usize};
2022
use crate::pattern::{BasePattern, Pattern};
2123
use crate::str::WitnessName;
2224
use crate::types::{StructuralType, TypeDeconstructible};
2325
use crate::value::StructuralValue;
2426
use crate::witness::Arguments;
25-
use crate::{ProgNode, Value};
27+
use crate::Value;
28+
29+
type ProgNode = Arc<named::ConstructNode<Elements>>;
2630

2731
/// Each SimplicityHL expression expects an _input value_.
2832
/// A SimplicityHL expression is translated into a Simplicity expression
@@ -258,13 +262,18 @@ impl Program {
258262
&self,
259263
arguments: Arguments,
260264
include_debug_symbols: bool,
261-
) -> Result<ProgNode, RichError> {
265+
) -> Result<Arc<named::CommitNode<Elements>>, RichError> {
262266
let mut scope = Scope::new(
263267
Arc::clone(self.call_tracker()),
264268
arguments,
265269
include_debug_symbols,
266270
);
267-
self.main().compile(&mut scope).map(PairBuilder::build)
271+
272+
let main = self.main();
273+
let construct = main.compile(&mut scope).map(PairBuilder::build)?;
274+
// SimplicityHL types should be correct by construction. If not, assign the
275+
// whole main function as the span for them, which is as sensible as anything.
276+
named::finalize_types(&construct).with_span(main)
268277
}
269278
}
270279

src/lib.rs

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
//! Library for parsing and compiling SimplicityHL
22
3-
pub type ProgNode = Arc<named::ConstructNode>;
4-
53
pub mod array;
64
pub mod ast;
7-
pub mod builtins;
85
pub mod compile;
96
pub mod debug;
107
pub mod dummy_env;
@@ -81,12 +78,15 @@ impl TemplateProgram {
8178
arguments
8279
.is_consistent(self.simfony.parameters())
8380
.map_err(|error| error.to_string())?;
81+
82+
let commit = self
83+
.simfony
84+
.compile(arguments, include_debug_symbols)
85+
.with_file(Arc::clone(&self.file))?;
86+
8487
Ok(CompiledProgram {
8588
debug_symbols: self.simfony.debug_symbols(self.file.as_ref()),
86-
simplicity: self
87-
.simfony
88-
.compile(arguments, include_debug_symbols)
89-
.with_file(Arc::clone(&self.file))?,
89+
simplicity: commit,
9090
witness_types: self.simfony.witness_types().shallow_clone(),
9191
})
9292
}
@@ -95,22 +95,11 @@ impl TemplateProgram {
9595
/// A SimplicityHL program, compiled to Simplicity.
9696
#[derive(Clone, Debug)]
9797
pub struct CompiledProgram {
98-
simplicity: ProgNode,
98+
simplicity: Arc<named::CommitNode<Elements>>,
9999
witness_types: WitnessTypes,
100100
debug_symbols: DebugSymbols,
101101
}
102102

103-
impl Default for CompiledProgram {
104-
fn default() -> Self {
105-
use simplicity::node::CoreConstructible;
106-
Self {
107-
simplicity: ProgNode::unit(&simplicity::types::Context::new()),
108-
witness_types: WitnessTypes::default(),
109-
debug_symbols: DebugSymbols::default(),
110-
}
111-
}
112-
}
113-
114103
impl CompiledProgram {
115104
/// Parse and compile a SimplicityHL program from the given string.
116105
///
@@ -134,8 +123,7 @@ impl CompiledProgram {
134123

135124
/// Access the Simplicity target code, without witness data.
136125
pub fn commit(&self) -> Arc<CommitNode<Elements>> {
137-
named::to_commit_node(&self.simplicity)
138-
.expect("Compiled SimplicityHL program has type 1 -> 1")
126+
named::forget_names(&self.simplicity)
139127
}
140128

141129
/// Satisfy the SimplicityHL program with the given `witness_values`.
@@ -163,13 +151,13 @@ impl CompiledProgram {
163151
witness_values
164152
.is_consistent(&self.witness_types)
165153
.map_err(|e| e.to_string())?;
166-
let simplicity_witness = named::to_witness_node(&self.simplicity, witness_values);
167-
let simplicity_redeem = match env {
168-
Some(env) => simplicity_witness.finalize_pruned(env),
169-
None => simplicity_witness.finalize_unpruned(),
170-
};
154+
155+
let mut simplicity_redeem = named::populate_witnesses(&self.simplicity, witness_values)?;
156+
if let Some(env) = env {
157+
simplicity_redeem = simplicity_redeem.prune(env).map_err(|e| e.to_string())?;
158+
}
171159
Ok(SatisfiedProgram {
172-
simplicity: simplicity_redeem.map_err(|e| e.to_string())?,
160+
simplicity: simplicity_redeem,
173161
debug_symbols: self.debug_symbols.clone(),
174162
})
175163
}

0 commit comments

Comments
 (0)