Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions hugr-core/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use std::sync::Arc;

use crate::{
Direction, Hugr, HugrView, Node, Port,
extension::{ExtensionId, ExtensionRegistry, SignatureError},
extension::{
ExtensionId, ExtensionRegistry, SignatureError, resolution::ExtensionResolutionError,
},
hugr::{HugrMut, NodeMetadata},
ops::{
AliasDecl, AliasDefn, CFG, Call, CallIndirect, Case, Conditional, Const, DFG,
Expand Down Expand Up @@ -60,16 +62,20 @@ enum ImportErrorInner {
Context(#[source] Box<ImportErrorInner>, String),

/// A signature mismatch was detected during import.
#[error("signature error: {0}")]
#[error("signature error")]
Signature(#[from] SignatureError),

/// An error relating to the loaded extension registry.
#[error("extension error: {0}")]
#[error("extension error")]
Extension(#[from] ExtensionError),

/// Incorrect order hints.
#[error("incorrect order hint: {0}")]
#[error("incorrect order hint")]
OrderHint(#[from] OrderHintError),

/// Extension resolution.
#[error("extension resolution error")]
ExtensionResolution(#[from] ExtensionResolutionError),
}

#[derive(Debug, Clone, Error)]
Expand Down Expand Up @@ -186,6 +192,10 @@ pub fn import_hugr(
ctx.link_ports()?;
ctx.link_static_ports()?;

ctx.hugr
.resolve_extension_defs(extensions)
.map_err(ImportErrorInner::ExtensionResolution)?;

Ok(ctx.hugr)
}

Expand Down
32 changes: 22 additions & 10 deletions hugr-core/tests/snapshots/model__roundtrip_add.snap
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
---
source: hugr-core/tests/model.rs
expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-add.edn\"))"
expression: ast
---
(hugr 0)

(mod)

(import core.fn)
(import core.meta.description)

(import core.nat)

(import arithmetic.int.iadd)
(import core.fn)

(import arithmetic.int.types.int)

(declare-operation
arithmetic.int.iadd
(param ?0 core.nat)
(core.fn
[(arithmetic.int.types.int ?0) (arithmetic.int.types.int ?0)]
[(arithmetic.int.types.int ?0)])
(meta
(core.meta.description
"addition modulo 2^N (signed and unsigned versions are the same op)")))

(define-func
example.add
(core.fn
[arithmetic.int.types.int arithmetic.int.types.int]
[arithmetic.int.types.int])
[(arithmetic.int.types.int 6) (arithmetic.int.types.int 6)]
[(arithmetic.int.types.int 6)])
(dfg [%0 %1] [%2]
(signature
(core.fn
[arithmetic.int.types.int arithmetic.int.types.int]
[arithmetic.int.types.int]))
(arithmetic.int.iadd [%0 %1] [%2]
[(arithmetic.int.types.int 6) (arithmetic.int.types.int 6)]
[(arithmetic.int.types.int 6)]))
((arithmetic.int.iadd 6) [%0 %1] [%2]
(signature
(core.fn
[arithmetic.int.types.int arithmetic.int.types.int]
[arithmetic.int.types.int])))))
[(arithmetic.int.types.int 6) (arithmetic.int.types.int 6)]
[(arithmetic.int.types.int 6)])))))
40 changes: 27 additions & 13 deletions hugr-core/tests/snapshots/model__roundtrip_cond.snap
Original file line number Diff line number Diff line change
@@ -1,42 +1,56 @@
---
source: hugr-core/tests/model.rs
expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-cond.edn\"))"
expression: ast
---
(hugr 0)

(mod)

(import core.meta.description)

(import core.nat)

(import core.fn)

(import core.adt)

(import arithmetic.int.types.int)

(import arithmetic.int.ineg)
(declare-operation
arithmetic.int.ineg
(param ?0 core.nat)
(core.fn [(arithmetic.int.types.int ?0)] [(arithmetic.int.types.int ?0)])
(meta
(core.meta.description
"negation modulo 2^N (signed and unsigned versions are the same op)")))

(define-func
example.cond
(core.fn
[(core.adt [[] []]) arithmetic.int.types.int]
[arithmetic.int.types.int])
[(core.adt [[] []]) (arithmetic.int.types.int 6)]
[(arithmetic.int.types.int 6)])
(dfg [%0 %1] [%2]
(signature
(core.fn
[(core.adt [[] []]) arithmetic.int.types.int]
[arithmetic.int.types.int]))
[(core.adt [[] []]) (arithmetic.int.types.int 6)]
[(arithmetic.int.types.int 6)]))
(cond [%0 %1] [%2]
(signature
(core.fn
[(core.adt [[] []]) arithmetic.int.types.int]
[arithmetic.int.types.int]))
[(core.adt [[] []]) (arithmetic.int.types.int 6)]
[(arithmetic.int.types.int 6)]))
(dfg [%3] [%3]
(signature
(core.fn [arithmetic.int.types.int] [arithmetic.int.types.int])))
(core.fn
[(arithmetic.int.types.int 6)]
[(arithmetic.int.types.int 6)])))
(dfg [%4] [%5]
(signature
(core.fn [arithmetic.int.types.int] [arithmetic.int.types.int]))
(arithmetic.int.ineg [%4] [%5]
(core.fn
[(arithmetic.int.types.int 6)]
[(arithmetic.int.types.int 6)]))
((arithmetic.int.ineg 6) [%4] [%5]
(signature
(core.fn
[arithmetic.int.types.int]
[arithmetic.int.types.int])))))))
[(arithmetic.int.types.int 6)]
[(arithmetic.int.types.int 6)])))))))
62 changes: 36 additions & 26 deletions hugr-core/tests/snapshots/model__roundtrip_order.snap
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
---
source: hugr-core/tests/model.rs
expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-order.edn\"))"
expression: ast
---
(hugr 0)

(mod)

(import core.meta.description)

(import core.nat)

(import core.order_hint.key)

(import core.fn)
Expand All @@ -14,47 +18,53 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-orde

(import arithmetic.int.types.int)

(import arithmetic.int.ineg)
(declare-operation
arithmetic.int.ineg
(param ?0 core.nat)
(core.fn [(arithmetic.int.types.int ?0)] [(arithmetic.int.types.int ?0)])
(meta
(core.meta.description
"negation modulo 2^N (signed and unsigned versions are the same op)")))

(define-func
main
(core.fn
[arithmetic.int.types.int
arithmetic.int.types.int
arithmetic.int.types.int
arithmetic.int.types.int]
[arithmetic.int.types.int
arithmetic.int.types.int
arithmetic.int.types.int
arithmetic.int.types.int])
[(arithmetic.int.types.int 6)
(arithmetic.int.types.int 6)
(arithmetic.int.types.int 6)
(arithmetic.int.types.int 6)]
[(arithmetic.int.types.int 6)
(arithmetic.int.types.int 6)
(arithmetic.int.types.int 6)
(arithmetic.int.types.int 6)])
(dfg [%0 %1 %2 %3] [%4 %5 %6 %7]
(signature
(core.fn
[arithmetic.int.types.int
arithmetic.int.types.int
arithmetic.int.types.int
arithmetic.int.types.int]
[arithmetic.int.types.int
arithmetic.int.types.int
arithmetic.int.types.int
arithmetic.int.types.int]))
[(arithmetic.int.types.int 6)
(arithmetic.int.types.int 6)
(arithmetic.int.types.int 6)
(arithmetic.int.types.int 6)]
[(arithmetic.int.types.int 6)
(arithmetic.int.types.int 6)
(arithmetic.int.types.int 6)
(arithmetic.int.types.int 6)]))
(meta (core.order_hint.order 4 7))
(meta (core.order_hint.order 5 6))
(meta (core.order_hint.order 5 4))
(meta (core.order_hint.order 6 7))
(arithmetic.int.ineg [%0] [%4]
((arithmetic.int.ineg 6) [%0] [%4]
(signature
(core.fn [arithmetic.int.types.int] [arithmetic.int.types.int]))
(core.fn [(arithmetic.int.types.int 6)] [(arithmetic.int.types.int 6)]))
(meta (core.order_hint.key 4)))
(arithmetic.int.ineg [%1] [%5]
((arithmetic.int.ineg 6) [%1] [%5]
(signature
(core.fn [arithmetic.int.types.int] [arithmetic.int.types.int]))
(core.fn [(arithmetic.int.types.int 6)] [(arithmetic.int.types.int 6)]))
(meta (core.order_hint.key 5)))
(arithmetic.int.ineg [%2] [%6]
((arithmetic.int.ineg 6) [%2] [%6]
(signature
(core.fn [arithmetic.int.types.int] [arithmetic.int.types.int]))
(core.fn [(arithmetic.int.types.int 6)] [(arithmetic.int.types.int 6)]))
(meta (core.order_hint.key 6)))
(arithmetic.int.ineg [%3] [%7]
((arithmetic.int.ineg 6) [%3] [%7]
(signature
(core.fn [arithmetic.int.types.int] [arithmetic.int.types.int]))
(core.fn [(arithmetic.int.types.int 6)] [(arithmetic.int.types.int 6)]))
(meta (core.order_hint.key 7)))))
24 changes: 14 additions & 10 deletions hugr-model/tests/fixtures/model-add.edn
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

(mod)

(define-func example.add
(define-func
example.add
(core.fn
[arithmetic.int.types.int arithmetic.int.types.int]
[arithmetic.int.types.int])
(dfg
[%0 %1]
[%2]
(signature (core.fn [arithmetic.int.types.int arithmetic.int.types.int] [arithmetic.int.types.int]))
(arithmetic.int.iadd
[%0 %1] [%2]
(signature (core.fn [arithmetic.int.types.int arithmetic.int.types.int] [arithmetic.int.types.int])))))
[(arithmetic.int.types.int 6) (arithmetic.int.types.int 6)]
[(arithmetic.int.types.int 6)])
(dfg [%0 %1] [%2]
(signature
(core.fn
[(arithmetic.int.types.int 6) (arithmetic.int.types.int 6)]
[(arithmetic.int.types.int 6)]))
((arithmetic.int.iadd 6) [%0 %1] [%2]
(signature
(core.fn
[(arithmetic.int.types.int 6) (arithmetic.int.types.int 6)]
[(arithmetic.int.types.int 6)])))))
37 changes: 25 additions & 12 deletions hugr-model/tests/fixtures/model-cond.edn
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,29 @@

(mod)

(define-func example.cond
(core.fn [(core.adt [[] []]) arithmetic.int.types.int]
[arithmetic.int.types.int])
(define-func
example.cond
(core.fn
[(core.adt [[] []]) (arithmetic.int.types.int 6)]
[(arithmetic.int.types.int 6)])
(dfg [%0 %1] [%2]
(signature (core.fn [(core.adt [[] []]) arithmetic.int.types.int] [arithmetic.int.types.int]))
(cond [%0 %1] [%2]
(signature (core.fn [(core.adt [[] []]) arithmetic.int.types.int] [arithmetic.int.types.int]))
(dfg [%3] [%3]
(signature (core.fn [arithmetic.int.types.int] [arithmetic.int.types.int])))
(dfg [%4] [%5]
(signature (core.fn [arithmetic.int.types.int] [arithmetic.int.types.int]))
(arithmetic.int.ineg [%4] [%5]
(signature (core.fn [arithmetic.int.types.int] [arithmetic.int.types.int])))))))
(signature
(core.fn
[(core.adt [[] []]) (arithmetic.int.types.int 6)]
[(arithmetic.int.types.int 6)]))
(cond [%0 %1] [%2]
(signature
(core.fn
[(core.adt [[] []]) (arithmetic.int.types.int 6)]
[(arithmetic.int.types.int 6)]))
(dfg [%3] [%3]
(signature
(core.fn [(arithmetic.int.types.int 6)] [(arithmetic.int.types.int 6)])))
(dfg [%4] [%5]
(signature
(core.fn [(arithmetic.int.types.int 6)] [(arithmetic.int.types.int 6)]))
((arithmetic.int.ineg 6) [%4] [%5]
(signature
(core.fn
[(arithmetic.int.types.int 6)]
[(arithmetic.int.types.int 6)])))))))
Loading
Loading