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

Some type inference cleanups #227

Merged
merged 4 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
34 changes: 18 additions & 16 deletions src/human_encoding/named_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ impl<J: Jet> NamedCommitNode<J> {
witness: &HashMap<Arc<str>, Arc<Value>>,
disconnect: &HashMap<Arc<str>, Arc<NamedCommitNode<J>>>,
) -> Arc<WitnessNode<J>> {
struct Populator<'a, J: Jet>(
&'a HashMap<Arc<str>, Arc<Value>>,
&'a HashMap<Arc<str>, Arc<NamedCommitNode<J>>>,
PhantomData<J>,
);
struct Populator<'a, J: Jet> {
witness_map: &'a HashMap<Arc<str>, Arc<Value>>,
disconnect_map: &'a HashMap<Arc<str>, Arc<NamedCommitNode<J>>>,
phantom: PhantomData<J>,
}

impl<'a, J: Jet> Converter<Named<Commit<J>>, Witness<J>> for Populator<'a, J> {
type Error = ();
Expand All @@ -133,7 +133,7 @@ impl<J: Jet> NamedCommitNode<J> {
// Which nodes are pruned is not known when this code is executed.
// If an unpruned node is unpopulated, then there will be an error
// during the finalization.
Ok(self.0.get(name).cloned())
Ok(self.witness_map.get(name).cloned())
}

fn convert_disconnect(
Expand All @@ -152,7 +152,7 @@ impl<J: Jet> NamedCommitNode<J> {
// We keep the missing disconnected branches empty.
// Like witness nodes (see above), disconnect nodes may be pruned later.
// The finalization will detect missing branches and throw an error.
let maybe_commit = self.1.get(hole_name);
let maybe_commit = self.disconnect_map.get(hole_name);
// FIXME: Recursive call of to_witness_node
// We cannot introduce a stack
// because we are implementing methods of the trait Converter
Expand All @@ -161,7 +161,9 @@ impl<J: Jet> NamedCommitNode<J> {
// OTOH, if a user writes a program with so many disconnected expressions
// that there is a stack overflow, it's his own fault :)
// This would fail in a fuzz test.
let witness = maybe_commit.map(|commit| commit.to_witness_node(self.0, self.1));
let witness = maybe_commit.map(|commit| {
commit.to_witness_node(self.witness_map, self.disconnect_map)
});
Ok(witness)
}
}
Expand All @@ -183,8 +185,12 @@ impl<J: Jet> NamedCommitNode<J> {
}
}

self.convert::<InternalSharing, _, _>(&mut Populator(witness, disconnect, PhantomData))
.unwrap()
self.convert::<InternalSharing, _, _>(&mut Populator {
witness_map: witness,
disconnect_map: disconnect,
phantom: PhantomData,
})
.unwrap()
}

/// Encode a Simplicity expression to bits without any witness data
Expand Down Expand Up @@ -407,17 +413,13 @@ impl<J: Jet> NamedConstructNode<J> {
if self.for_main {
// For `main`, only apply type ascriptions *after* inference has completely
// determined the type.
let source_bound =
types::Bound::Complete(Arc::clone(&commit_data.arrow().source));
let source_ty = types::Type::from(source_bound);
let source_ty = types::Type::complete(Arc::clone(&commit_data.arrow().source));
for ty in data.node.cached_data().user_source_types.as_ref() {
if let Err(e) = source_ty.unify(ty, "binding source type annotation") {
self.errors.add(data.node.position(), e);
}
}
let target_bound =
types::Bound::Complete(Arc::clone(&commit_data.arrow().target));
let target_ty = types::Type::from(target_bound);
let target_ty = types::Type::complete(Arc::clone(&commit_data.arrow().target));
for ty in data.node.cached_data().user_target_types.as_ref() {
if let Err(e) = target_ty.unify(ty, "binding target type annotation") {
self.errors.add(data.node.position(), e);
Expand Down
4 changes: 1 addition & 3 deletions src/human_encoding/parse/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,9 +633,7 @@ fn grammar<J: Jet + 'static>() -> Grammar<Ast<J>> {
Error::BadWordLength { bit_length },
));
}
let ty = types::Type::two_two_n(bit_length.trailing_zeros() as usize)
.final_data()
.unwrap();
let ty = types::Final::two_two_n(bit_length.trailing_zeros() as usize);
// unwrap ok here since literally every sequence of bits is a valid
// value for the given type
let value = iter.read_value(&ty).unwrap();
Expand Down
Loading