-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathextension.rs
424 lines (368 loc) · 14.3 KB
/
extension.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! Extensions
//!
//! TODO: YAML declaration and parsing. This should be similar to a plugin
//! system (outside the `types` module), which also parses nested [`OpDef`]s.
use std::collections::hash_map::Entry;
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::fmt::{Debug, Display, Formatter};
use std::sync::Arc;
use smol_str::SmolStr;
use thiserror::Error;
use crate::hugr::IdentList;
use crate::ops;
use crate::ops::custom::{ExtensionOp, OpaqueOp};
use crate::types::type_param::{check_type_args, TypeArgError};
use crate::types::type_param::{TypeArg, TypeParam};
use crate::types::{check_typevar_decl, CustomType, PolyFuncType, Substitution, TypeBound};
mod infer;
pub use infer::{infer_extensions, ExtensionSolution, InferExtensionError};
mod op_def;
pub use op_def::{CustomSignatureFunc, OpDef};
mod type_def;
pub use type_def::{TypeDef, TypeDefBound};
pub mod prelude;
pub mod validate;
pub use prelude::{PRELUDE, PRELUDE_REGISTRY};
/// Extension Registries store extensions to be looked up e.g. during validation.
#[derive(Clone, Debug)]
pub struct ExtensionRegistry(BTreeMap<ExtensionId, Extension>);
impl ExtensionRegistry {
/// Makes a new (empty) registry.
pub const fn new() -> Self {
Self(BTreeMap::new())
}
/// Gets the Extension with the given name
pub fn get(&self, name: &str) -> Option<&Extension> {
self.0.get(name)
}
}
/// An Extension Registry containing no extensions.
pub const EMPTY_REG: ExtensionRegistry = ExtensionRegistry::new();
impl<T: IntoIterator<Item = Extension>> From<T> for ExtensionRegistry {
fn from(value: T) -> Self {
let mut reg = Self::new();
for ext in value.into_iter() {
let prev = reg.0.insert(ext.name.clone(), ext);
if let Some(prev) = prev {
panic!("Multiple extensions with same name: {}", prev.name)
};
}
reg
}
}
/// An error that can occur in computing the signature of a node.
/// TODO: decide on failure modes
#[derive(Debug, Clone, Error, PartialEq, Eq)]
#[allow(missing_docs)]
pub enum SignatureError {
/// Name mismatch
#[error("Definition name ({0}) and instantiation name ({1}) do not match.")]
NameMismatch(SmolStr, SmolStr),
/// Extension mismatch
#[error("Definition extension ({0:?}) and instantiation extension ({1:?}) do not match.")]
ExtensionMismatch(ExtensionId, ExtensionId),
/// When the type arguments of the node did not match the params declared by the OpDef
#[error("Type arguments of node did not match params declared by definition: {0}")]
TypeArgMismatch(#[from] TypeArgError),
/// Invalid type arguments
#[error("Invalid type arguments for operation")]
InvalidTypeArgs,
/// The Extension Registry did not contain an Extension referenced by the Signature
#[error("Extension '{0}' not found")]
ExtensionNotFound(ExtensionId),
/// The Extension was found in the registry, but did not contain the Type(Def) referenced in the Signature
#[error("Extension '{exn}' did not contain expected TypeDef '{typ}'")]
ExtensionTypeNotFound { exn: ExtensionId, typ: SmolStr },
/// The bound recorded for a CustomType doesn't match what the TypeDef would compute
#[error("Bound on CustomType ({actual}) did not match TypeDef ({expected})")]
WrongBound {
actual: TypeBound,
expected: TypeBound,
},
/// A Type Variable's cache of its declared kind is incorrect
#[error("Type Variable claims to be {cached:?} but actual declaration {actual:?}")]
TypeVarDoesNotMatchDeclaration {
actual: TypeParam,
cached: TypeParam,
},
/// A type variable that was used has not been declared
#[error("Type variable {idx} was not declared ({num_decls} in scope)")]
FreeTypeVar { idx: usize, num_decls: usize },
/// The type stored in a [LeafOp::TypeApply] is not what we compute from the
/// [ExtensionRegistry].
///
/// [LeafOp::TypeApply]: crate::ops::LeafOp::TypeApply
#[error("Incorrect result of type application - cached {cached} but expected {expected}")]
TypeApplyIncorrectCache {
cached: PolyFuncType,
expected: PolyFuncType,
},
}
/// Concrete instantiations of types and operations defined in extensions.
trait CustomConcrete {
fn def_name(&self) -> &SmolStr;
fn type_args(&self) -> &[TypeArg];
fn parent_extension(&self) -> &ExtensionId;
}
impl CustomConcrete for OpaqueOp {
fn def_name(&self) -> &SmolStr {
self.name()
}
fn type_args(&self) -> &[TypeArg] {
self.args()
}
fn parent_extension(&self) -> &ExtensionId {
self.extension()
}
}
impl CustomConcrete for CustomType {
fn def_name(&self) -> &SmolStr {
self.name()
}
fn type_args(&self) -> &[TypeArg] {
self.args()
}
fn parent_extension(&self) -> &ExtensionId {
self.extension()
}
}
/// Type-parametrised functionality shared between [`TypeDef`] and [`OpDef`].
trait TypeParametrised {
/// The concrete object built by binding type arguments to parameters
type Concrete: CustomConcrete;
/// The extension-unique name.
fn name(&self) -> &SmolStr;
/// Type parameters.
fn params(&self) -> &[TypeParam];
/// The parent extension.
fn extension(&self) -> &ExtensionId;
/// Check provided type arguments are valid against parameters.
fn check_args_impl(&self, args: &[TypeArg]) -> Result<(), SignatureError> {
check_type_args(args, self.params()).map_err(SignatureError::TypeArgMismatch)
}
}
/// A constant value provided by a extension.
/// Must be an instance of a type available to the extension.
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct ExtensionValue {
extension: ExtensionId,
name: SmolStr,
typed_value: ops::Const,
}
impl ExtensionValue {
/// Returns a reference to the typed value of this [`ExtensionValue`].
pub fn typed_value(&self) -> &ops::Const {
&self.typed_value
}
/// Returns a reference to the name of this [`ExtensionValue`].
pub fn name(&self) -> &str {
self.name.as_ref()
}
/// Returns a reference to the extension this [`ExtensionValue`] belongs to.
pub fn extension(&self) -> &ExtensionId {
&self.extension
}
}
/// A unique identifier for a extension.
///
/// The actual [`Extension`] is stored externally.
pub type ExtensionId = IdentList;
/// A extension is a set of capabilities required to execute a graph.
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct Extension {
/// Unique identifier for the extension.
pub name: ExtensionId,
/// Other extensions defining types used by this extension.
/// That is, an upper-bound on the types that can be returned by
/// computing the signature of any operation in this extension,
/// for any possible [TypeArg].
pub extension_reqs: ExtensionSet,
/// Types defined by this extension.
types: HashMap<SmolStr, TypeDef>,
/// Static values defined by this extension.
values: HashMap<SmolStr, ExtensionValue>,
/// Operation declarations with serializable definitions.
// Note: serde will serialize this because we configure with `features=["rc"]`.
// That will clone anything that has multiple references, but each
// OpDef should appear exactly once in this map (keyed by its name),
// and the other references to the OpDef are from ExternalOp's in the Hugr
// (which are serialized as OpaqueOp's i.e. Strings).
operations: HashMap<SmolStr, Arc<op_def::OpDef>>,
}
impl Extension {
/// Creates a new extension with the given name.
pub fn new(name: ExtensionId) -> Self {
Self::new_with_reqs(name, Default::default())
}
/// Creates a new extension with the given name and requirements.
pub fn new_with_reqs(name: ExtensionId, extension_reqs: ExtensionSet) -> Self {
Self {
name,
extension_reqs,
types: Default::default(),
values: Default::default(),
operations: Default::default(),
}
}
/// Allows read-only access to the operations in this Extension
pub fn get_op(&self, op_name: &str) -> Option<&Arc<op_def::OpDef>> {
self.operations.get(op_name)
}
/// Allows read-only access to the types in this Extension
pub fn get_type(&self, type_name: &str) -> Option<&type_def::TypeDef> {
self.types.get(type_name)
}
/// Allows read-only access to the values in this Extension
pub fn get_value(&self, type_name: &str) -> Option<&ExtensionValue> {
self.values.get(type_name)
}
/// Returns the name of the extension.
pub fn name(&self) -> &ExtensionId {
&self.name
}
/// Iterator over the operations of this [`Extension`].
pub fn operations(&self) -> impl Iterator<Item = (&SmolStr, &Arc<OpDef>)> {
self.operations.iter()
}
/// Iterator over the types of this [`Extension`].
pub fn types(&self) -> impl Iterator<Item = (&SmolStr, &TypeDef)> {
self.types.iter()
}
/// Add a named static value to the extension.
pub fn add_value(
&mut self,
name: impl Into<SmolStr>,
typed_value: ops::Const,
) -> Result<&mut ExtensionValue, ExtensionBuildError> {
let extension_value = ExtensionValue {
extension: self.name.clone(),
name: name.into(),
typed_value,
};
match self.values.entry(extension_value.name.clone()) {
Entry::Occupied(_) => Err(ExtensionBuildError::OpDefExists(extension_value.name)),
Entry::Vacant(ve) => Ok(ve.insert(extension_value)),
}
}
/// Instantiate an [`ExtensionOp`] which references an [`OpDef`] in this extension.
pub fn instantiate_extension_op(
&self,
op_name: &str,
args: impl Into<Vec<TypeArg>>,
ext_reg: &ExtensionRegistry,
) -> Result<ExtensionOp, SignatureError> {
let op_def = self.get_op(op_name).expect("Op not found.");
ExtensionOp::new(op_def.clone(), args, ext_reg)
}
}
impl PartialEq for Extension {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
}
}
/// An error that can occur in computing the signature of a node.
/// TODO: decide on failure modes
#[derive(Debug, Clone, Error, PartialEq, Eq)]
pub enum ExtensionBuildError {
/// Existing [`OpDef`]
#[error("Extension already has an op called {0}.")]
OpDefExists(SmolStr),
/// Existing [`TypeDef`]
#[error("Extension already has an type called {0}.")]
TypeDefExists(SmolStr),
}
/// A set of extensions identified by their unique [`ExtensionId`].
#[derive(Clone, Debug, Default, Hash, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ExtensionSet(BTreeSet<ExtensionId>);
impl ExtensionSet {
/// Creates a new empty extension set.
pub const fn new() -> Self {
Self(BTreeSet::new())
}
/// Adds a extension to the set.
pub fn insert(&mut self, extension: &ExtensionId) {
self.0.insert(extension.clone());
}
/// Adds a type var (which must have been declared as a [TypeParam::Extensions]) to this set
pub fn insert_type_var(&mut self, idx: usize) {
// Represent type vars as string representation of DeBruijn index.
// This is not a legal IdentList or ExtensionId so should not conflict.
self.0
.insert(ExtensionId::new_unchecked(idx.to_string().as_str()));
}
/// Returns `true` if the set contains the given extension.
pub fn contains(&self, extension: &ExtensionId) -> bool {
self.0.contains(extension)
}
/// Returns `true` if the set is a subset of `other`.
pub fn is_subset(&self, other: &Self) -> bool {
self.0.is_subset(&other.0)
}
/// Returns `true` if the set is a superset of `other`.
pub fn is_superset(&self, other: &Self) -> bool {
self.0.is_superset(&other.0)
}
/// Create a extension set with a single element.
pub fn singleton(extension: &ExtensionId) -> Self {
let mut set = Self::new();
set.insert(extension);
set
}
/// An ExtensionSet containing a single type variable
/// (which must have been declared as a [TypeParam::Extensions])
pub fn type_var(idx: usize) -> Self {
let mut set = Self::new();
set.insert_type_var(idx);
set
}
/// Returns the union of two extension sets.
pub fn union(mut self, other: &Self) -> Self {
self.0.extend(other.0.iter().cloned());
self
}
/// The things in other which are in not in self
pub fn missing_from(&self, other: &Self) -> Self {
ExtensionSet::from_iter(other.0.difference(&self.0).cloned())
}
/// Iterate over the contained ExtensionIds
pub fn iter(&self) -> impl Iterator<Item = &ExtensionId> {
self.0.iter()
}
/// True if this set contains no [ExtensionId]s
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub(crate) fn validate(&self, params: &[TypeParam]) -> Result<(), SignatureError> {
self.iter()
.filter_map(as_typevar)
.try_for_each(|var_idx| check_typevar_decl(params, var_idx, &TypeParam::Extensions))
}
pub(crate) fn substitute(&self, t: &impl Substitution) -> Self {
Self::from_iter(self.0.iter().flat_map(|e| match as_typevar(e) {
None => vec![e.clone()],
Some(i) => match t.apply_var(i, &TypeParam::Extensions) {
TypeArg::Extensions{es} => es.iter().cloned().collect::<Vec<_>>(),
_ => panic!("value for type var was not extension set - type scheme should be validated first"),
},
}))
}
}
fn as_typevar(e: &ExtensionId) -> Option<usize> {
// Type variables are represented as radix-10 numbers, which are illegal
// as standard ExtensionIds. Hence if an ExtensionId starts with a digit,
// we assume it must be a type variable, and fail fast if it isn't.
match e.chars().next() {
Some(c) if c.is_ascii_digit() => Some(str::parse(e).unwrap()),
_ => None,
}
}
impl Display for ExtensionSet {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
f.debug_list().entries(self.0.iter()).finish()
}
}
impl FromIterator<ExtensionId> for ExtensionSet {
fn from_iter<I: IntoIterator<Item = ExtensionId>>(iter: I) -> Self {
Self(BTreeSet::from_iter(iter))
}
}