phase_rs/normal_syntax/
mod.rs

1//! Normal-form syntax definitions
2//!
3//! Syntax for evaluated terms.
4//! This is assumed to be typechecked/well-formed.
5
6pub mod term;
7pub use term::TermN;
8
9pub mod pattern;
10pub use pattern::PatternN;
11
12use crate::{
13    normal_syntax::term::AtomN,
14    typed_syntax::{PatternType, TermType},
15};
16
17/// Trait for objects that can built with compositions, tensors, or from an `AtomN`.
18pub trait Buildable {
19    /// Build a composition object from a sequence of subobjects and a given type.
20    /// Subobjects should be given in diagrammatic order, not function composition order.
21    fn comp(iter: impl DoubleEndedIterator<Item = Self>, ty: &TermType) -> Self;
22    /// Build a tensor product from a sequence of subobjects.
23    fn tensor(iter: impl Iterator<Item = Self>) -> Self;
24    /// Build an object from an atom.
25    fn atom(atom: AtomN) -> Self;
26}
27
28impl Buildable for TermN {
29    fn comp(iter: impl DoubleEndedIterator<Item = Self>, ty: &TermType) -> Self {
30        TermN::Comp(iter.collect(), *ty)
31    }
32
33    fn tensor(iter: impl Iterator<Item = Self>) -> Self {
34        TermN::Tensor(iter.collect())
35    }
36
37    fn atom(atom: AtomN) -> Self {
38        TermN::Atom(atom)
39    }
40}
41
42impl Buildable for PatternN {
43    fn comp(iter: impl DoubleEndedIterator<Item = Self>, ty: &TermType) -> Self {
44        PatternN::Comp(iter.rev().collect(), PatternType(ty.0, ty.0))
45    }
46
47    fn tensor(iter: impl Iterator<Item = Self>) -> Self {
48        PatternN::Tensor(iter.collect())
49    }
50
51    fn atom(atom: AtomN) -> Self {
52        PatternN::Unitary(Box::new(atom))
53    }
54}