Skip to content

Commit

Permalink
feat: Simplify hugr-model (#1893)
Browse files Browse the repository at this point in the history
This PR simplifies `hugr_model` by moving every operation and term that
can be defined as an extension to a core extension. This way we do not
need special cases in the binary format, text format, validation
(future), codegen (future), documentation generation (future). Closes
#1900.
  • Loading branch information
zrho authored Feb 4, 2025
1 parent 7312c62 commit 2630e3d
Show file tree
Hide file tree
Showing 33 changed files with 1,508 additions and 2,111 deletions.
286 changes: 127 additions & 159 deletions hugr-core/src/export.rs

Large diffs are not rendered by default.

901 changes: 499 additions & 402 deletions hugr-core/src/import.rs

Large diffs are not rendered by default.

25 changes: 11 additions & 14 deletions hugr-core/tests/snapshots/model__roundtrip_add.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-add.
---
(hugr 0)

(import arithmetic.int.iadd)

(import arithmetic.int.types.int)

(define-func example.add
[(@ arithmetic.int.types.int) (@ arithmetic.int.types.int)]
[(@ arithmetic.int.types.int)]
(ext)
(core.fn
[arithmetic.int.types.int arithmetic.int.types.int]
[arithmetic.int.types.int]
(ext))
(dfg
[%0 %1] [%2]
(signature
(->
[(@ arithmetic.int.types.int) (@ arithmetic.int.types.int)]
[(@ arithmetic.int.types.int)]
(core.fn
[arithmetic.int.types.int arithmetic.int.types.int]
[arithmetic.int.types.int]
(ext)))
((@ arithmetic.int.iadd) [%0 %1] [%2]
(arithmetic.int.iadd [%0 %1] [%2]
(signature
(->
[(@ arithmetic.int.types.int) (@ arithmetic.int.types.int)]
[(@ arithmetic.int.types.int)]
(core.fn
[arithmetic.int.types.int arithmetic.int.types.int]
[arithmetic.int.types.int]
(ext arithmetic.int))))))
8 changes: 3 additions & 5 deletions hugr-core/tests/snapshots/model__roundtrip_alias.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-alia
---
(hugr 0)

(import arithmetic.int.types.int)
(declare-alias local.float core.type)

(declare-alias local.float type)
(define-alias local.int core.type arithmetic.int.types.int)

(define-alias local.int type (@ arithmetic.int.types.int))

(define-alias local.endo type (-> [] [] (ext)))
(define-alias local.endo core.type (core.fn [] [] (ext)))
80 changes: 41 additions & 39 deletions hugr-core/tests/snapshots/model__roundtrip_call.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,68 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-call
---
(hugr 0)

(import compat.meta-json)

(import arithmetic.int.types.int)

(declare-func example.callee
(forall ?0 ext-set)
[(@ arithmetic.int.types.int)]
[(@ arithmetic.int.types.int)]
(ext ?0 ... arithmetic.int)
(meta
(@ compat.meta-json "description" "\"This is a function declaration.\""))
(meta (@ compat.meta-json "title" "\"Callee\"")))
(param ?0 core.ext_set)
(core.fn
[arithmetic.int.types.int]
[arithmetic.int.types.int]
(ext ?0 ... arithmetic.int))
(meta (compat.meta_json "description" "\"This is a function declaration.\""))
(meta (compat.meta_json "title" "\"Callee\"")))

(define-func example.caller
[(@ arithmetic.int.types.int)]
[(@ arithmetic.int.types.int)]
(ext arithmetic.int)
(core.fn
[arithmetic.int.types.int]
[arithmetic.int.types.int]
(ext arithmetic.int))
(meta
(@
compat.meta-json
(compat.meta_json
"description"
"\"This defines a function that calls the function which we declared earlier.\""))
(meta (@ compat.meta-json "title" "\"Caller\""))
(meta (compat.meta_json "title" "\"Caller\""))
(dfg
[%0] [%1]
(signature
(->
[(@ arithmetic.int.types.int)]
[(@ arithmetic.int.types.int)]
(core.fn
[arithmetic.int.types.int]
[arithmetic.int.types.int]
(ext arithmetic.int)))
(call (@ example.callee (ext)) [%0] [%1]
((core.call_indirect
[arithmetic.int.types.int]
[arithmetic.int.types.int]
(ext arithmetic.int)
(example.callee (ext)))
[%0] [%1]
(signature
(->
[(@ arithmetic.int.types.int)]
[(@ arithmetic.int.types.int)]
(core.fn
[arithmetic.int.types.int]
[arithmetic.int.types.int]
(ext arithmetic.int))))))

(define-func example.load
[]
[(->
[(@ arithmetic.int.types.int)]
[(@ arithmetic.int.types.int)]
(ext arithmetic.int))]
(ext)
(core.fn
[]
[(core.fn
[arithmetic.int.types.int]
[arithmetic.int.types.int]
(ext arithmetic.int))]
(ext))
(dfg
[] [%0]
(signature
(->
(core.fn
[]
[(->
[(@ arithmetic.int.types.int)]
[(@ arithmetic.int.types.int)]
[(core.fn
[arithmetic.int.types.int]
[arithmetic.int.types.int]
(ext arithmetic.int))]
(ext)))
(load-func (@ example.caller) [] [%0]
((core.load_const _ _ example.caller) [] [%0]
(signature
(->
(core.fn
[]
[(->
[(@ arithmetic.int.types.int)]
[(@ arithmetic.int.types.int)]
[(core.fn
[arithmetic.int.types.int]
[arithmetic.int.types.int]
(ext arithmetic.int))]
(ext))))))
31 changes: 17 additions & 14 deletions hugr-core/tests/snapshots/model__roundtrip_cfg.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,31 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-cfg.
(hugr 0)

(define-func example.cfg
(forall ?0 type)
[?0] [?0] (ext)
(param ?0 core.type)
(core.fn [?0] [?0] (ext))
(dfg
[%0] [%1]
(signature (-> [?0] [?0] (ext)))
(signature (core.fn [?0] [?0] (ext)))
(cfg [%0] [%1]
(signature (-> [?0] [?0] (ext)))
(signature (core.fn [?0] [?0] (ext)))
(cfg
[%2] [%3]
(signature (-> [(ctrl [?0])] [(ctrl [?0])] (ext)))
(signature (core.fn [(core.ctrl [?0])] [(core.ctrl [?0])] (ext)))
(block [%2] [%6]
(signature (-> [(ctrl [?0])] [(ctrl [?0])] (ext)))
(signature (core.fn [(core.ctrl [?0])] [(core.ctrl [?0])] (ext)))
(dfg
[%4] [%5]
(signature (-> [?0] [(adt [[?0]])] (ext)))
(tag 0 [%4] [%5] (signature (-> [?0] [(adt [[?0]])] (ext))))))
(signature (core.fn [?0] [(core.adt [[?0]])] (ext)))
((core.make_adt _ _ 0) [%4] [%5]
(signature (core.fn [?0] [(core.adt [[?0]])] (ext))))))
(block [%6] [%3 %6]
(signature (-> [(ctrl [?0])] [(ctrl [?0]) (ctrl [?0])] (ext)))
(signature
(core.fn
[(core.ctrl [?0])]
[(core.ctrl [?0]) (core.ctrl [?0])]
(ext)))
(dfg
[%7] [%8]
(signature (-> [?0] [(adt [[?0] [?0]])] (ext)))
(tag
0
[%7] [%8]
(signature (-> [?0] [(adt [[?0] [?0]])] (ext))))))))))
(signature (core.fn [?0] [(core.adt [[?0] [?0]])] (ext)))
((core.make_adt _ _ 0) [%7] [%8]
(signature (core.fn [?0] [(core.adt [[?0] [?0]])] (ext))))))))))
42 changes: 18 additions & 24 deletions hugr-core/tests/snapshots/model__roundtrip_cond.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,39 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-cond
---
(hugr 0)

(import arithmetic.int.types.int)

(import arithmetic.int.ineg)

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

0 comments on commit 2630e3d

Please sign in to comment.