Skip to content

Commit 96e7df2

Browse files
committed
move ProgNode into compile.rs as a private type
Now that no public types contain ConstructNodes, we don't need a type alias for a ConstructNode to be exported. Move this into compile.rs, make it private, and replace a couple isolated uses in pattern.rs with generic ones.
1 parent 090370c commit 96e7df2

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

src/compile.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ use crate::str::WitnessName;
2121
use crate::types::{StructuralType, TypeDeconstructible};
2222
use crate::value::StructuralValue;
2323
use crate::witness::Arguments;
24-
use crate::{ProgNode, Value};
24+
use crate::Value;
25+
26+
type ProgNode = Arc<named::ConstructNode<Elements>>;
2527

2628
/// Each SimplicityHL expression expects an _input value_.
2729
/// A SimplicityHL expression is translated into a Simplicity expression

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//! Library for parsing and compiling SimplicityHL
22
3-
pub type ProgNode = Arc<named::ConstructNode<Elements>>;
4-
53
pub mod array;
64
pub mod ast;
75
pub mod compile;

src/pattern.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ use miniscript::iter::{Tree, TreeLike};
77

88
use crate::array::BTreeSlice;
99
use crate::error::Error;
10-
use crate::named::{PairBuilder, SelectorBuilder};
10+
use crate::named::{CoreExt, PairBuilder, SelectorBuilder};
1111
use crate::str::Identifier;
1212
use crate::types::{ResolvedType, TypeInner};
13-
use crate::ProgNode;
1413

1514
/// Pattern for binding values to variables.
1615
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
@@ -195,14 +194,15 @@ impl BasePattern {
195194

196195
/// Check if the `identifier` is contained inside the pattern.
197196
pub fn contains(&self, identifier: &Identifier) -> bool {
198-
self.get(identifier).is_some()
197+
self.pre_order_iter()
198+
.any(|node| matches!(node, BasePattern::Identifier(id) if id == identifier))
199199
}
200200

201201
/// Compute a Simplicity expression that returns the value of the given `identifier`.
202202
/// The expression takes as input a value that matches the `self` pattern.
203203
///
204204
/// The expression is a sequence of `take` and `drop` followed by `iden`.
205-
fn get(&self, identifier: &Identifier) -> Option<SelectorBuilder<ProgNode>> {
205+
fn get<P: CoreExt>(&self, identifier: &Identifier) -> Option<SelectorBuilder<P>> {
206206
let mut selector = SelectorBuilder::default();
207207

208208
for data in self.verbose_pre_order_iter() {
@@ -302,11 +302,11 @@ impl BasePattern {
302302
/// This means there are infinitely many translating expressions from `self` to `to`.
303303
/// For instance, `iden`, `iden & iden`, `(iden & iden) & iden`, and so on.
304304
/// We enforce a unique translation by banning ignore from the `to` pattern.
305-
pub fn translate(
305+
pub fn translate<P: CoreExt>(
306306
&self,
307307
ctx: &simplicity::types::Context,
308308
to: &Self,
309-
) -> Option<PairBuilder<ProgNode>> {
309+
) -> Option<PairBuilder<P>> {
310310
#[derive(Debug, Clone)]
311311
enum Task<'a> {
312312
Translate(&'a BasePattern, &'a BasePattern),
@@ -439,6 +439,8 @@ impl From<&Pattern> for BasePattern {
439439
#[cfg(test)]
440440
mod tests {
441441
use super::*;
442+
use crate::named;
443+
use simplicity::jet::Elements;
442444

443445
#[test]
444446
fn translate_pattern() {
@@ -462,7 +464,9 @@ mod tests {
462464

463465
for (target, expected_expr) in target_expr {
464466
let ctx = simplicity::types::Context::new();
465-
let expr = env.translate(&ctx, &target).unwrap();
467+
let expr = env
468+
.translate::<Arc<named::ConstructNode<Elements>>>(&ctx, &target)
469+
.unwrap();
466470
assert_eq!(expected_expr, expr.as_ref().display_expr().to_string());
467471
}
468472
}

0 commit comments

Comments
 (0)