-
Notifications
You must be signed in to change notification settings - Fork 7
/
signature.rs
345 lines (302 loc) · 10.2 KB
/
signature.rs
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//! Abstract and concrete Signature types.
#[cfg(feature = "pyo3")]
use pyo3::{pyclass, pymethods};
use delegate::delegate;
use smol_str::SmolStr;
use std::fmt::{self, Display, Write};
use std::ops::Index;
use crate::extension::ExtensionSet;
use crate::types::{Type, TypeRow};
use crate::{Direction, IncomingPort, OutgoingPort, Port, PortIndex};
#[cfg_attr(feature = "pyo3", pyclass)]
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
/// Describes the edges required to/from a node. This includes both the concept of "signature" in the spec,
/// and also the target (value) of a call (static).
pub struct FunctionType {
/// Value inputs of the function.
pub input: TypeRow,
/// Value outputs of the function.
pub output: TypeRow,
/// The extension requirements which are added by the operation
pub extension_reqs: ExtensionSet,
}
#[derive(Clone, Default, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
/// A concrete signature, which has been instantiated with a set of input extensions
pub struct Signature {
/// The underlying signature
pub signature: FunctionType,
/// The extensions which are associated with all the inputs and carried through
pub input_extensions: ExtensionSet,
}
impl FunctionType {
/// Builder method, add extension_reqs to an FunctionType
pub fn with_extension_delta(mut self, rs: &ExtensionSet) -> Self {
self.extension_reqs = self.extension_reqs.union(rs);
self
}
/// Instantiate an FunctionType, converting it to a concrete one
pub fn with_input_extensions(self, es: ExtensionSet) -> Signature {
Signature {
signature: self,
input_extensions: es,
}
}
/// Instantiate a signature with the empty set of extensions
pub fn pure(self) -> Signature {
self.with_input_extensions(ExtensionSet::new())
}
}
impl From<Signature> for FunctionType {
fn from(sig: Signature) -> Self {
sig.signature
}
}
impl Signature {
/// Calculate the extension requirements of the output wires
pub fn output_extensions(&self) -> ExtensionSet {
self.input_extensions
.clone()
.union(&self.signature.extension_reqs)
}
}
#[cfg_attr(feature = "pyo3", pymethods)]
impl FunctionType {
/// The number of wires in the signature.
#[inline(always)]
pub fn is_empty(&self) -> bool {
self.input.is_empty() && self.output.is_empty()
}
}
impl FunctionType {
/// Create a new signature with specified inputs and outputs.
pub fn new(input: impl Into<TypeRow>, output: impl Into<TypeRow>) -> Self {
Self {
input: input.into(),
output: output.into(),
extension_reqs: ExtensionSet::new(),
}
}
/// Create a new signature with the same input and output types.
pub fn new_linear(linear: impl Into<TypeRow>) -> Self {
let linear = linear.into();
Self::new(linear.clone(), linear)
}
/// Returns the type of a value [`Port`]. Returns `None` if the port is out
/// of bounds.
#[inline]
pub fn get(&self, port: impl Into<Port>) -> Option<&Type> {
let port = port.into();
match port.direction() {
Direction::Incoming => self.input.get(port),
Direction::Outgoing => self.output.get(port),
}
}
/// Returns the type of a value [`Port`]. Returns `None` if the port is out
/// of bounds.
#[inline]
pub fn get_mut(&mut self, port: Port) -> Option<&mut Type> {
match port.direction() {
Direction::Incoming => self.input.get_mut(port),
Direction::Outgoing => self.output.get_mut(port),
}
}
/// Returns the number of ports in the signature.
#[inline]
pub fn port_count(&self, dir: Direction) -> usize {
match dir {
Direction::Incoming => self.input.len(),
Direction::Outgoing => self.output.len(),
}
}
/// Returns the number of input ports in the signature.
#[inline]
pub fn input_count(&self) -> usize {
self.port_count(Direction::Incoming)
}
/// Returns the number of output ports in the signature.
#[inline]
pub fn output_count(&self) -> usize {
self.port_count(Direction::Outgoing)
}
/// Returns a slice of the types for the given direction.
#[inline]
pub fn types(&self, dir: Direction) -> &[Type] {
match dir {
Direction::Incoming => &self.input,
Direction::Outgoing => &self.output,
}
}
/// Returns a slice of the input types.
#[inline]
pub fn input_types(&self) -> &[Type] {
self.types(Direction::Incoming)
}
/// Returns a slice of the output types.
#[inline]
pub fn output_types(&self) -> &[Type] {
self.types(Direction::Outgoing)
}
#[inline]
/// Returns the input row
pub fn input(&self) -> &TypeRow {
&self.input
}
#[inline]
/// Returns the output row
pub fn output(&self) -> &TypeRow {
&self.output
}
}
impl FunctionType {
/// Returns the linear part of the signature
/// TODO: This fails when mixing different linear types.
#[inline(always)]
pub fn linear(&self) -> impl Iterator<Item = &Type> {
debug_assert_eq!(
self.input
.iter()
.filter(|t| !t.copyable())
.collect::<Vec<_>>(),
self.output
.iter()
.filter(|t| !t.copyable())
.collect::<Vec<_>>()
);
self.input.iter().filter(|t| !t.copyable())
}
/// Returns the `Port`s in the signature for a given direction.
#[inline]
pub fn ports(&self, dir: Direction) -> impl Iterator<Item = Port> {
(0..self.port_count(dir)).map(move |i| Port::new(dir, i))
}
/// Returns the incoming `Port`s in the signature.
#[inline]
pub fn input_ports(&self) -> impl Iterator<Item = IncomingPort> {
self.ports(Direction::Incoming)
.map(|p| p.as_incoming().unwrap())
}
/// Returns the outgoing `Port`s in the signature.
#[inline]
pub fn output_ports(&self) -> impl Iterator<Item = OutgoingPort> {
self.ports(Direction::Outgoing)
.map(|p| p.as_outgoing().unwrap())
}
}
impl Signature {
/// Returns a reference to the extension set for the ports of the
/// signature in a given direction
pub fn get_extension(&self, dir: &Direction) -> ExtensionSet {
match dir {
Direction::Incoming => self.input_extensions.clone(),
Direction::Outgoing => self.output_extensions(),
}
}
delegate! {
to self.signature {
/// Inputs of the function type
pub fn input(&self) -> &TypeRow;
/// Outputs of the function type
pub fn output(&self) -> &TypeRow;
}
}
}
impl Display for FunctionType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if !self.input.is_empty() {
self.input.fmt(f)?;
f.write_str(" -> ")?;
}
f.write_char('[')?;
self.extension_reqs.fmt(f)?;
f.write_char(']')?;
self.output.fmt(f)
}
}
impl Display for Signature {
delegate! {
to self.signature {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result;
}
}
}
/// Descriptive names for the ports in a [`Signature`].
///
/// This is a separate type from [`Signature`] as it is not normally used during the compiler operations.
#[cfg_attr(feature = "pyo3", pyclass)]
#[derive(Clone, Default, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct SignatureDescription {
/// Input of the function.
pub input: Vec<SmolStr>,
/// Output of the function.
pub output: Vec<SmolStr>,
}
#[cfg_attr(feature = "pyo3", pymethods)]
impl SignatureDescription {
/// The number of wires in the signature.
#[inline(always)]
pub fn is_empty(&self) -> bool {
self.input.is_empty() && self.output.is_empty()
}
}
impl SignatureDescription {
/// Create a new signature.
pub fn new(input: impl Into<Vec<SmolStr>>, output: impl Into<Vec<SmolStr>>) -> Self {
Self {
input: input.into(),
output: output.into(),
}
}
/// Create a new signature with only linear inputs and outputs.
pub fn new_linear(linear: impl Into<Vec<SmolStr>>) -> Self {
let linear = linear.into();
SignatureDescription::new(linear.clone(), linear)
}
pub(crate) fn row_zip<'a>(
type_row: &'a TypeRow,
name_row: &'a [SmolStr],
) -> impl Iterator<Item = (&'a SmolStr, &'a Type)> {
name_row
.iter()
.chain(&EmptyStringIterator)
.zip(type_row.iter())
}
/// Iterate over the input wires of the signature and their names.
///
/// Unnamed wires are given an empty string name.
///
/// TODO: Return Option<&String> instead of &String for the description.
pub fn input_zip<'a>(
&'a self,
signature: &'a Signature,
) -> impl Iterator<Item = (&SmolStr, &Type)> {
Self::row_zip(signature.input(), &self.input)
}
/// Iterate over the output wires of the signature and their names.
///
/// Unnamed wires are given an empty string name.
pub fn output_zip<'a>(
&'a self,
signature: &'a Signature,
) -> impl Iterator<Item = (&SmolStr, &Type)> {
Self::row_zip(signature.output(), &self.output)
}
}
impl Index<Port> for SignatureDescription {
type Output = SmolStr;
fn index(&self, index: Port) -> &Self::Output {
match index.direction() {
Direction::Incoming => self.input.get(index.index()).unwrap_or(EMPTY_STRING_REF),
Direction::Outgoing => self.output.get(index.index()).unwrap_or(EMPTY_STRING_REF),
}
}
}
/// An iterator that always returns the an empty string.
pub(crate) struct EmptyStringIterator;
/// A reference to an empty string. Used by [`EmptyStringIterator`].
pub(crate) const EMPTY_STRING_REF: &SmolStr = &SmolStr::new_inline("");
impl<'a> Iterator for &'a EmptyStringIterator {
type Item = &'a SmolStr;
fn next(&mut self) -> Option<Self::Item> {
Some(EMPTY_STRING_REF)
}
}