Skip to content

Commit 901cf91

Browse files
committed
replace Construct with Commit in CompiledProgram
This commit represents an important change: in the user-facing types we are no longer holding ConstructNode objects, which mean that we are not handing the user incomplete types which are tied to a specific inference context. This means that within our code, whenever we need to manipulate types, we need to create a new local type inference context which is then dropped at the end of the function. We'll need this when we update rust-simplicity to a version that has only locally-scoped context objects. The bulk of the diff is in src/named.rs, changing the populate_witness method to convert a CommitNode to a RedeemNode, which is mostly just fixing type signatures by chasing compiler errors. Sorry for the noise. I also add a new stringly-typed error, which I'm not thrilled with, but this crate is already full of them.
1 parent 91b7eb8 commit 901cf91

File tree

2 files changed

+44
-40
lines changed

2 files changed

+44
-40
lines changed

src/lib.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,16 @@ impl TemplateProgram {
8080
arguments
8181
.is_consistent(self.simfony.parameters())
8282
.map_err(|error| error.to_string())?;
83+
84+
let construct = self
85+
.simfony
86+
.compile(arguments, include_debug_symbols)
87+
.with_file(Arc::clone(&self.file))?;
88+
let commit = named::finalize_types(&construct).map_err(|e| e.to_string())?;
89+
8390
Ok(CompiledProgram {
8491
debug_symbols: self.simfony.debug_symbols(self.file.as_ref()),
85-
simplicity: self
86-
.simfony
87-
.compile(arguments, include_debug_symbols)
88-
.with_file(Arc::clone(&self.file))?,
92+
simplicity: commit,
8993
witness_types: self.simfony.witness_types().shallow_clone(),
9094
})
9195
}
@@ -94,7 +98,7 @@ impl TemplateProgram {
9498
/// A SimplicityHL program, compiled to Simplicity.
9599
#[derive(Clone, Debug)]
96100
pub struct CompiledProgram {
97-
simplicity: ProgNode,
101+
simplicity: Arc<named::CommitNode>,
98102
witness_types: WitnessTypes,
99103
debug_symbols: DebugSymbols,
100104
}
@@ -123,8 +127,6 @@ impl CompiledProgram {
123127
/// Access the Simplicity target code, without witness data.
124128
pub fn commit(&self) -> Arc<CommitNode<Elements>> {
125129
named::forget_names(&self.simplicity)
126-
.finalize_types()
127-
.expect("Compiled SimplicityHL program has type 1 -> 1")
128130
}
129131

130132
/// Satisfy the SimplicityHL program with the given `witness_values`.
@@ -152,13 +154,13 @@ impl CompiledProgram {
152154
witness_values
153155
.is_consistent(&self.witness_types)
154156
.map_err(|e| e.to_string())?;
155-
let simplicity_witness = named::populate_witnesses(&self.simplicity, witness_values);
156-
let simplicity_redeem = match env {
157-
Some(env) => simplicity_witness.finalize_pruned(env),
158-
None => simplicity_witness.finalize_unpruned(),
159-
};
157+
158+
let mut simplicity_redeem = named::populate_witnesses(&self.simplicity, witness_values)?;
159+
if let Some(env) = env {
160+
simplicity_redeem = simplicity_redeem.prune(env).map_err(|e| e.to_string())?;
161+
}
160162
Ok(SatisfiedProgram {
161-
simplicity: simplicity_redeem.map_err(|e| e.to_string())?,
163+
simplicity: simplicity_redeem,
162164
debug_symbols: self.debug_symbols.clone(),
163165
})
164166
}

src/named.rs

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -197,57 +197,59 @@ where
197197
/// types in the construct `node`. This can be done by calling [`WitnessValues::is_consistent`]
198198
/// on the original SimplicityHL program before it is compiled to Simplicity.
199199
pub fn populate_witnesses<J: Jet>(
200-
node: &Node<WithNames<node::Construct<J>>>,
200+
node: &Node<WithNames<node::Commit<J>>>,
201201
values: WitnessValues,
202-
) -> Arc<node::ConstructNode<J>> {
202+
) -> Result<Arc<node::RedeemNode<J>>, String> {
203203
struct Populator {
204204
values: WitnessValues,
205205
}
206206

207-
impl<J: Jet> Converter<WithNames<node::Construct<J>>, node::Construct<J>> for Populator {
208-
type Error = core::convert::Infallible;
207+
impl<J: Jet> Converter<WithNames<node::Commit<J>>, node::Redeem<J>> for Populator {
208+
type Error = String;
209209

210210
fn convert_witness(
211211
&mut self,
212-
_: &PostOrderIterItem<&Node<WithNames<node::Construct<J>>>>,
212+
_: &PostOrderIterItem<&Node<WithNames<node::Commit<J>>>>,
213213
witness: &WitnessName,
214-
) -> Result<Option<simplicity::Value>, Self::Error> {
215-
let maybe_value = self
216-
.values
217-
.get(witness)
218-
.map(StructuralValue::from)
219-
.map(simplicity::Value::from);
220-
Ok(maybe_value)
214+
) -> Result<simplicity::Value, Self::Error> {
215+
match self.values.get(witness) {
216+
Some(val) => Ok(simplicity::Value::from(StructuralValue::from(val))),
217+
None => Err(format!("missing witness for {witness}")),
218+
}
221219
}
222220

223221
fn convert_disconnect(
224222
&mut self,
225-
_: &PostOrderIterItem<&Node<WithNames<node::Construct<J>>>>,
226-
_: Option<&Arc<node::ConstructNode<J>>>,
223+
_: &PostOrderIterItem<&Node<WithNames<node::Commit<J>>>>,
224+
_: Option<&Arc<node::RedeemNode<J>>>,
227225
_: &NoDisconnect,
228-
) -> Result<Option<Arc<node::ConstructNode<J>>>, Self::Error> {
229-
Ok(None)
226+
) -> Result<Arc<node::RedeemNode<J>>, Self::Error> {
227+
unreachable!("SimplicityHL does not use disconnect right now")
230228
}
231229

232230
fn convert_data(
233231
&mut self,
234-
data: &PostOrderIterItem<&Node<WithNames<node::Construct<J>>>>,
235-
_: Inner<
236-
&Arc<node::ConstructNode<J>>,
232+
data: &PostOrderIterItem<&Node<WithNames<node::Commit<J>>>>,
233+
inner: Inner<
234+
&Arc<node::RedeemNode<J>>,
237235
J,
238-
&Option<Arc<node::ConstructNode<J>>>,
239-
&Option<simplicity::Value>,
236+
&Arc<node::RedeemNode<J>>,
237+
&simplicity::Value,
240238
>,
241-
) -> Result<node::ConstructData<J>, Self::Error> {
242-
Ok(data.node.cached_data().clone())
239+
) -> Result<Arc<node::RedeemData<J>>, Self::Error> {
240+
let inner = inner
241+
.map(|node| node.cached_data())
242+
.map_disconnect(|node| node.cached_data())
243+
.map_witness(simplicity::Value::shallow_clone);
244+
Ok(Arc::new(node::RedeemData::new(
245+
data.node.cached_data().arrow().shallow_clone(),
246+
inner,
247+
)))
243248
}
244249
}
245250

246251
let mut populator = Populator { values };
247-
match node.convert::<InternalSharing, _, _>(&mut populator) {
248-
Ok(ret) => ret,
249-
Err(inf) => match inf {},
250-
}
252+
node.convert::<InternalSharing, _, _>(&mut populator)
251253
}
252254

253255
// This awkward construction is required by rust-simplicity to implement WitnessConstructible

0 commit comments

Comments
 (0)