hugr-model 0.28.1

Data model for Quantinuum's HUGR intermediate representation
Documentation
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
use std::{borrow::Cow, collections::BTreeSet, hash::BuildHasherDefault};

use indexmap::IndexMap;
use rustc_hash::FxHasher;
use thiserror::Error;

use crate::v0::table::{NodeId, RegionId};

type FxIndexMap<K, V> = IndexMap<K, V, BuildHasherDefault<FxHasher>>;

/// Borrowed symbol name used as a lookup key in scoped symbol tables.
pub type SymbolName<'a> = &'a str;

/// Symbol binding table that keeps track of symbol resolution and scoping.
///
/// Nodes may introduce a symbol so that other parts of the IR can refer to the
/// node. Symbols have an associated name and are scoped via regions. A symbol
/// can shadow another symbol with the same name from an outer region, but
/// within any single region each name/version pair must be unique. Versioned
/// references resolve exactly. Unversioned references prefer an unversioned
/// binding, then the latest visible versioned binding.
///
/// When a symbol is referred to directly by the id of the node, the symbol must
/// be in scope at the point of reference as if the reference was by name. This
/// guarantees that transformations between directly indexed and named formats
/// are always valid.
///
/// # Examples
///
/// ```
/// # pub use hugr_model::v0::table::{NodeId, RegionId};
/// # pub use hugr_model::v0::scope::SymbolTable;
/// let mut symbols = SymbolTable::new();
/// symbols.enter(RegionId(0));
/// symbols.insert("foo", None, NodeId(0)).unwrap();
/// assert_eq!(symbols.resolve("foo", None).unwrap(), NodeId(0));
/// symbols.enter(RegionId(1));
/// assert_eq!(symbols.resolve("foo", None).unwrap(), NodeId(0));
/// symbols.insert("foo", None, NodeId(1)).unwrap();
/// assert_eq!(symbols.resolve("foo", None).unwrap(), NodeId(1));
/// assert!(!symbols.is_visible(NodeId(0)));
/// symbols.exit();
/// assert_eq!(symbols.resolve("foo", None).unwrap(), NodeId(0));
/// assert!(symbols.is_visible(NodeId(0)));
/// assert!(!symbols.is_visible(NodeId(1)));
/// ```
#[derive(Debug, Clone, Default)]
pub struct SymbolTable<'a> {
    symbols: FxIndexMap<SymbolKey<'a>, BindingIndex>,
    bindings: FxIndexMap<NodeId, Binding>,
    scopes: FxIndexMap<RegionId, Scope>,
    latest_versioned: FxIndexMap<SymbolName<'a>, BTreeSet<semver::Version>>,
}

impl<'a> SymbolTable<'a> {
    /// Create a new symbol table.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Enter a new scope for the given region.
    pub fn enter(&mut self, region: RegionId) {
        self.scopes.insert(
            region,
            Scope {
                binding_stack: self.bindings.len(),
            },
        );
    }

    /// Exit a previously entered scope.
    ///
    /// # Panics
    ///
    /// Panics if there are no remaining open scopes.
    pub fn exit(&mut self) {
        let (_, scope) = self.scopes.pop().unwrap();

        for _ in scope.binding_stack..self.bindings.len() {
            let (_, binding) = self.bindings.pop().unwrap();
            let key = self
                .symbols
                .get_index(binding.symbol_index)
                .expect("Symbol must be present in version table")
                .0
                .clone();

            if let Some(shadows) = binding.shadows {
                self.symbols[binding.symbol_index] = shadows;
            } else {
                let last = self.symbols.pop();
                debug_assert_eq!(last.unwrap().1, self.bindings.len());
                self.remove_latest_versioned(&key);
            }
        }
    }

    /// Insert a new symbol into the current scope.
    ///
    /// # Errors
    ///
    /// Returns an error if the symbol is already defined in the current scope.
    /// In the case of an error the table remains unchanged.
    ///
    /// # Panics
    ///
    /// Panics if there is no current scope.
    pub fn insert(
        &mut self,
        name: SymbolName<'a>,
        version: Option<&semver::Version>,
        node: NodeId,
    ) -> Result<(), DuplicateSymbolError<'_>> {
        self.insert_binding(name, version, node)
    }

    /// Insert a new import into the current scope.
    ///
    /// # Errors
    ///
    /// Returns an error if the import is already defined in the current scope.
    /// In the case of an error the table remains unchanged.
    ///
    /// # Panics
    ///
    /// Panics if there is no current scope.
    pub fn insert_import(
        &mut self,
        name: SymbolName<'a>,
        version: Option<&semver::Version>,
        node: NodeId,
    ) -> Result<(), DuplicateSymbolError<'_>> {
        self.insert_binding(name, version, node)
    }

    fn insert_binding(
        &mut self,
        name: SymbolName<'a>,
        version: Option<&semver::Version>,
        node: NodeId,
    ) -> Result<(), DuplicateSymbolError<'_>> {
        let key = SymbolKey::new(name, version);
        let scope_depth = self.scopes.len() as u16 - 1;
        let (symbol_index, shadowed) = self.symbols.insert_full(key.clone(), self.bindings.len());

        if let Some(shadowed) = shadowed {
            let (shadowed_node, shadowed_binding) = self.bindings.get_index(shadowed).unwrap();
            if shadowed_binding.scope_depth == scope_depth {
                self.symbols.insert(key, shadowed);
                return Err(DuplicateSymbolError(name.into(), node, *shadowed_node));
            }
        }

        self.insert_latest_versioned(&key);

        self.bindings.insert(
            node,
            Binding {
                scope_depth,
                shadows: shadowed,
                symbol_index,
            },
        );

        Ok(())
    }

    /// Check whether a symbol is currently visible in the current scope.
    #[must_use]
    pub fn is_visible(&self, node: NodeId) -> bool {
        let Some(binding) = self.bindings.get(&node) else {
            return false;
        };

        // Check that the symbol has not been shadowed at this point.
        self.symbols[binding.symbol_index] == binding.symbol_index
    }

    /// Tries to resolve a symbol name in the current scope.
    pub fn resolve(
        &self,
        name: SymbolName<'a>,
        version: Option<&semver::Version>,
    ) -> Result<NodeId, UnknownSymbolError<'_>> {
        let index = match version {
            Some(version) => self
                .symbols
                .get(&SymbolKey::new(name, Some(version)))
                .copied(),
            None => self
                .symbols
                .get(&SymbolKey::new(name, None))
                .copied()
                .or_else(|| self.latest_versioned(name)),
        }
        .ok_or(UnknownSymbolError(name.into()))?;

        // NOTE: The unwrap is safe because the `symbols` map
        // points to valid indices in the `bindings` map.
        let (node, _) = self.bindings.get_index(index).unwrap();
        Ok(*node)
    }

    /// Return the latest visible versioned binding for `name`.
    ///
    /// The primary symbol map is kept in insertion order so scopes can be
    /// unwound as a stack. This side index groups the currently visible
    /// versioned bindings by symbol name to speed up the lookup of the latest
    /// declared version.
    fn latest_versioned(&self, name: SymbolName<'a>) -> Option<BindingIndex> {
        let latest = self.latest_versioned.get(name)?.last()?;
        self.symbols
            .get(&SymbolKey::new(name, Some(latest)))
            .copied()
    }

    /// Add `key` to the latest-version side index if it is versioned.
    fn insert_latest_versioned(&mut self, key: &SymbolKey<'a>) {
        let Some(version) = &key.version else {
            return;
        };

        self.latest_versioned
            .entry(key.name)
            .or_default()
            .insert(version.clone());
    }

    /// Remove a versioned binding from the side index.
    fn remove_latest_versioned(&mut self, key: &SymbolKey<'a>) {
        let Some(version) = &key.version else {
            return;
        };

        let Some(versions) = self.latest_versioned.get_mut(key.name) else {
            return;
        };

        versions.remove(version);
        if versions.is_empty() {
            self.latest_versioned.swap_remove(key.name);
        }
    }

    /// Returns the depth of the given region, if it corresponds to a currently open scope.
    #[must_use]
    pub fn region_to_depth(&self, region: RegionId) -> Option<ScopeDepth> {
        Some(self.scopes.get_index_of(&region)? as _)
    }

    /// Returns the region corresponding to the scope at the given depth.
    #[must_use]
    pub fn depth_to_region(&self, depth: ScopeDepth) -> Option<RegionId> {
        let (region, _) = self.scopes.get_index(depth as _)?;
        Some(*region)
    }

    /// Resets the symbol table to its initial state while maintaining its
    /// allocated memory.
    pub fn clear(&mut self) {
        self.symbols.clear();
        self.bindings.clear();
        self.scopes.clear();
        self.latest_versioned.clear();
    }
}

/// Symbol lookup key for a name and optional extension version.
///
/// The version is optional so the table can represent both legacy unversioned
/// symbols and modern versioned extension symbols. `None` is a real binding key,
/// not a wildcard; wildcard/default behavior is handled explicitly by
/// [`SymbolTable::resolve`].
///
/// TODO: Remove the legacy unversioned path once encoded HUGRs without
/// extension version information are no longer supported.
/// <http://github.com/Quantinuum/hugr/issues/3086>
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct SymbolKey<'a> {
    name: SymbolName<'a>,
    version: Option<semver::Version>,
}

impl<'a> SymbolKey<'a> {
    fn new(name: SymbolName<'a>, version: Option<&semver::Version>) -> Self {
        Self {
            name,
            version: version.cloned(),
        }
    }
}

#[derive(Debug, Clone, Copy)]
struct Binding {
    /// The depth of the scope in which this binding is defined.
    scope_depth: ScopeDepth,

    /// The index of the binding that is shadowed by this one, if any.
    shadows: Option<BindingIndex>,

    /// The index of this binding's symbol in the symbol table.
    ///
    /// The symbol table always points to the currently visible binding for a
    /// symbol. Therefore this index is only valid if this binding is not shadowed.
    /// In particular, we detect shadowing by checking if the entry in the symbol
    /// table at this index does indeed point to this binding.
    symbol_index: SymbolIndex,
}

#[derive(Debug, Clone, Copy)]
struct Scope {
    /// The length of the `bindings` stack when this scope was entered.
    binding_stack: usize,
}

type BindingIndex = usize;
type SymbolIndex = usize;

pub type ScopeDepth = u16;

/// Error that occurs when trying to resolve an unknown symbol.
#[derive(Debug, Clone, Error)]
#[error("symbol name `{0}` not found in this scope")]
pub struct UnknownSymbolError<'a>(pub Cow<'a, str>);

/// Error that occurs when trying to introduce a symbol that is already defined in the current scope.
#[derive(Debug, Clone, Error)]
#[error("symbol `{0}` is already defined in this scope")]
pub struct DuplicateSymbolError<'a>(pub Cow<'a, str>, pub NodeId, pub NodeId);

#[cfg(test)]
mod tests {
    use super::*;
    use rstest::{fixture, rstest};

    #[derive(Debug, Clone, Copy)]
    enum BindingSource {
        Import,
        Symbol,
    }

    impl BindingSource {
        fn insert(
            self,
            symbols: &mut SymbolTable<'static>,
            name: &'static str,
            version: Option<&semver::Version>,
            node: NodeId,
        ) {
            match self {
                Self::Import => symbols.insert_import(name, version, node).unwrap(),
                Self::Symbol => symbols.insert(name, version, node).unwrap(),
            }
        }
    }

    #[fixture]
    fn symbols() -> SymbolTable<'static> {
        let mut symbols = SymbolTable::new();
        symbols.enter(RegionId(0));
        symbols
    }

    fn version(version: &str) -> semver::Version {
        version.parse().unwrap()
    }

    /// Unversioned references resolve to the latest visible versioned binding.
    #[rstest]
    #[case::imports(BindingSource::Import)]
    #[case::symbols(BindingSource::Symbol)]
    fn latest_version(
        #[case] source: BindingSource,
        #[from(symbols)] mut symbols: SymbolTable<'static>,
    ) {
        let older = version("1.2.3");
        let newer = version("1.3.0");

        source.insert(&mut symbols, "std.int.add", Some(&older), NodeId(0));
        source.insert(&mut symbols, "std.int.add", Some(&newer), NodeId(1));

        assert_eq!(symbols.resolve("std.int.add", None).unwrap(), NodeId(1));
        assert_eq!(
            symbols.resolve("std.int.add", Some(&older)).unwrap(),
            NodeId(0)
        );
        assert_eq!(
            symbols.resolve("std.int.add", Some(&newer)).unwrap(),
            NodeId(1)
        );
    }

    /// A single visible versioned binding can be inferred from an unversioned use.
    #[rstest]
    #[case::imports(BindingSource::Import)]
    #[case::symbols(BindingSource::Symbol)]
    fn single_version(
        #[case] source: BindingSource,
        #[from(symbols)] mut symbols: SymbolTable<'static>,
    ) {
        let version = version("1.2.3");
        source.insert(&mut symbols, "std.int.add", Some(&version), NodeId(0));

        assert_eq!(symbols.resolve("std.int.add", None).unwrap(), NodeId(0));
    }

    /// An exact unversioned binding is separate from versioned extension bindings.
    #[rstest]
    fn unversioned_is_exact(#[from(symbols)] mut symbols: SymbolTable<'static>) {
        let version = version("1.2.3");

        symbols.insert("std.int.add", None, NodeId(0)).unwrap();
        symbols
            .insert_import("std.int.add", Some(&version), NodeId(1))
            .unwrap();

        assert_eq!(symbols.resolve("std.int.add", None).unwrap(), NodeId(0));
        assert_eq!(
            symbols.resolve("std.int.add", Some(&version)).unwrap(),
            NodeId(1)
        );
    }

    /// Leaving a scope restores the latest visible version from the outer scope.
    #[rstest]
    #[case::imports(BindingSource::Import)]
    #[case::symbols(BindingSource::Symbol)]
    fn latest_after_exit(
        #[case] source: BindingSource,
        #[from(symbols)] mut symbols: SymbolTable<'static>,
    ) {
        let older = version("1.2.3");
        let newer = version("1.3.0");

        source.insert(&mut symbols, "std.int.add", Some(&older), NodeId(0));
        symbols.enter(RegionId(1));
        source.insert(&mut symbols, "std.int.add", Some(&newer), NodeId(1));

        assert_eq!(symbols.resolve("std.int.add", None).unwrap(), NodeId(1));

        symbols.exit();

        assert_eq!(symbols.resolve("std.int.add", None).unwrap(), NodeId(0));
    }

    /// Shadowing the same version restores the shadowed binding on scope exit.
    #[rstest]
    #[case::imports(BindingSource::Import)]
    #[case::symbols(BindingSource::Symbol)]
    fn latest_shadowed(
        #[case] source: BindingSource,
        #[from(symbols)] mut symbols: SymbolTable<'static>,
    ) {
        let version = version("1.2.3");

        source.insert(&mut symbols, "std.int.add", Some(&version), NodeId(0));
        symbols.enter(RegionId(1));
        source.insert(&mut symbols, "std.int.add", Some(&version), NodeId(1));

        assert_eq!(symbols.resolve("std.int.add", None).unwrap(), NodeId(1));

        symbols.exit();

        assert_eq!(symbols.resolve("std.int.add", None).unwrap(), NodeId(0));
    }
}