grabapl 0.0.4

A library for graph-based programming languages, with pluggable type systems and a focus on visible intermediate states.
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
use grabapl::operation::builder::{BuilderOpLike, OperationBuilder, stack_based_builder};
use grabapl::operation::builtin::LibBuiltinOperation;
use grabapl::operation::query::BuiltinQuery;
use grabapl::operation::user_defined::{AbstractNodeId, UserDefinedOperation};
use grabapl::operation::{BuiltinOperation, run_from_concrete};
use grabapl::prelude::*;
use grabapl::semantics::ConcreteGraph;
use std::collections::{HashMap, HashSet};

mod util;
use util::semantics::*;

#[test]
fn no_modifications_dont_change_abstract_value() {
    let op_ctx = OperationContext::<TestSemantics>::new();
    let mut builder = OperationBuilder::new(&op_ctx, 0);

    builder
        .expect_parameter_node("a", NodeType::Integer)
        .unwrap();
    let a = AbstractNodeId::ParameterMarker("a".into());
    let state_before = builder.show_state().unwrap();
    builder
        .add_operation(BuilderOpLike::Builtin(TestOperation::NoOp), vec![a.clone()])
        .unwrap();
    let state_after = builder.show_state().unwrap();

    let a_type_before = state_before.node_av_of_aid(&a).unwrap();
    let a_type_after = state_after.node_av_of_aid(&a).unwrap();
    assert_eq!(
        a_type_before, a_type_after,
        "Abstract value of node did not remain unchanged after no-op operation"
    );
    assert_eq!(
        a_type_after,
        &NodeType::Integer,
        "Abstract value of node should be Integer after no-op operation"
    );
}

fn get_abstract_value_changing_operation() -> UserDefinedOperation<TestSemantics> {
    let op_ctx = OperationContext::<TestSemantics>::new();
    let mut builder = OperationBuilder::<TestSemantics>::new(&op_ctx, 0);
    builder
        .expect_parameter_node("p0", NodeType::Object)
        .unwrap();
    let p0 = AbstractNodeId::ParameterMarker("p0".into());
    builder
        .start_query(
            TestQuery::ValueEqualTo(NodeValue::Integer(0)),
            vec![p0.clone()],
        )
        .unwrap();
    builder.enter_true_branch().unwrap();
    builder
        .add_operation(
            BuilderOpLike::Builtin(TestOperation::SetTo {
                op_typ: NodeType::Object,
                target_typ: NodeType::String,
                value: NodeValue::String("Changed".to_string()),
            }),
            vec![p0.clone()],
        )
        .unwrap();
    builder.enter_false_branch().unwrap();
    builder
        .add_operation(
            BuilderOpLike::Builtin(TestOperation::SetTo {
                op_typ: NodeType::Object,
                target_typ: NodeType::Integer,
                value: NodeValue::Integer(42),
            }),
            vec![p0.clone()],
        )
        .unwrap();
    builder.end_query().unwrap();
    builder.build().unwrap()
}

fn get_abstract_value_changing_operation_no_branches() -> UserDefinedOperation<TestSemantics> {
    let op_ctx = OperationContext::<TestSemantics>::new();
    let mut builder = OperationBuilder::<TestSemantics>::new(&op_ctx, 0);
    builder
        .expect_parameter_node("p0", NodeType::Object)
        .unwrap();
    let p0 = AbstractNodeId::param("p0");
    // Add an operation that changes the abstract value
    builder
        .add_operation(
            BuilderOpLike::Builtin(TestOperation::SetTo {
                op_typ: NodeType::Object,
                // we *set* to the same type, which is not the same as a noop.
                target_typ: NodeType::Object,
                value: NodeValue::String("Changed".to_string()),
            }),
            vec![p0],
        )
        .unwrap();
    builder.build().unwrap()
}

#[test]
fn modifications_change_abstract_value_even_if_same_internal_type_for_custom() {
    let mut op_ctx = OperationContext::<TestSemantics>::new();
    op_ctx.add_custom_operation(0, get_abstract_value_changing_operation());
    let mut builder = OperationBuilder::new(&op_ctx, 1);

    builder
        .expect_parameter_node("a", NodeType::Integer)
        .unwrap();
    let a = AbstractNodeId::param("a");
    let state_before = builder.show_state().unwrap();

    // Add an operation that changes the abstract value
    builder
        .add_operation(BuilderOpLike::FromOperationId(0), vec![a.clone()])
        .unwrap();

    let state_after = builder.show_state().unwrap();

    let a_type_before = state_before.node_av_of_aid(&a).unwrap();
    let a_type_after = state_after.node_av_of_aid(&a).unwrap();

    assert_ne!(
        a_type_before, a_type_after,
        "Abstract value of node should change after operation"
    );
    assert_eq!(
        a_type_after,
        &NodeType::Object,
        "Abstract value of node should be Object after operation"
    );
}

#[test]
fn modifications_change_abstract_value_even_if_same_internal_type_for_builtin() {
    let op_ctx = OperationContext::<TestSemantics>::new();
    let mut builder = OperationBuilder::new(&op_ctx, 0);

    builder
        .expect_parameter_node("a", NodeType::Integer)
        .unwrap();
    let a = AbstractNodeId::param("a");
    let state_before = builder.show_state().unwrap();

    // Add an operation that changes the abstract value
    builder
        .add_operation(
            BuilderOpLike::Builtin(TestOperation::SetTo {
                op_typ: NodeType::Object,
                // we *set* to the same type, which is not the same as a noop.
                target_typ: NodeType::Object,
                value: NodeValue::String("Changed".to_string()),
            }),
            vec![a.clone()],
        )
        .unwrap();

    let state_after = builder.show_state().unwrap();

    let a_type_before = state_before.node_av_of_aid(&a).unwrap();
    let a_type_after = state_after.node_av_of_aid(&a).unwrap();

    assert_ne!(
        a_type_before, a_type_after,
        "Abstract value of node should change after operation"
    );
    assert_eq!(
        a_type_after,
        &NodeType::Object,
        "Abstract value of node should be Object after operation"
    );
}

#[test]
fn modifications_change_abstract_value_even_if_same_internal_type_for_custom_with_builtin() {
    let mut op_ctx = OperationContext::<TestSemantics>::new();
    op_ctx.add_custom_operation(0, get_abstract_value_changing_operation_no_branches());
    let mut builder = OperationBuilder::new(&op_ctx, 1);

    builder
        .expect_parameter_node("a", NodeType::Integer)
        .unwrap();
    let a = AbstractNodeId::param("a");
    let state_before = builder.show_state().unwrap();

    // Add an operation that changes the abstract value
    builder
        .add_operation(BuilderOpLike::FromOperationId(0), vec![a.clone()])
        .unwrap();

    let state_after = builder.show_state().unwrap();

    let a_type_before = state_before.node_av_of_aid(&a).unwrap();
    let a_type_after = state_after.node_av_of_aid(&a).unwrap();

    assert_ne!(
        a_type_before, a_type_after,
        "Abstract value of node should change after operation"
    );
    assert_eq!(
        a_type_after,
        &NodeType::Object,
        "Abstract value of node should be Object after operation"
    );
}

fn get_custom_op_new_node_in_regular_query_branches() -> UserDefinedOperation<TestSemantics> {
    let op_ctx = OperationContext::<TestSemantics>::new();
    let mut builder = OperationBuilder::<TestSemantics>::new(&op_ctx, 0);
    builder
        .expect_parameter_node("p0", NodeType::Object)
        .unwrap();
    let p0 = AbstractNodeId::param("p0");

    // Start a query that will create a new node in both branches
    builder
        .start_query(TestQuery::ValueEqualTo(NodeValue::Integer(0)), vec![p0])
        .unwrap();

    // True branch
    builder.enter_true_branch().unwrap();
    builder
        .add_named_operation(
            "new".into(),
            BuilderOpLike::Builtin(TestOperation::AddNode {
                node_type: NodeType::String,
                value: NodeValue::String("x".to_string()),
            }),
            vec![],
        )
        .unwrap();

    // False branch
    builder.enter_false_branch().unwrap();
    builder
        .add_named_operation(
            "new".into(),
            BuilderOpLike::Builtin(TestOperation::AddNode {
                node_type: NodeType::Integer,
                value: NodeValue::Integer(42),
            }),
            vec![],
        )
        .unwrap();

    builder.end_query().unwrap();

    let output_aid = AbstractNodeId::DynamicOutputMarker("new".into(), "new".into());
    builder
        .return_node(output_aid, "output".into(), NodeType::Object)
        .unwrap();

    builder.build().unwrap()
}

fn get_custom_op_new_node_in_shape_query_branches() -> UserDefinedOperation<TestSemantics> {
    let op_ctx = OperationContext::<TestSemantics>::new();
    let mut builder = OperationBuilder::<TestSemantics>::new(&op_ctx, 0);
    builder
        .expect_parameter_node("p0", NodeType::Object)
        .unwrap();
    let p0 = AbstractNodeId::param("p0");

    // Start a query that will create a new node in both branches
    builder.start_shape_query("new").unwrap();
    builder
        .expect_shape_node("new".into(), NodeType::String)
        .unwrap();
    // TODO: we will need an edge here.

    // True branch
    builder.enter_true_branch().unwrap();
    // TODO: rename

    // False branch
    builder.enter_false_branch().unwrap();
    builder
        .add_named_operation(
            "new".into(),
            BuilderOpLike::Builtin(TestOperation::AddNode {
                node_type: NodeType::Integer,
                value: NodeValue::Integer(42),
            }),
            vec![],
        )
        .unwrap();

    builder.end_query().unwrap();

    let output_aid = AbstractNodeId::DynamicOutputMarker("new".into(), "new".into());
    let res = builder.return_node(output_aid, "output".into(), NodeType::Object);
    // UPDATE: we're allowed to return shape query nodes now
    // assert!(
    //     res.is_err(),
    //     "`output_aid` partially originates from a shape query, hence it may not be returned"
    // );
    assert!(res.is_ok());

    builder.build().unwrap()
}

#[test]
fn new_node_from_both_branches_is_visible_for_regular_query() {
    let mut op_ctx = OperationContext::<TestSemantics>::new();
    op_ctx.add_custom_operation(0, get_custom_op_new_node_in_regular_query_branches());
    let mut builder = OperationBuilder::new(&op_ctx, 1);
    builder
        .expect_parameter_node("p0", NodeType::Integer)
        .unwrap();
    let p0 = AbstractNodeId::param("p0");
    let state_before = builder.show_state().unwrap();
    // Add an operation that creates a new node in both branches
    builder
        .add_named_operation(
            "helper".into(),
            BuilderOpLike::FromOperationId(0),
            vec![p0.clone()],
        )
        .unwrap();
    let state_after = builder.show_state().unwrap();
    let num_before = state_before.graph.nodes().count();
    let num_after = state_after.graph.nodes().count();
    assert_eq!(
        num_after,
        num_before + 1,
        "Expected a new node to be visible"
    );

    let returned_node = AbstractNodeId::DynamicOutputMarker("helper".into(), "output".into());

    // test that I can actually use the returned node
    builder
        .add_operation(
            BuilderOpLike::Builtin(TestOperation::CopyValueFromTo),
            vec![returned_node, p0.clone()],
        )
        .unwrap();
    let operation = builder.build().unwrap();
    op_ctx.add_custom_operation(1, operation);

    let mut concrete_graph = ConcreteGraph::<TestSemantics>::new();
    let p0_key = concrete_graph.add_node(NodeValue::Integer(0));
    run_from_concrete(&mut concrete_graph, &op_ctx, 1, &[p0_key]).unwrap();
    let new_node_value = concrete_graph.get_node_attr(p0_key).unwrap();
    assert_eq!(
        new_node_value,
        &NodeValue::String("x".to_string()),
        "Expected the new node to have the value 'x'"
    );
}

#[test]
fn new_node_from_both_branches_is_invisible_for_shape_query() {
    let mut op_ctx = OperationContext::<TestSemantics>::new();
    op_ctx.add_custom_operation(0, get_custom_op_new_node_in_shape_query_branches());
    let mut builder = OperationBuilder::new(&op_ctx, 1);
    let input_marker = SubstMarker::from("input");
    builder
        .expect_parameter_node(input_marker.clone(), NodeType::Integer)
        .unwrap();
    let input_aid = AbstractNodeId::ParameterMarker(input_marker.clone());
    let state_before = builder.show_state().unwrap();
    // Add an operation that creates a new node in both branches
    builder
        .add_named_operation(
            "helper".into(),
            BuilderOpLike::FromOperationId(0),
            vec![input_aid],
        )
        .unwrap();
    let state_after = builder.show_state().unwrap();
    let num_before = state_before.graph.nodes().count();
    let num_after = state_after.graph.nodes().count();
    // UPDATE: we're allowed to return shape query nodes now, so the number of nodes should change by one
    // assert_eq!(num_after, num_before, "Expected no new nodes to be visible");
    assert_eq!(
        num_after,
        num_before + 1,
        "Expected a new node to be visible"
    );
}

// TODO: Add test for the "additive changes in the shape query true branch" header from problems-testcases.md
//  i.e., _new_ nodes and _new_ edges, not just matched, pre-existing nodes in the true branch.

#[test]
fn return_node_partially_from_shape_query_fails() {
    let mut op_ctx = OperationContext::<TestSemantics>::new();
    let helper_op = {
        let mut builder = OperationBuilder::<TestSemantics>::new(&op_ctx, 0);
        builder
            .expect_parameter_node("p0", NodeType::Integer)
            .unwrap();
        let p0 = AbstractNodeId::param("p0");
        // Start a shape query to check if p0 has a child with edge 'child'
        builder.start_shape_query("child").unwrap();
        builder
            .expect_shape_node("new".into(), NodeType::String)
            .unwrap();
        let child_aid = AbstractNodeId::dynamic_output("child", "new");
        builder
            .expect_shape_edge(p0, child_aid.clone(), EdgeType::Exact("child".to_string()))
            .unwrap();
        builder.enter_false_branch().unwrap();
        // if we don't have a child node, create one
        builder
            .add_named_operation(
                "child".into(),
                BuilderOpLike::Builtin(TestOperation::AddNode {
                    node_type: NodeType::String,
                    value: NodeValue::String("x".to_string()),
                }),
                vec![],
            )
            .unwrap();
        builder.end_query().unwrap();

        // Return the child node
        let res = builder.return_node(child_aid, "child".into(), NodeType::String);
        // UPDATE: returning a node from shape queries is now allowed
        // assert!(
        //     res.is_err(),
        //     "Expected returning a node partially originating from a shape query to fail"
        // );
        assert!(res.is_ok());

        builder.build().unwrap()
    };
    op_ctx.add_custom_operation(0, helper_op);

    // now see what happens if we try to run this in a builder
    let mut builder = OperationBuilder::new(&op_ctx, 1);
    builder
        .expect_parameter_node("p0", NodeType::Integer)
        .unwrap();
    let p0 = AbstractNodeId::param("p0");
    builder.expect_context_node("c0", NodeType::String).unwrap();
    let c0 = AbstractNodeId::param("c0");
    builder
        .expect_parameter_edge("p0", "c0", EdgeType::Exact("child".to_string()))
        .unwrap();
    let state_before = builder.show_state().unwrap();
    builder
        .add_named_operation(
            "helper".into(),
            BuilderOpLike::FromOperationId(0),
            vec![p0.clone()],
        )
        .unwrap();
    let state_after = builder.show_state().unwrap();
    let aids_before = state_before
        .node_keys_to_aid
        .right_values()
        .collect::<HashSet<_>>();
    let aids_after = state_after
        .node_keys_to_aid
        .right_values()
        .collect::<HashSet<_>>();
    // UPDATE: we're allowed to return shape query nodes now, so the number of nodes should change by one
    // assert_eq!(
    //     aids_before, aids_after,
    //     "Expected no new nodes to be created in the graph"
    // );
    assert_eq!(
        aids_after.len(),
        aids_before.len() + 1,
        "Expected a new node to be created in the graph"
    );

    if true {
        // NOTE: this only exhibits the desired crash if the problem this test is checking against is not fixed.
        // UPDATE: indeed ^ is fixed, since the shape query will not match against a 'visible' node. So the problem is fixed, and we can run this without crashes

        // for fun, see what happens when we delete the returned node and then try to use c0
        let returned_node = AbstractNodeId::DynamicOutputMarker("helper".into(), "child".into());
        builder
            .add_operation(
                BuilderOpLike::Builtin(TestOperation::DeleteNode),
                vec![returned_node],
            )
            .unwrap();
        // now use c0 to copy from c0 to p0
        // note: this is the operation that would crash (the concrete graph would not have the node) if we were allowed to return the node.
        builder
            .add_operation(
                BuilderOpLike::Builtin(TestOperation::CopyValueFromTo),
                vec![c0, p0],
            )
            .unwrap();
        let operation = builder.build().unwrap();
        op_ctx.add_custom_operation(1, operation);

        let mut concrete_graph = ConcreteGraph::<TestSemantics>::new();
        let p0_key = concrete_graph.add_node(NodeValue::Integer(0));
        let c0_key = concrete_graph.add_node(NodeValue::String("context".to_string()));
        concrete_graph.add_edge(p0_key, c0_key, "child".to_string());

        // crash, CopyValueFromTo doesn't find substmarker 0.
        run_from_concrete(&mut concrete_graph, &op_ctx, 1, &[p0_key]).unwrap();
    }
}

// Test that the full matrix of: [node types, edge types] x [set, delete, new] works as expected.
// In particular, set and delete should propagate information about the new type into the caller operation's signature.

#[test]
fn builder_infers_correct_signatures() {
    let param_instructions = |builder: &mut OperationBuilder<TestSemantics>| {
        builder
            .expect_parameter_node("p0", NodeType::Integer)
            .unwrap();
        builder
            .expect_parameter_node("p1", NodeType::Integer)
            .unwrap();
        builder
            .expect_parameter_node("p2", NodeType::Integer)
            .unwrap();
        builder.expect_context_node("c0", NodeType::Object).unwrap();
        builder.expect_context_node("c1", NodeType::Object).unwrap();
        builder
            .expect_parameter_edge("p0", "c0", EdgeType::Wildcard)
            .unwrap();
        builder
            .expect_parameter_edge("p2", "c1", EdgeType::Wildcard)
            .unwrap();
        builder
            .expect_parameter_edge("p0", "c1", EdgeType::Wildcard)
            .unwrap();
    };

    let mut op_ctx = OperationContext::<TestSemantics>::new();
    let mut builder = OperationBuilder::new(&op_ctx, 0);
    param_instructions(&mut builder);
    // param: p0->c0, p1, p2->c1, p0->c1
    // delete p1, delete c0 (which implies deletion of edge p0->c0), set p0, delete edge p2->c1, set c1, set p0->c1
    // and create new node n0 to return, and new edge p0->c1 to return.
    // new node n1 to not return, and new edge p0->n1 to not return.

    let p0 = AbstractNodeId::ParameterMarker("p0".into());
    let p1 = AbstractNodeId::ParameterMarker("p1".into());
    let p2 = AbstractNodeId::ParameterMarker("p2".into());
    let c0 = AbstractNodeId::ParameterMarker("c0".into());
    let c1 = AbstractNodeId::ParameterMarker("c1".into());
    let n0 = AbstractNodeId::DynamicOutputMarker("new".into(), "new".into());
    let n1 = AbstractNodeId::DynamicOutputMarker("new1".into(), "new".into());

    // delete p1
    builder
        .add_operation(BuilderOpLike::Builtin(TestOperation::DeleteNode), vec![p1])
        .unwrap();
    // delete c0
    builder
        .add_operation(BuilderOpLike::Builtin(TestOperation::DeleteNode), vec![c0])
        .unwrap();
    // set p0 to Integer (i.e., no change - this must still be visible!)
    builder
        .add_operation(
            BuilderOpLike::Builtin(TestOperation::SetTo {
                op_typ: NodeType::Object,
                target_typ: NodeType::Integer,
                value: NodeValue::Integer(0),
            }),
            vec![p0],
        )
        .unwrap();
    // delete edge p2->c1
    builder
        .add_operation(
            BuilderOpLike::Builtin(TestOperation::DeleteEdge),
            vec![p2, c1],
        )
        .unwrap();
    // set c1 to String (i.e., subtype of Object - this must still be visible!)
    builder
        .add_operation(
            BuilderOpLike::Builtin(TestOperation::SetTo {
                op_typ: NodeType::Object,
                target_typ: NodeType::String,
                value: NodeValue::String("context".to_string()),
            }),
            vec![c1],
        )
        .unwrap();
    // set edge p0->c1 to 'p0->c1' (i.e., subtype of Wildcard)
    builder
        .add_operation(
            BuilderOpLike::Builtin(TestOperation::SetEdgeTo {
                node_typ: NodeType::Object,
                param_typ: EdgeType::Wildcard,
                target_typ: EdgeType::Exact("p0->c1".to_string()),
                value: "p0->c1".to_string(),
            }),
            vec![p0, c1],
        )
        .unwrap();
    // create new node n0
    builder
        .add_named_operation(
            "new".into(),
            BuilderOpLike::Builtin(TestOperation::AddNode {
                node_type: NodeType::String,
                value: NodeValue::String("new".to_string()),
            }),
            vec![],
        )
        .unwrap();
    // create new edge p0->c1
    builder
        .add_operation(
            BuilderOpLike::Builtin(TestOperation::AddEdge {
                node_typ: NodeType::Object,
                param_typ: EdgeType::Wildcard,
                target_typ: EdgeType::Exact("new_edge".to_string()),
                value: "new_edge".to_string(),
            }),
            vec![p0, c1],
        )
        .unwrap();
    // create new non-returned node n1
    builder
        .add_named_operation(
            "new1".into(),
            BuilderOpLike::Builtin(TestOperation::AddNode {
                node_type: NodeType::Integer,
                value: NodeValue::Integer(42),
            }),
            vec![],
        )
        .unwrap();
    // create new non-returned edge p0->n1
    builder
        .add_operation(
            BuilderOpLike::Builtin(TestOperation::AddEdge {
                node_typ: NodeType::Object,
                param_typ: EdgeType::Wildcard,
                target_typ: EdgeType::Exact("new_edge1".to_string()),
                value: "new_edge1".to_string(),
            }),
            vec![p0, n1],
        )
        .unwrap();
    // return n0
    builder
        .return_node(n0.clone(), "new".into(), NodeType::String)
        .unwrap();
    // return p0->c1 edge
    builder
        .return_edge(p0, c1, EdgeType::Exact("new_edge".to_string()))
        .unwrap();
    // try to return p0->n1 edge, which should fail because n1 is not returned
    let res = builder.return_edge(p0, n1, EdgeType::Exact("new_edge1".to_string()));
    assert!(
        res.is_err(),
        "Expected returning edge p0->n1 to fail because n1 is not returned"
    );
    let operation = builder.build().unwrap();
    // get signature
    let signature = operation.signature();

    // assert our desired changes
    // number of explicit params
    assert_eq!(
        signature.parameter.explicit_input_nodes.len(),
        3,
        "Expected 3 explicit input nodes, p0, p1, p2"
    );
    // new nodes and edges
    assert_eq!(
        &signature.output.new_nodes,
        &HashMap::from([("new".into(), NodeType::String)]),
        "Expected new node 'new' of type String"
    );
    assert_eq!(
        &signature.output.new_edges,
        &HashMap::from([(
            (
                SubstMarker::from("p0").into(),
                SubstMarker::from("c1").into()
            ),
            EdgeType::Exact("new_edge".to_string()),
        )]),
        "Expected new edge from p0 to c1 of type 'new_edge'"
    );
    macro_rules! assert_deleted_and_changed_nodes_and_edges {
        ($signature:expr, $expected_maybe_changed_nodes:expr) => {
            // deleted nodes and edges
            assert_eq!(
                &$signature.output.maybe_deleted_nodes,
                &HashSet::from([
                    SubstMarker::from("p1").into(),
                    SubstMarker::from("c0").into()
                ]),
                "Expected nodes p1 and c0 to be deleted"
            );
            assert_eq!(
                &$signature.output.maybe_deleted_edges,
                &HashSet::from([
                    (
                        SubstMarker::from("p2").into(),
                        SubstMarker::from("c1").into()
                    ),
                    (
                        SubstMarker::from("p0").into(),
                        SubstMarker::from("c0").into()
                    )
                ]),
                "Expected edges p2->c1 and p0->c0 to be deleted"
            );
            // changed nodes and edges
            assert_eq!(
                &$signature.output.maybe_changed_nodes, &$expected_maybe_changed_nodes,
                "Expected nodes p0 to be changed to Integer and c1 to String"
            );
            assert_eq!(
                &$signature.output.maybe_changed_edges,
                &HashMap::from([(
                    (
                        SubstMarker::from("p0").into(),
                        SubstMarker::from("c1").into()
                    ),
                    EdgeType::Exact("p0->c1".to_string())
                )]),
                "Expected edge p0->c1 to be changed to 'new_edge'"
            );
        };
    }
    assert_deleted_and_changed_nodes_and_edges!(
        signature,
        HashMap::from([
            (SubstMarker::from("p0").into(), NodeType::Integer),
            (SubstMarker::from("c1").into(), NodeType::String)
        ])
    );

    // Now ensure the same changes (minus the newly added nodes and edges) are propagated to another operation
    // that calls this operation.

    op_ctx.add_custom_operation(0, operation);
    let mut builder = OperationBuilder::new(&op_ctx, 1);
    // same parameter graph so we can call the other operation
    param_instructions(&mut builder);

    // now call the other operation
    builder
        .add_operation(BuilderOpLike::FromOperationId(0), vec![p0, p1, p2])
        .unwrap();
    let operation = builder.build().unwrap();
    let signature = operation.signature();
    // assert changes and deletions
    // note that the expected node changes are different for c1, since
    assert_deleted_and_changed_nodes_and_edges!(
        signature,
        HashMap::from([
            (SubstMarker::from("p0").into(), NodeType::Integer),
            (SubstMarker::from("c1").into(), NodeType::String)
        ])
    );
}

// TODO: add tests for:
//  * shape queries not being allowed to match already-matched nodes
//  * recursion abstract changes

macro_rules! recursion_signature_is_sound {
    (before) => {
        // when we change the abstract value of the node _before_ the recursive call
        recursion_signature_is_sound!(true, false, false, NodeType::Integer, NodeType::Integer);
    };
    (after) => {
        // when we change the abstract value of the node _after_ the recursive call
        recursion_signature_is_sound!(false, true, false, NodeType::Integer, NodeType::Integer);
    };
    ($fst:literal, $snd:literal, $set_last_to_string:literal, $p0_typ:expr, $c0_typ:expr) => {
        let op_ctx = OperationContext::<TestSemantics>::new();
        let mut builder = OperationBuilder::new(&op_ctx, 0);
        // the operation we're designing takes p0->c0, the start of a linked list, and sets all nodes (except the last node) to Integer.
        // it does the "except the last node" check by first seeing if there is a child, and only then recursing.
        builder
            .expect_parameter_node("p0", NodeType::Object)
            .unwrap();
        builder.expect_context_node("c0", NodeType::Object).unwrap();
        builder
            .expect_parameter_edge("p0", "c0", EdgeType::Wildcard)
            .unwrap();
        let p0 = AbstractNodeId::ParameterMarker("p0".into());
        let c0 = AbstractNodeId::ParameterMarker("c0".into());
        if $fst {
            builder
                .add_operation(
                    BuilderOpLike::Builtin(TestOperation::SetTo {
                        op_typ: NodeType::Object,
                        target_typ: NodeType::Integer,
                        value: NodeValue::Integer(0),
                    }),
                    vec![p0],
                )
                .unwrap();
        }
        builder.start_shape_query("q").unwrap();
        builder
            .expect_shape_node("child".into(), NodeType::Object)
            .unwrap();
        let child_aid = AbstractNodeId::dynamic_output("q", "child");
        builder
            .expect_shape_edge(c0.clone(), child_aid.clone(), EdgeType::Wildcard)
            .unwrap();
        builder.enter_true_branch().unwrap();
        // if we have a child, recurse
        builder
            .add_named_operation(
                "recurse".into(),
                BuilderOpLike::Recurse,
                vec![c0], // only need to select c0: child_aid should be matched by context.
            )
            .unwrap();
        if $set_last_to_string {
            builder.enter_false_branch().unwrap();
            // if we don't have a child, set the last node to String
            builder
                .add_operation(
                    BuilderOpLike::Builtin(TestOperation::SetTo {
                        op_typ: NodeType::Object,
                        target_typ: NodeType::String,
                        value: NodeValue::String("Last".to_string()),
                    }),
                    vec![c0.clone()],
                )
                .unwrap();
        }
        builder.end_query().unwrap();
        if $snd {
            builder
                .add_operation(
                    BuilderOpLike::Builtin(TestOperation::SetTo {
                        op_typ: NodeType::Object,
                        target_typ: NodeType::Integer,
                        value: NodeValue::Integer(0),
                    }),
                    vec![p0],
                )
                .unwrap();
        }

        let operation = builder.build().unwrap();
        let signature = operation.signature();
        // assert that the signature is correct
        assert_eq!(
            signature.output.maybe_deleted_nodes,
            HashSet::new(),
            "Expected no nodes to be deleted"
        );
        assert_eq!(
            signature.output.maybe_deleted_edges,
            HashSet::new(),
            "Expected no edges to be deleted"
        );
        assert_eq!(
            signature.output.maybe_changed_nodes,
            HashMap::from([
                (SubstMarker::from("p0").into(), $p0_typ),
                (SubstMarker::from("c0").into(), $c0_typ), // Note: c0 also changed due to the recursive call.
            ]),
            "Expected both p0 and c0 to change"
        );
        assert_eq!(
            signature.output.maybe_changed_edges,
            HashMap::new(),
            "Expected no edges to be changed"
        );
        assert_eq!(
            signature.output.new_nodes,
            HashMap::new(),
            "Expected no new nodes to be created"
        );
        assert_eq!(
            signature.output.new_edges,
            HashMap::new(),
            "Expected no new edges to be created"
        );
    };
}

#[test_log::test]
fn recursion_signature_is_sound_when_changed_before() {
    // if we do changes and then recurse, those are correctly communicated to caller operations via the signature.
    recursion_signature_is_sound!(before);
}

#[test_log::test]
fn recursion_signature_is_sound_when_changed_after() {
    // if we recurse and then do changes, those are correctly communicated to caller operations via the signature.
    recursion_signature_is_sound!(after);
    // Note: this test passes because we recalculate the signature at the very end, *and then use it for calculating the recurse call's effects*!
}

#[test_log::test]
fn recursion_signature_is_sound_when_changed_before_and_last_node_set_to_string() {
    // since c0 may or may not be the last node, the system has no choice but to infer a common supertype.
    recursion_signature_is_sound!(true, false, true, NodeType::Integer, NodeType::Object);
}

#[test_log::test]
fn recursion_signature_is_sound_when_changed_after_and_last_node_set_to_string() {
    // since c0 may or may not be the last node, the system has no choice but to infer a common supertype.
    recursion_signature_is_sound!(false, true, true, NodeType::Integer, NodeType::Object);
}

// TODO: add test for recursion that matches differently based on future changes. See the excalidraws.

#[test_log::test]
fn recursion_breaks_when_modification_changes_after_use() {
    let op_ctx = OperationContext::<TestSemantics>::new();
    let mut builder = OperationBuilder::new(&op_ctx, 0);
    // we're writing a recursive operation
    builder
        .expect_parameter_node("p0", NodeType::Integer)
        .unwrap();
    let p0 = AbstractNodeId::param("p0");
    // recurse on p0
    builder
        .add_operation(BuilderOpLike::Recurse, vec![p0])
        .unwrap();
    // expect it to be an integer
    builder
        .add_operation(
            BuilderOpLike::Builtin(TestOperation::SetTo {
                op_typ: NodeType::Integer, // This enforces that the input type is integer
                target_typ: NodeType::Integer, // no-op
                value: NodeValue::Integer(42),
            }),
            vec![p0],
        )
        .unwrap();
    // now, change the abstract value of p0 to String
    let res = builder.add_operation(
        BuilderOpLike::Builtin(TestOperation::SetTo {
            op_typ: NodeType::Object,
            target_typ: NodeType::String,
            value: NodeValue::String("Changed".to_string()),
        }),
        vec![p0],
    );
    assert!(
        res.is_err(),
        "Expected changing the abstract value of p0 to fail, since it is used in a recursive call after which it is used as an integer"
    );
}

#[test_log::test]
fn shape_query_doesnt_match_nodes_for_which_handles_exist() {
    // TODO: make this more lenient. See problems-testcases.md to support eg read-only shape queries.

    // If an outer operation already has a handle to a specific concrete node (checked dynamically),
    // then a shape query cannot match that node.

    fn get_shape_query_modifying_operation(
        op_id: OperationId,
    ) -> UserDefinedOperation<TestSemantics> {
        let op_ctx = OperationContext::<TestSemantics>::new();
        let mut builder = OperationBuilder::new(&op_ctx, op_id);
        builder
            .expect_parameter_node("p0", NodeType::Object)
            .unwrap();
        let p0 = AbstractNodeId::param("p0");
        // start a shape query for a child.
        builder.start_shape_query("q").unwrap();
        builder
            .expect_shape_node("child".into(), NodeType::Object)
            .unwrap();
        let child_aid = AbstractNodeId::dynamic_output("q", "child");
        builder
            .expect_shape_edge(p0.clone(), child_aid.clone(), EdgeType::Wildcard)
            .unwrap();
        builder.enter_true_branch().unwrap();
        // if we have a child, set it to "I'm a string"
        // TODO: once we support read-only shape queries, add a second test that replaces this SetTo with a CopyTo, and then assert that it is matched.
        builder
            .add_operation(
                BuilderOpLike::Builtin(TestOperation::SetTo {
                    op_typ: NodeType::Object,
                    target_typ: NodeType::String,
                    value: NodeValue::String("I'm a string".to_string()),
                }),
                vec![child_aid],
            )
            .unwrap();
        builder.enter_false_branch().unwrap();
        // if we don't, set p0 to "no child"
        builder
            .add_operation(
                BuilderOpLike::Builtin(TestOperation::SetTo {
                    op_typ: NodeType::Object,
                    target_typ: NodeType::String,
                    value: NodeValue::String("no child".to_string()),
                }),
                vec![p0],
            )
            .unwrap();

        builder.build().unwrap()
    }

    let mut op_ctx = OperationContext::<TestSemantics>::new();
    op_ctx.add_custom_operation(0, get_shape_query_modifying_operation(0));
    let mut builder = OperationBuilder::new(&op_ctx, 1);
    builder
        .expect_parameter_node("p0", NodeType::Object)
        .unwrap();
    builder
        .expect_context_node("c0", NodeType::Integer)
        .unwrap();
    let p0 = AbstractNodeId::param("p0");
    let c0 = AbstractNodeId::param("c0");
    builder
        .expect_parameter_edge("p0", "c0", EdgeType::Wildcard)
        .unwrap();
    // call op 0
    builder
        .add_operation(BuilderOpLike::FromOperationId(0), vec![p0])
        .unwrap();
    let state = builder.show_state().unwrap();
    // c0 should still be Integer, since the operation does not know about the inner operation's shape query.
    let c0_key = state.node_keys_to_aid.get_right(&c0).unwrap();
    assert_eq!(
        state.graph.get_node_attr(*c0_key).unwrap(),
        &NodeType::Integer,
        "Expected c0 to remain unchanged, since the operation does not know about the inner operation's shape query"
    );

    let op = builder.build().unwrap();
    op_ctx.add_custom_operation(1, op);

    // now run the operation with a concrete graph
    {
        // in the concrete:
        // check that no child leads to the node being set to "no child"
        let mut g_no_child = TestSemantics::new_concrete_graph();
        let p0_key = g_no_child.add_node(NodeValue::Integer(42));
        run_from_concrete(&mut g_no_child, &op_ctx, 0, &[p0_key]).unwrap();
        let p0_value = g_no_child.get_node_attr(p0_key).unwrap();
        assert_eq!(
            p0_value,
            &NodeValue::String("no child".to_string()),
            "Expected p0 to be set to 'no child' when no child exists"
        );
    }
    {
        // in the concrete:
        // check that a node with a child leads to the child being set to "I'm a string"
        let mut g_with_child = TestSemantics::new_concrete_graph();
        let p0_key = g_with_child.add_node(NodeValue::Integer(42));
        let c0_key = g_with_child.add_node(NodeValue::Integer(43));
        g_with_child.add_edge(p0_key, c0_key, "child".to_string());
        run_from_concrete(&mut g_with_child, &op_ctx, 0, &[p0_key]).unwrap();
        let p0_value = g_with_child.get_node_attr(p0_key).unwrap();
        let c0_value = g_with_child.get_node_attr(c0_key).unwrap();
        assert_eq!(
            p0_value,
            &NodeValue::Integer(42),
            "Expected p0 to remain unchanged when a child exists"
        );
        assert_eq!(
            c0_value,
            &NodeValue::String("I'm a string".to_string()),
            "Expected child to be set to 'I'm a string' when it exists"
        );
    }
    {
        // in the abstract, i.e., with the outer operation active and having a handle to the child node:
        let mut g = TestSemantics::new_concrete_graph();
        let p0_key = g.add_node(NodeValue::Integer(42));
        let c0_key = g.add_node(NodeValue::Integer(43));
        g.add_edge(p0_key, c0_key, "child".to_string());
        run_from_concrete(&mut g, &op_ctx, 1, &[p0_key]).unwrap();
        let p0_value = g.get_node_attr(p0_key).unwrap();
        let c0_value = g.get_node_attr(c0_key).unwrap();
        // despite p0 having a child, the shape query should not match it.
        // otherwise, the abstract information from the outer operation is unsound.
        assert_eq!(
            p0_value,
            &NodeValue::String("no child".to_string()),
            "Expected p0 to be set to 'no child' when the shape query does not match the child node, even if one exists"
        );
        assert_eq!(
            c0_value,
            &NodeValue::Integer(43),
            "Expected child to remain unchanged since it is not matched, even though it exists",
        );
    }
}

// TODO: add "forget node" instruction that can be used to make sure that shape queries can match and modify
//  the forgotten nodes.

#[test_log::test]
fn may_writes_remember_previous_abstract_value() {
    // The semantics of our abstract output changes for the "changed_*" cases is that those are writes that *may have happened*.
    // Importantly, they may not have happened too.
    // So, we cannot *overwrite* the current abstract value with the result of a write that only *may* have happened, we need to consider the case that no write happened.
    // The builder itself must also be aware of this and not overapproximate to the point of uselessness.

    let mut op_ctx = OperationContext::<TestSemantics>::new();

    let op = {
        let mut builder = OperationBuilder::new(&op_ctx, 0);
        // an operation that takes a p0: Object and changes it to a String.
        // TODO: we could loosen the constraints and make it so that a known, unconditional change, even in a user defined op, leads to unconditional changes in the caller.
        //  but at the moment, any changes in UDOs are considered "may" changes.
        // in other words, the query below is actually not necessary.
        builder
            .expect_parameter_node("p0", NodeType::Object)
            .unwrap();
        let p0 = AbstractNodeId::param("p0");
        // note: query only necessary to actually break type safety in the concrete.
        builder
            .start_query(TestQuery::ValueEqualTo(NodeValue::Integer(3)), vec![p0])
            .unwrap();
        builder.enter_true_branch().unwrap();
        builder
            .add_operation(
                BuilderOpLike::LibBuiltin(LibBuiltinOperation::SetNode {
                    param: NodeType::Object,
                    value: NodeValue::String("Changed".to_string()),
                }),
                vec![p0.clone()],
            )
            .unwrap();
        builder.end_query().unwrap();
        let state = builder.show_state().unwrap();
        // note that we still expect p0 to be object, it's just that we tell our caller that it may have changed to String.
        let typ = state.node_av_of_aid(&p0).unwrap();
        assert_eq!(
            typ,
            &NodeType::Object,
            "Expected p0 to remain Object after conditional change to String"
        );
        builder.build().unwrap()
    };
    op_ctx.add_custom_operation(0, op);

    let mut builder = OperationBuilder::new(&op_ctx, 1);
    builder
        .expect_parameter_node("p0", NodeType::Object)
        .unwrap();
    let p0 = AbstractNodeId::param("p0");
    // a builtin op that changes p0 to Integer should unconditionally change the abstract value of p0 to Integer.
    builder
        .add_operation(
            BuilderOpLike::LibBuiltin(LibBuiltinOperation::SetNode {
                param: NodeType::Object,
                value: NodeValue::Integer(42),
            }),
            vec![p0],
        )
        .unwrap();
    let state = builder.show_state().unwrap();
    let typ = state.node_av_of_aid(&p0).unwrap();
    assert_eq!(
        typ,
        &NodeType::Integer,
        "Expected p0 to be Integer after unconditional, builtin SetNode"
    );

    // a custom op that conditionally (i.e., *may*) changes p0 to String should change the abstract value of p0 to the join of its previous value (Integer) and String, so Object.
    builder
        .add_operation(BuilderOpLike::FromOperationId(0), vec![p0])
        .unwrap();
    let state = builder.show_state().unwrap();
    let typ = state.node_av_of_aid(&p0).unwrap();
    assert_eq!(
        typ,
        &NodeType::Object,
        "Expected p0 to be Object after conditional, user-defined SetNode"
    );

    let op = builder.build().unwrap();
    op_ctx.add_custom_operation(1, op);

    // See here for broken type safety:
    if true {
        // if false {
        let mut g = TestSemantics::new_concrete_graph();
        let p0_key = g.add_node(NodeValue::Integer(0));
        // this won't hit the inner operation's set to string operation, so it would remain integer.
        run_from_concrete(&mut g, &op_ctx, 1, &[p0_key]).unwrap();
        let p0_value = g.get_node_attr(p0_key).unwrap();
        assert_eq!(
            p0_value,
            &NodeValue::Integer(42),
            "Expected p0 to remain Integer after running the operation, since the inner operation's set to String was not hit"
        );
    }

    // furthermore, just because a operation _may_ change a node, it doesn't unnecessarily make the caller's av less precise.
    let mut builder = OperationBuilder::new(&op_ctx, 2);
    builder
        .expect_parameter_node("p0", NodeType::String)
        .unwrap();
    // we expect a String
    builder
        .add_operation(BuilderOpLike::FromOperationId(0), vec![p0])
        .unwrap();
    let state = builder.show_state().unwrap();
    let typ = state.node_av_of_aid(&p0).unwrap();
    assert_eq!(
        typ,
        &NodeType::String,
        "Expected p0 to remain String after running the operation, since the inner operation let us now it may at most write a string."
    );
}

// Test that shape queries have the entire connected component as context - i.e., they cannot match anything that already exists in the abstract graph.
#[test_log::test]
fn shape_query_cannot_match_existing_nodes() {
    let mut op_ctx = OperationContext::<TestSemantics>::new();
    let mut builder = OperationBuilder::new(&op_ctx, 0);
    // we expect a p0: Object -child-> p1: Object
    builder
        .expect_parameter_node("p0", NodeType::Object)
        .unwrap();
    builder
        .expect_parameter_node("p1", NodeType::Object)
        .unwrap();
    let p0 = AbstractNodeId::param("p0");
    let p1 = AbstractNodeId::param("p1");
    builder
        .expect_parameter_edge("p0", "p1", EdgeType::Exact("child".into()))
        .unwrap();
    // start a shape query for a child.
    builder.start_shape_query("q").unwrap();
    builder
        .expect_shape_node("child".into(), NodeType::Object)
        .unwrap();
    let child_aid = AbstractNodeId::dynamic_output("q", "child");
    builder
        .expect_shape_edge(p0, child_aid, EdgeType::Exact("child".into()))
        .unwrap();
    builder.enter_true_branch().unwrap();
    // now delete child
    builder
        .add_operation(
            BuilderOpLike::LibBuiltin(LibBuiltinOperation::RemoveNode {
                param: NodeType::Object,
            }),
            vec![child_aid],
        )
        .unwrap();
    builder.end_query().unwrap();
    // we expect that p1 was not matched, hence it must still exist
    let state = builder.show_state().unwrap();
    let p1_av = state.node_av_of_aid(&p1);
    assert_eq!(
        p1_av,
        Some(&NodeType::Object),
        "Expected p1 to remain Object after running the shape query, since it was not matched"
    );
    // finish
    let op = builder.build().unwrap();
    op_ctx.add_custom_operation(0, op);

    // run on concrete graph.
    let mut g = TestSemantics::new_concrete_graph();
    let p0_key = g.add_node(NodeValue::Integer(0));
    let p1_key = g.add_node(NodeValue::Integer(1));
    g.add_edge(p0_key, p1_key, "child".into());

    run_from_concrete(&mut g, &op_ctx, 0, &[p0_key, p1_key]).unwrap();
    // assert that p1 still exists
    assert_eq!(
        g.get_node_attr(p1_key),
        Some(&NodeValue::Integer(1)),
        "Expected p1 to remain a node in the graph",
    );
}

#[test_log::test]
fn rename_nodes_and_merge_test() {
    let op_ctx = OperationContext::<TestSemantics>::new();
    let mut builder = OperationBuilder::new(&op_ctx, 0);
    builder
        .expect_parameter_node("p0", NodeType::Object)
        .unwrap();
    let p0 = AbstractNodeId::param("p0");
    // assert that we cannot rename a param
    let res = builder.rename_node(p0, "test");
    assert!(
        res.is_err(),
        "Expected to not be able to rename a parameter marker"
    );

    // first branches: merge two nodes with same output marker, but different operation marker
    builder
        .start_query(TestQuery::ValueEqualTo(NodeValue::Integer(0)), vec![p0])
        .unwrap();
    builder.enter_true_branch().unwrap();
    builder
        .add_named_operation(
            "op1".into(),
            BuilderOpLike::LibBuiltin(LibBuiltinOperation::AddNode {
                value: NodeValue::String("hello".into()),
            }),
            vec![],
        )
        .unwrap();
    let true_branch_aid = AbstractNodeId::dynamic_output("op1", "new");
    builder.rename_node(true_branch_aid, "a").unwrap();
    builder.enter_false_branch().unwrap();
    builder
        .add_named_operation(
            "op2".into(),
            BuilderOpLike::LibBuiltin(LibBuiltinOperation::AddNode {
                value: NodeValue::Integer(0),
            }),
            vec![],
        )
        .unwrap();
    let false_branch_aid = AbstractNodeId::dynamic_output("op2", "new");
    builder.rename_node(false_branch_aid, "a").unwrap();
    builder.end_query().unwrap();
    let a_aid = AbstractNodeId::named("a");
    // assert that the node "a" is now a merge of the two nodes
    let state = builder.show_state().unwrap();
    let a_av = state.node_av_of_aid(&a_aid);
    assert_eq!(
        a_av,
        Some(&NodeType::Object),
        "Expected node 'a' to be a merge of the two nodes with the same name, but different operation markers"
    );

    // second branches: shape queries with same operation marker, but different output markers
    // also assert that we cannot return these
    builder.start_shape_query("query").unwrap();
    builder
        .expect_shape_node("wow".into(), NodeType::Integer)
        .unwrap();
    let wow_aid = AbstractNodeId::dynamic_output("query", "wow");
    builder
        .expect_shape_edge(p0, wow_aid, EdgeType::Wildcard)
        .unwrap();
    builder.enter_true_branch().unwrap();
    builder.rename_node(wow_aid, "b").unwrap();
    builder.enter_false_branch().unwrap();
    // in false branch create a new node ("wow", "new")
    builder
        .add_named_operation(
            "wow".into(),
            BuilderOpLike::LibBuiltin(LibBuiltinOperation::AddNode {
                value: NodeValue::String("Something".into()),
            }),
            vec![],
        )
        .unwrap();
    let false_branch_aid = AbstractNodeId::dynamic_output("wow", "new");
    builder.rename_node(false_branch_aid, "b").unwrap();
    builder.end_query().unwrap();
    let b_aid = AbstractNodeId::named("b");
    // assert that the node "b" is now a merge of the two nodes
    let state = builder.show_state().unwrap();
    let b_av = state.node_av_of_aid(&b_aid);
    assert_eq!(
        b_av,
        Some(&NodeType::Object),
        "Expected node 'b' to be a merge of the two nodes with the same operation marker, but different output markers"
    );
    // assert that we cannot return these nodes
    // UPDATE: we're allowed to return shape query nodes now
    let res = builder.return_node(b_aid.clone(), "b".into(), NodeType::Object);
    // assert!(
    //     res.is_err(),
    //     "Expected to not be able to return a renamed node that partially comes from a shape query",
    // );
    assert!(res.is_ok());
}

#[test_log::test]
fn shape_query_allows_refinement_of_existing_nodes_and_edges() {
    let mut op_ctx = OperationContext::<TestSemantics>::new();
    let mut builder = OperationBuilder::new(&op_ctx, 0);
    // we expect a p0: Object -*-> p1: Object
    builder
        .expect_parameter_node("p0", NodeType::Object)
        .unwrap();
    builder
        .expect_parameter_node("p1", NodeType::Object)
        .unwrap();
    let p0 = AbstractNodeId::param("p0");
    let p1 = AbstractNodeId::param("p1");
    builder
        .expect_parameter_edge("p0", "p1", EdgeType::Wildcard)
        .unwrap();

    // first: start a shape query to check if p0->p1 has type "child"
    builder.start_shape_query("q").unwrap();
    builder
        .expect_shape_edge(p0, p1, EdgeType::Exact("child".to_string()))
        .unwrap();
    builder.enter_true_branch().unwrap();
    // if it does, set p0 to "has child"
    builder
        .add_operation(
            BuilderOpLike::LibBuiltin(LibBuiltinOperation::SetNode {
                param: NodeType::Object,
                value: NodeValue::String("has child".to_string()),
            }),
            vec![p0.clone()],
        )
        .unwrap();
    builder.end_query().unwrap();

    // also, check if p1 has type Integer.
    builder.start_shape_query("q1").unwrap();
    builder
        .expect_shape_node_change(p1, NodeType::Integer)
        .unwrap();
    builder.enter_true_branch().unwrap();
    // if it does, set p1 to "was integer"
    builder
        .add_operation(
            BuilderOpLike::LibBuiltin(LibBuiltinOperation::SetNode {
                param: NodeType::Object,
                value: NodeValue::String("was integer".to_string()),
            }),
            vec![p1.clone()],
        )
        .unwrap();

    // finalize and test
    let operation = builder.build().unwrap();
    op_ctx.add_custom_operation(0, operation);

    {
        // in the concrete:
        // check that p0->p1 has type "child" and p0 is set to "has child" and p1 is set to "was integer"
        let mut g = TestSemantics::new_concrete_graph();
        let p0_key = g.add_node(NodeValue::Integer(42));
        let p1_key = g.add_node(NodeValue::Integer(43));
        g.add_edge(p0_key, p1_key, "child".to_string());

        run_from_concrete(&mut g, &op_ctx, 0, &[p0_key, p1_key]).unwrap();

        let p0_value = g.get_node_attr(p0_key);
        assert_eq!(p0_value, Some(&NodeValue::String("has child".to_string())));

        let p1_value = g.get_node_attr(p1_key);
        assert_eq!(
            p1_value,
            Some(&NodeValue::String("was integer".to_string()))
        );
    }
}

#[test_log::test]
fn shape_query_av_refinement_works_in_branch_merge() {
    let mut op_ctx = OperationContext::<TestSemantics>::new();
    let mut builder = OperationBuilder::new(&op_ctx, 0);
    // we expect a p0: Object
    builder
        .expect_parameter_node("p0", NodeType::Object)
        .unwrap();
    let p0 = AbstractNodeId::param("p0");
    // check if p0 is actually an Integer
    builder.start_shape_query("q").unwrap();
    builder
        .expect_shape_node_change(p0, NodeType::Integer)
        .unwrap();
    builder.enter_false_branch().unwrap();
    // if not, turn it into an integer
    builder
        .add_operation(
            BuilderOpLike::LibBuiltin(LibBuiltinOperation::SetNode {
                param: NodeType::Object,
                value: NodeValue::Integer(42),
            }),
            vec![p0.clone()],
        )
        .unwrap();
    builder.end_query().unwrap();

    // assert that after the merge we statically know that p0 is an Integer
    let state = builder.show_state().unwrap();
    let p0_av = state.node_av_of_aid(&p0);
    assert_eq!(
        p0_av,
        Some(&NodeType::Integer),
        "Expected p0 to be Integer after the shape query refinement"
    );

    let op = builder.build().unwrap();
    op_ctx.add_custom_operation(0, op);

    // check behavior in concrete
    let mut g = TestSemantics::new_concrete_graph();
    let p0_key = g.add_node(NodeValue::String("not an integer".to_string()));
    run_from_concrete(&mut g, &op_ctx, 0, &[p0_key]).unwrap();
    // assert that p0 is now an integer
    let p0_value = g.get_node_attr(p0_key);
    assert_eq!(
        p0_value,
        Some(&NodeValue::Integer(42)),
        "Expected p0 to be set to Integer after the shape query did not match"
    );

    let p1_key = g.add_node(NodeValue::Integer(100));
    run_from_concrete(&mut g, &op_ctx, 0, &[p1_key]).unwrap();
    // assert that p1 did not change since the true branch does nothing
    let p1_value = g.get_node_attr(p1_key);
    assert_eq!(
        p1_value,
        Some(&NodeValue::Integer(100)),
        "Expected p1 to not change"
    );
}

// Check that deleting a node in an inner op, then calling that on an abstract graph with an edge connected to it results in a signature that
// indicates the edge was also deleted.
#[test_log::test]
fn delete_node_deletes_all_incident_edges_in_signature() {
    let mut op_ctx = OperationContext::<TestSemantics>::new();
    let op_deleting_one_node = {
        let mut builder = OperationBuilder::new(&op_ctx, 0);
        // expect p0: Object
        builder
            .expect_parameter_node("p0", NodeType::Object)
            .unwrap();
        let p0 = AbstractNodeId::param("p0");
        // delete it
        builder
            .add_operation(
                BuilderOpLike::LibBuiltin(LibBuiltinOperation::RemoveNode {
                    param: NodeType::Object,
                }),
                vec![p0],
            )
            .unwrap();
        let op = builder.build().unwrap();
        // assert op is deleting a node
        let signature = op.signature();
        assert_eq!(
            signature.output.maybe_deleted_nodes,
            HashSet::from([SubstMarker::from("p0").into()]),
            "Expected the operation to delete p0"
        );
        op
    };
    op_ctx.add_custom_operation(0, op_deleting_one_node);

    // now call that operation from an operation that expects a p0: Object -child-> p1: Object
    let mut builder = OperationBuilder::new(&op_ctx, 1);
    builder
        .expect_parameter_node("p0", NodeType::Object)
        .unwrap();
    builder
        .expect_parameter_node("p1", NodeType::Object)
        .unwrap();
    let p0 = AbstractNodeId::param("p0");
    let p1 = AbstractNodeId::param("p1");
    builder
        .expect_parameter_edge("p0", "p1", EdgeType::Wildcard)
        .unwrap();
    // call the operation that deletes p1
    builder
        .add_operation(BuilderOpLike::FromOperationId(0), vec![p1])
        .unwrap();
    // assert that the signature indicates that p1 was deleted, and hence the edge p0->p1 was also deleted
    let op = builder.build().unwrap();
    let signature = op.signature();
    assert_eq!(
        signature.output.maybe_deleted_nodes,
        HashSet::from([SubstMarker::from("p1")]),
        "Expected the operation to delete p1"
    );
    assert_eq!(
        signature.output.maybe_deleted_edges,
        HashSet::from([("p0".into(), "p1".into())]),
        "Expected the operation to delete the edge p0->p1"
    );
}

#[test_log::test]
fn delete_node_after_writing_to_it() {
    let op_ctx = OperationContext::<TestSemantics>::new();
    let mut builder = OperationBuilder::new(&op_ctx, 0);
    // expect p0: Object
    builder
        .expect_parameter_node("p0", NodeType::Object)
        .unwrap();
    let p0 = AbstractNodeId::param("p0");
    // write to it
    builder
        .add_operation(
            BuilderOpLike::LibBuiltin(LibBuiltinOperation::SetNode {
                param: NodeType::Object,
                value: NodeValue::String("Hello".to_string()),
            }),
            vec![p0.clone()],
        )
        .unwrap();
    // delete it
    builder
        .add_operation(
            BuilderOpLike::LibBuiltin(LibBuiltinOperation::RemoveNode {
                param: NodeType::Object,
            }),
            vec![p0],
        )
        .unwrap();
    let op = builder.build().unwrap();
    // assert op is deleting a node
    let signature = op.signature();
    assert_eq!(
        signature.output.maybe_deleted_nodes,
        HashSet::from([SubstMarker::from("p0").into()]),
        "Expected the operation to delete p0"
    );
    // TODO: decide if this should be inside the signature or not.
    //  the node has been deleted, so really this is useless information and could confuse consumers.
    //  so add an invariant to opsignature that a node can only be mentioned in either deleted or changed?
    //  could use an enum for that. and make it a hashmap to enforce uniqueness: p0 => Deleted/Changed.
    // TODO: do this!
    assert_eq!(
        signature.output.maybe_changed_nodes,
        HashMap::from([(SubstMarker::from("p0").into(), NodeType::String)]),
        "Expected the operation to change p0 to Object before deleting it"
    );
}

// Just for testing the new builder. TODO: delete.
#[test_log::test]
fn new_builder_test() {
    let op_ctx = OperationContext::<TestSemantics>::new();
    let mut builder = stack_based_builder::Builder::new(&op_ctx, 0);

    use grabapl::operation::builder::BuilderInstruction as BI;

    builder
        .consume(BI::ExpectParameterNode("p0".into(), NodeType::Object))
        .unwrap();

    builder
        .consume(BI::AddNamedOperation(
            "hello".into(),
            BuilderOpLike::LibBuiltin(LibBuiltinOperation::AddNode {
                value: NodeValue::String("Hello".to_string()),
            }),
            vec![],
        ))
        .unwrap();
    let show_data = builder.show();
    println!("{:?}", show_data);

    builder
        .consume(BI::RenameNode(
            AbstractNodeId::dynamic_output("hello", "new"),
            "renamed".into(),
        ))
        .unwrap();
    let show_data = builder.show();
    println!("{:?}", show_data);

    let res = builder.consume(BI::ExpectParameterNode("p0".into(), NodeType::Object));
    println!("{:?}", res);

    // assert!(false);
}

#[test_log::test]
fn recursion_return_node() {
    let op_ctx = OperationContext::<TestSemantics>::new();
    let mut builder = OperationBuilder::new(&op_ctx, 0);
    // we're writing a recursive operation that returns a node.
    builder
        .expect_parameter_node("p0", NodeType::Integer)
        .unwrap();
    let p0 = AbstractNodeId::param("p0");
    // check if p0 is 200, if so, create a new node with value 100 and return it.
    // otherwise, recurse on p0. (note this is a nonsense operation)
    builder
        .start_query(TestQuery::ValueEqualTo(NodeValue::Integer(200)), vec![p0])
        .unwrap();
    builder.enter_true_branch().unwrap();
    builder
        .add_named_operation(
            "op0".into(),
            BuilderOpLike::LibBuiltin(LibBuiltinOperation::AddNode {
                value: NodeValue::Integer(100),
            }),
            vec![],
        )
        .unwrap();
    let new_node_aid = AbstractNodeId::dynamic_output("op0", "new");
    // TODO: unfortunate that we need to specify "ret_node" twice as string.
    // maybe it would be nice to have the rename_node, expect_parameter_node etc ops return an AID?
    let ret_node_aid = AbstractNodeId::named("ret_node");
    builder.rename_node(new_node_aid, "ret_node").unwrap();
    builder.enter_false_branch().unwrap();
    // recurse
    builder
        .add_named_operation("recursion".into(), BuilderOpLike::Recurse, vec![p0])
        .unwrap();
    let new_node_aid = AbstractNodeId::dynamic_output("recursion", "ret_node");
    builder
        .expect_self_return_node("ret_node", NodeType::Integer)
        .unwrap();
    // rename the output node to ret_node
    builder.rename_node(new_node_aid, "ret_node").unwrap();
    builder.end_query().unwrap();
    // return the ret_node
    builder
        .return_node(ret_node_aid, "ret_node".into(), NodeType::Integer)
        .unwrap();

    // TODO: make this work.
    //  Maybe it should be by allowing us to specify expected return nodes somewhere? maybe anywhere?
    //  and then we make sure we consider those:
    //  1. when recursing - we need to create the nodes (pretend they exist)
    //  2. when building - we need to make sure that the return nodes are actually created and returned with the appropriate name and type.

    let _ = builder.build().unwrap();
}

#[test_log::test]
fn recursion_expect_self_return_node_corner_cases() {
    let op_ctx = OperationContext::<TestSemantics>::new();
    let mut builder = OperationBuilder::new(&op_ctx, 0);
    // we're writing a recursive operation that returns a node.
    builder
        .expect_parameter_node("p0", NodeType::Integer)
        .unwrap();
    let p0 = AbstractNodeId::param("p0");
    // recurse on p0
    builder
        .add_named_operation("self".into(), BuilderOpLike::Recurse, vec![p0])
        .unwrap();
    // expect self output to be an integer
    builder
        .expect_self_return_node("ret_node", NodeType::Integer)
        .unwrap();
    let ret_node_aid = AbstractNodeId::dynamic_output("self", "ret_node");
    // Call op that requires integer argument
    builder
        .add_operation(
            BuilderOpLike::Builtin(TestOperation::SetTo {
                op_typ: NodeType::Integer, // This enforces that the input type is integer
                target_typ: NodeType::Integer, // no-op
                value: NodeValue::Integer(42),
            }),
            vec![ret_node_aid],
        )
        .unwrap();

    // now pull a little prank and expect the return node to actually be String
    let res = builder.expect_self_return_node("ret_node", NodeType::String);
    assert!(
        res.is_err(),
        "Expected expecting the return node to be String to fail, since it needs to be Integer in a prior instruction"
    );

    // build without returning
    let res = builder.build();
    assert!(
        res.is_err(),
        "Expected building the operation to fail, since we expect a return node that is not created"
    );
    // now actually return the node
    builder
        .return_node(ret_node_aid, "ret_node".into(), NodeType::Integer)
        .unwrap();
    let _ = builder.build().unwrap();
}