1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//! Definition of dataflow operations with no children.

use super::dataflow::DataflowOpTrait;
use super::{impl_op_name, OpTag};

use crate::extension::ExtensionSet;

use crate::{
    extension::ExtensionId,
    types::{EdgeKind, Signature, Type, TypeRow},
};

/// A no-op operation.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[non_exhaustive]
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
pub struct Noop {
    /// The type of edges connecting the Noop.
    pub ty: Type,
}

impl Noop {
    /// Create a new Noop operation.
    pub fn new(ty: Type) -> Self {
        Self { ty }
    }
}

impl Default for Noop {
    fn default() -> Self {
        Self { ty: Type::UNIT }
    }
}

/// An operation that packs all its inputs into a tuple.
#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
#[non_exhaustive]
pub struct MakeTuple {
    ///Tuple element types.
    pub tys: TypeRow,
}

impl MakeTuple {
    /// Create a new MakeTuple operation.
    pub fn new(tys: TypeRow) -> Self {
        Self { tys }
    }
}

/// An operation that unpacks a tuple into its components.
#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
#[non_exhaustive]
pub struct UnpackTuple {
    ///Tuple element types.
    pub tys: TypeRow,
}

impl UnpackTuple {
    /// Create a new UnpackTuple operation.
    pub fn new(tys: TypeRow) -> Self {
        Self { tys }
    }
}

/// An operation that creates a tagged sum value from one of its variants.
#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[non_exhaustive]
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
pub struct Tag {
    /// The variant to create.
    pub tag: usize,
    /// The variants of the sum type.
    /// TODO this allows *none* of the variants to contain row variables, but
    /// we could allow variants *other than the tagged one* to contain rowvars.
    pub variants: Vec<TypeRow>,
}

impl Tag {
    /// Create a new Tag operation.
    pub fn new(tag: usize, variants: Vec<TypeRow>) -> Self {
        Self { tag, variants }
    }
}

/// A node which adds a extension req to the types of the wires it is passed
/// It has no effect on the values passed along the edge
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
#[non_exhaustive]
pub struct Lift {
    /// The types of the edges
    pub type_row: TypeRow,
    /// The extensions which we're adding to the inputs
    pub new_extension: ExtensionId,
}

impl Lift {
    /// Create a new Lift operation.
    pub fn new(type_row: TypeRow, new_extension: ExtensionId) -> Self {
        Self {
            type_row,
            new_extension,
        }
    }
}

impl_op_name!(Noop);
impl_op_name!(MakeTuple);
impl_op_name!(UnpackTuple);
impl_op_name!(Tag);
impl_op_name!(Lift);

impl DataflowOpTrait for Noop {
    const TAG: OpTag = OpTag::Leaf;

    /// A human-readable description of the operation.
    fn description(&self) -> &str {
        "Noop gate"
    }

    /// The signature of the operation.
    fn signature(&self) -> Signature {
        Signature::new(vec![self.ty.clone()], vec![self.ty.clone()])
    }

    fn other_input(&self) -> Option<EdgeKind> {
        Some(EdgeKind::StateOrder)
    }

    fn other_output(&self) -> Option<EdgeKind> {
        Some(EdgeKind::StateOrder)
    }
}

impl DataflowOpTrait for MakeTuple {
    const TAG: OpTag = OpTag::Leaf;

    /// A human-readable description of the operation.
    fn description(&self) -> &str {
        "MakeTuple operation"
    }

    /// The signature of the operation.
    fn signature(&self) -> Signature {
        Signature::new(self.tys.clone(), vec![Type::new_tuple(self.tys.clone())])
    }

    fn other_input(&self) -> Option<EdgeKind> {
        Some(EdgeKind::StateOrder)
    }

    fn other_output(&self) -> Option<EdgeKind> {
        Some(EdgeKind::StateOrder)
    }
}

impl DataflowOpTrait for UnpackTuple {
    const TAG: OpTag = OpTag::Leaf;

    /// A human-readable description of the operation.
    fn description(&self) -> &str {
        "UnpackTuple operation"
    }

    /// The signature of the operation.
    fn signature(&self) -> Signature {
        Signature::new(vec![Type::new_tuple(self.tys.clone())], self.tys.clone())
    }

    fn other_input(&self) -> Option<EdgeKind> {
        Some(EdgeKind::StateOrder)
    }

    fn other_output(&self) -> Option<EdgeKind> {
        Some(EdgeKind::StateOrder)
    }
}

impl DataflowOpTrait for Tag {
    const TAG: OpTag = OpTag::Leaf;

    /// A human-readable description of the operation.
    fn description(&self) -> &str {
        "Tag Sum operation"
    }

    /// The signature of the operation.
    fn signature(&self) -> Signature {
        Signature::new(
            self.variants
                .get(self.tag)
                .expect("Not a valid tag")
                .clone(),
            vec![Type::new_sum(self.variants.clone())],
        )
    }

    fn other_input(&self) -> Option<EdgeKind> {
        Some(EdgeKind::StateOrder)
    }

    fn other_output(&self) -> Option<EdgeKind> {
        Some(EdgeKind::StateOrder)
    }
}

impl DataflowOpTrait for Lift {
    const TAG: OpTag = OpTag::Leaf;

    /// A human-readable description of the operation.
    fn description(&self) -> &str {
        "Add a extension requirement to an edge"
    }

    /// The signature of the operation.
    fn signature(&self) -> Signature {
        Signature::new(self.type_row.clone(), self.type_row.clone())
            .with_extension_delta(ExtensionSet::singleton(&self.new_extension))
    }

    fn other_input(&self) -> Option<EdgeKind> {
        Some(EdgeKind::StateOrder)
    }

    fn other_output(&self) -> Option<EdgeKind> {
        Some(EdgeKind::StateOrder)
    }
}