Skip to content

Commit 2630e3d

Browse files
authored
feat: Simplify hugr-model (#1893)
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.
1 parent 7312c62 commit 2630e3d

33 files changed

+1508
-2111
lines changed

hugr-core/src/export.rs

Lines changed: 127 additions & 159 deletions
Large diffs are not rendered by default.

hugr-core/src/import.rs

Lines changed: 499 additions & 402 deletions
Large diffs are not rendered by default.

hugr-core/tests/snapshots/model__roundtrip_add.snap

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,21 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-add.
44
---
55
(hugr 0)
66

7-
(import arithmetic.int.iadd)
8-
9-
(import arithmetic.int.types.int)
10-
117
(define-func example.add
12-
[(@ arithmetic.int.types.int) (@ arithmetic.int.types.int)]
13-
[(@ arithmetic.int.types.int)]
14-
(ext)
8+
(core.fn
9+
[arithmetic.int.types.int arithmetic.int.types.int]
10+
[arithmetic.int.types.int]
11+
(ext))
1512
(dfg
1613
[%0 %1] [%2]
1714
(signature
18-
(->
19-
[(@ arithmetic.int.types.int) (@ arithmetic.int.types.int)]
20-
[(@ arithmetic.int.types.int)]
15+
(core.fn
16+
[arithmetic.int.types.int arithmetic.int.types.int]
17+
[arithmetic.int.types.int]
2118
(ext)))
22-
((@ arithmetic.int.iadd) [%0 %1] [%2]
19+
(arithmetic.int.iadd [%0 %1] [%2]
2320
(signature
24-
(->
25-
[(@ arithmetic.int.types.int) (@ arithmetic.int.types.int)]
26-
[(@ arithmetic.int.types.int)]
21+
(core.fn
22+
[arithmetic.int.types.int arithmetic.int.types.int]
23+
[arithmetic.int.types.int]
2724
(ext arithmetic.int))))))

hugr-core/tests/snapshots/model__roundtrip_alias.snap

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-alia
44
---
55
(hugr 0)
66

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

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

11-
(define-alias local.int type (@ arithmetic.int.types.int))
12-
13-
(define-alias local.endo type (-> [] [] (ext)))
11+
(define-alias local.endo core.type (core.fn [] [] (ext)))

hugr-core/tests/snapshots/model__roundtrip_call.snap

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,66 +4,68 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-call
44
---
55
(hugr 0)
66

7-
(import compat.meta-json)
8-
9-
(import arithmetic.int.types.int)
10-
117
(declare-func example.callee
12-
(forall ?0 ext-set)
13-
[(@ arithmetic.int.types.int)]
14-
[(@ arithmetic.int.types.int)]
15-
(ext ?0 ... arithmetic.int)
16-
(meta
17-
(@ compat.meta-json "description" "\"This is a function declaration.\""))
18-
(meta (@ compat.meta-json "title" "\"Callee\"")))
8+
(param ?0 core.ext_set)
9+
(core.fn
10+
[arithmetic.int.types.int]
11+
[arithmetic.int.types.int]
12+
(ext ?0 ... arithmetic.int))
13+
(meta (compat.meta_json "description" "\"This is a function declaration.\""))
14+
(meta (compat.meta_json "title" "\"Callee\"")))
1915

2016
(define-func example.caller
21-
[(@ arithmetic.int.types.int)]
22-
[(@ arithmetic.int.types.int)]
23-
(ext arithmetic.int)
17+
(core.fn
18+
[arithmetic.int.types.int]
19+
[arithmetic.int.types.int]
20+
(ext arithmetic.int))
2421
(meta
25-
(@
26-
compat.meta-json
22+
(compat.meta_json
2723
"description"
2824
"\"This defines a function that calls the function which we declared earlier.\""))
29-
(meta (@ compat.meta-json "title" "\"Caller\""))
25+
(meta (compat.meta_json "title" "\"Caller\""))
3026
(dfg
3127
[%0] [%1]
3228
(signature
33-
(->
34-
[(@ arithmetic.int.types.int)]
35-
[(@ arithmetic.int.types.int)]
29+
(core.fn
30+
[arithmetic.int.types.int]
31+
[arithmetic.int.types.int]
3632
(ext arithmetic.int)))
37-
(call (@ example.callee (ext)) [%0] [%1]
33+
((core.call_indirect
34+
[arithmetic.int.types.int]
35+
[arithmetic.int.types.int]
36+
(ext arithmetic.int)
37+
(example.callee (ext)))
38+
[%0] [%1]
3839
(signature
39-
(->
40-
[(@ arithmetic.int.types.int)]
41-
[(@ arithmetic.int.types.int)]
40+
(core.fn
41+
[arithmetic.int.types.int]
42+
[arithmetic.int.types.int]
4243
(ext arithmetic.int))))))
4344

4445
(define-func example.load
45-
[]
46-
[(->
47-
[(@ arithmetic.int.types.int)]
48-
[(@ arithmetic.int.types.int)]
49-
(ext arithmetic.int))]
50-
(ext)
46+
(core.fn
47+
[]
48+
[(core.fn
49+
[arithmetic.int.types.int]
50+
[arithmetic.int.types.int]
51+
(ext arithmetic.int))]
52+
(ext))
5153
(dfg
5254
[] [%0]
5355
(signature
54-
(->
56+
(core.fn
5557
[]
56-
[(->
57-
[(@ arithmetic.int.types.int)]
58-
[(@ arithmetic.int.types.int)]
58+
[(core.fn
59+
[arithmetic.int.types.int]
60+
[arithmetic.int.types.int]
5961
(ext arithmetic.int))]
6062
(ext)))
61-
(load-func (@ example.caller) [] [%0]
63+
((core.load_const _ _ example.caller) [] [%0]
6264
(signature
63-
(->
65+
(core.fn
6466
[]
65-
[(->
66-
[(@ arithmetic.int.types.int)]
67-
[(@ arithmetic.int.types.int)]
67+
[(core.fn
68+
[arithmetic.int.types.int]
69+
[arithmetic.int.types.int]
6870
(ext arithmetic.int))]
6971
(ext))))))

hugr-core/tests/snapshots/model__roundtrip_cfg.snap

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,31 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-cfg.
55
(hugr 0)
66

77
(define-func example.cfg
8-
(forall ?0 type)
9-
[?0] [?0] (ext)
8+
(param ?0 core.type)
9+
(core.fn [?0] [?0] (ext))
1010
(dfg
1111
[%0] [%1]
12-
(signature (-> [?0] [?0] (ext)))
12+
(signature (core.fn [?0] [?0] (ext)))
1313
(cfg [%0] [%1]
14-
(signature (-> [?0] [?0] (ext)))
14+
(signature (core.fn [?0] [?0] (ext)))
1515
(cfg
1616
[%2] [%3]
17-
(signature (-> [(ctrl [?0])] [(ctrl [?0])] (ext)))
17+
(signature (core.fn [(core.ctrl [?0])] [(core.ctrl [?0])] (ext)))
1818
(block [%2] [%6]
19-
(signature (-> [(ctrl [?0])] [(ctrl [?0])] (ext)))
19+
(signature (core.fn [(core.ctrl [?0])] [(core.ctrl [?0])] (ext)))
2020
(dfg
2121
[%4] [%5]
22-
(signature (-> [?0] [(adt [[?0]])] (ext)))
23-
(tag 0 [%4] [%5] (signature (-> [?0] [(adt [[?0]])] (ext))))))
22+
(signature (core.fn [?0] [(core.adt [[?0]])] (ext)))
23+
((core.make_adt _ _ 0) [%4] [%5]
24+
(signature (core.fn [?0] [(core.adt [[?0]])] (ext))))))
2425
(block [%6] [%3 %6]
25-
(signature (-> [(ctrl [?0])] [(ctrl [?0]) (ctrl [?0])] (ext)))
26+
(signature
27+
(core.fn
28+
[(core.ctrl [?0])]
29+
[(core.ctrl [?0]) (core.ctrl [?0])]
30+
(ext)))
2631
(dfg
2732
[%7] [%8]
28-
(signature (-> [?0] [(adt [[?0] [?0]])] (ext)))
29-
(tag
30-
0
31-
[%7] [%8]
32-
(signature (-> [?0] [(adt [[?0] [?0]])] (ext))))))))))
33+
(signature (core.fn [?0] [(core.adt [[?0] [?0]])] (ext)))
34+
((core.make_adt _ _ 0) [%7] [%8]
35+
(signature (core.fn [?0] [(core.adt [[?0] [?0]])] (ext))))))))))

hugr-core/tests/snapshots/model__roundtrip_cond.snap

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,39 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-cond
44
---
55
(hugr 0)
66

7-
(import arithmetic.int.types.int)
8-
9-
(import arithmetic.int.ineg)
10-
117
(define-func example.cond
12-
[(adt [[] []]) (@ arithmetic.int.types.int)]
13-
[(@ arithmetic.int.types.int)]
14-
(ext)
8+
(core.fn
9+
[(core.adt [[] []]) arithmetic.int.types.int]
10+
[arithmetic.int.types.int]
11+
(ext))
1512
(dfg
1613
[%0 %1] [%2]
1714
(signature
18-
(->
19-
[(adt [[] []]) (@ arithmetic.int.types.int)]
20-
[(@ arithmetic.int.types.int)]
15+
(core.fn
16+
[(core.adt [[] []]) arithmetic.int.types.int]
17+
[arithmetic.int.types.int]
2118
(ext)))
2219
(cond
2320
[%0 %1] [%2]
2421
(signature
25-
(->
26-
[(adt [[] []]) (@ arithmetic.int.types.int)]
27-
[(@ arithmetic.int.types.int)]
22+
(core.fn
23+
[(core.adt [[] []]) arithmetic.int.types.int]
24+
[arithmetic.int.types.int]
2825
(ext)))
2926
(dfg
3027
[%3] [%3]
3128
(signature
32-
(->
33-
[(@ arithmetic.int.types.int)]
34-
[(@ arithmetic.int.types.int)]
29+
(core.fn
30+
[arithmetic.int.types.int]
31+
[arithmetic.int.types.int]
3532
(ext))))
3633
(dfg
3734
[%4] [%5]
3835
(signature
39-
(->
40-
[(@ arithmetic.int.types.int)]
41-
[(@ arithmetic.int.types.int)]
42-
(ext)))
43-
((@ arithmetic.int.ineg) [%4] [%5]
36+
(core.fn [arithmetic.int.types.int] [arithmetic.int.types.int] (ext)))
37+
(arithmetic.int.ineg [%4] [%5]
4438
(signature
45-
(->
46-
[(@ arithmetic.int.types.int)]
47-
[(@ arithmetic.int.types.int)]
39+
(core.fn
40+
[arithmetic.int.types.int]
41+
[arithmetic.int.types.int]
4842
(ext arithmetic.int))))))))

0 commit comments

Comments
 (0)