-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial.html
More file actions
1046 lines (894 loc) · 58.5 KB
/
tutorial.html
File metadata and controls
1046 lines (894 loc) · 58.5 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>KATch2 NetKAT Analyzer</title>
<link rel="icon" type="image/png" href="assets/favicon.png">
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
margin: 0;
padding: 20px;
/* background-color: #f4f6f8; */
background-color: #fff;
color: #333;
display: flex;
flex-direction: column;
align-items: center;
}
.container {
background-color: #fff;
padding: 35px;
border-radius: 12px;
/* box-shadow: 0 8px 25px rgba(0,0,0,0.12); */
width: 100%;
max-width: 900px;
display: flex;
flex-direction: column;
/* border: 1px solid rgba(0,0,0,0.05); */
}
h1 {
color: #1a1a1a;
text-align: center;
margin-top: 0;
margin-bottom: 30px;
font-size: 2.2em;
font-weight: 700;
letter-spacing: -0.02em;
}
h1 img {
vertical-align: middle;
margin-right: -50px;
margin-bottom: -10px;
width: 100px;
height: 100px;
}
h2 {
color: #2c3e50;
font-size: 1.6em;
font-weight: 600;
margin-top: 40px;
margin-bottom: 10px;
/* border-bottom: 2px solid #e8f4f8; */
text-align: center;
}
h3 {
color: #34495e;
font-size: 1.3em;
font-weight: 600;
margin-top: 15px;
margin-bottom: 8px;
}
h4 {
color: #7f8c8d;
font-size: 1.1em;
font-weight: 600;
margin-top: 12px;
margin-bottom: 6px;
text-transform: uppercase;
letter-spacing: 0.5px;
font-size: 0.9em;
}
p {
color: #444;
margin-bottom: 12px;
}
code {
background-color: #f8f9fa;
padding: 2px 6px;
border-radius: 4px;
font-family: 'Monaco', 'Menlo', monospace;
font-size: 0.9em;
color: #e83e8c;
border: 1px solid #e9ecef;
}
ul {
line-height: 1.6;
color: #555;
margin-bottom: 12px;
}
li {
margin-bottom: 4px;
}
/* Practice Area Styling */
.practice-section {
margin: 25px 0;
padding: 0;
}
.practice-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 30px;
margin: 15px 0;
}
.examples-column, .editor-column {
padding: 0;
}
.examples-column h3, .editor-column h3 {
margin-top: 0;
margin-bottom: 12px;
color: #343a40;
font-size: 1.2em;
}
.github-link {
text-align: center;
margin-top: 30px;
padding-top: 30px;
border-top: 1px solid #e9ecef;
}
.github-link a {
text-decoration: none;
color: #333;
font-size: 0.9em;
}
.github-link svg {
width: 20px;
height: 20px;
vertical-align: middle;
margin-right: 5px;
fill: currentColor;
}
/* Responsive diagram layout */
.diagram-editor-container {
display: flex;
flex-direction: column;
gap: 20px;
margin: 20px 0;
}
@media (min-width: 900px) {
.diagram-editor-container {
flex-direction: row;
align-items: stretch;
}
.diagram-container {
flex: 0 0 auto;
min-width: 400px;
display: flex;
}
.editor-container {
flex: 1;
min-width: 0;
display: flex;
}
}
.diagram-box {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
gap: 15px;
padding: 20px;
background: #f8f9fa;
border-radius: 6px;
margin-bottom: 15px;
flex: 1;
}
</style>
</head>
<body>
<div class="container">
<h1>
<img src="assets/katch2-logo-black.webp" alt="KATch2 Logo">
KATch<sup style="font-size: 0.6em">2</sup> NetKAT Tutorial
</h1>
<p style="text-align: center">This tutorial will guide you through the basics of NetKAT.</p>
<h2>Packets and Variables</h2>
<p>In NetKAT, a <strong>packet</strong> is represented as a sequence (or vector) of bits. We use variables like <nk>x0</nk>, <nk>x1</nk>, <nk>x2</nk>, and so on, to refer to these bits.
For example, if a packet is <nk>101</nk>, then:</p>
<ul>
<li><nk>x0</nk> would be <nk>1</nk> (the first bit)</li>
<li><nk>x1</nk> would be <nk>0</nk> (the second bit)</li>
<li><nk>x2</nk> would be <nk>1</nk> (the third bit)</li>
</ul>
<h3>Real-World Example: Ethernet Frame</h3>
<p>In real networking, packets have structured fields. Here's a simplified Ethernet frame showing how packet bits correspond to actual network data:</p>
<div style="background-color: #f8f9fa; border: 1px solid #dee2e6; border-radius: 6px; padding: 15px; margin: 15px 0; font-family: monospace; font-size: 13px;">
<div style="margin-bottom: 8px; font-weight: bold; color: #495057;">Ethernet Frame (simplified)</div>
<div style="display: flex; border: 1px solid #adb5bd; border-radius: 3px; overflow: hidden;">
<div style="background-color: #e3f2fd; padding: 8px 12px; border-right: 1px solid #adb5bd; text-align: center; min-width: 80px;">
<div style="font-weight: bold; color: #1976d2;">Dest MAC</div>
<div style="font-size: 11px; color: #666;">48 bits</div>
</div>
<div style="background-color: #f3e5f5; padding: 8px 12px; border-right: 1px solid #adb5bd; text-align: center; min-width: 80px;">
<div style="font-weight: bold; color: #7b1fa2;">Src MAC</div>
<div style="font-size: 11px; color: #666;">48 bits</div>
</div>
<div style="background-color: #fff3e0; padding: 8px 12px; border-right: 1px solid #adb5bd; text-align: center; min-width: 60px;">
<div style="font-weight: bold; color: #f57c00;">EtherType</div>
<div style="font-size: 11px; color: #666;">16 bits</div>
</div>
<div style="background-color: #e8f5e8; padding: 8px 12px; text-align: center; flex: 1;">
<div style="font-weight: bold; color: #388e3c;">Payload</div>
<div style="font-size: 11px; color: #666;">46-1500 bytes</div>
</div>
</div>
<div style="margin-top: 10px; font-size: 12px; color: #666;">
<strong>Bit mapping:</strong> x0=first bit of Dest MAC, x1=second bit of Dest MAC, x2=third bit, etc.
</div>
</div>
<p>In this example:</p>
<ul>
<li><nk>x0-x47</nk> would represent the 48-bit destination MAC address</li>
<li><nk>x48-x95</nk> would represent the 48-bit source MAC address</li>
<li><nk>x96-x111</nk> would represent the 16-bit EtherType field</li>
<li><nk>x112</nk> and beyond would be the start of the payload data, but the payload is typically not modeled in NetKAT</li>
</ul>
<p>NetKAT policies can test or modify any of these bits to implement network behavior like filtering, forwarding, or packet transformation.</p>
<h2>Tests (Packet Filters)</h2>
<p>Tests are conditions that check the value of a packet bit. They act as filters: if the condition is true, the packet passes through; otherwise, it is dropped (or "filtered out").</p>
<div style="background-color: #f8f9fa; border: 1px solid #dee2e6; border-radius: 6px; padding: 20px; margin: 15px 0; font-family: monospace;">
<div style="margin-bottom: 12px; font-weight: bold; color: #495057; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;">Test Filtering: x2 == 1</div>
<div style="display: flex; align-items: center; margin-bottom: 8px;">
<span style="background: #e3f2fd; padding: 4px 8px; border-radius: 3px; margin-right: 8px; min-width: 60px; text-align: center;">101</span>
<span style="margin: 0 8px;">→</span>
<span style="background: #e8f5e8; padding: 4px 8px; border-radius: 3px; color: #2e7d32; margin-right: 8px;">✓ PASS</span>
<span style="margin: 0 8px;">→</span>
<span style="background: #e3f2fd; padding: 4px 8px; border-radius: 3px;">101</span>
</div>
<div style="display: flex; align-items: center; margin-bottom: 8px;">
<span style="background: #e3f2fd; padding: 4px 8px; border-radius: 3px; margin-right: 8px; min-width: 60px; text-align: center;">010</span>
<span style="margin: 0 8px;">→</span>
<span style="background: #ffebee; padding: 4px 8px; border-radius: 3px; color: #c62828; margin-right: 8px;">✗ DROP</span>
<span style="margin: 0 8px;">→</span>
<span style="color: #999; font-style: italic;">(no output)</span>
</div>
<div style="display: flex; align-items: center;">
<span style="background: #e3f2fd; padding: 4px 8px; border-radius: 3px; margin-right: 8px; min-width: 60px; text-align: center;">111</span>
<span style="margin: 0 8px;">→</span>
<span style="background: #e8f5e8; padding: 4px 8px; border-radius: 3px; color: #2e7d32; margin-right: 8px;">✓ PASS</span>
<span style="margin: 0 8px;">→</span>
<span style="background: #e3f2fd; padding: 4px 8px; border-radius: 3px;">111</span>
</div>
</div>
<p style="font-style: italic; color: #666; margin: 10px 0;">Try this example in the interactive editor below:</p>
<netkat example="A test that checks if the third bit (x2) is 1. Try changing program to see how packets are filtered.">// You can edit this to test different programs
x2 == 1
// The analysis results will be displayed here:</netkat>
<p>You can also test for equality to 0, like <nk>x3 == 0</nk>.</p>
<netkat exercise="Write a test that only allows packets where x1 is 0">x1 == 0</netkat>
<p>Note that the examples displayed are as wide as the largest bit mentioned in the program.</p>
<h2>Assignments</h2>
<p>Assignments modify the packet's bits. The expression <nk>x2 := 1</nk> sets the third bit of the packet to 1, regardless of its previous value.</p>
<div style="background-color: #f8f9fa; border: 1px solid #dee2e6; border-radius: 6px; padding: 20px; margin: 15px 0; font-family: monospace;">
<div style="margin-bottom: 12px; font-weight: bold; color: #495057; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;">Assignment Effect: x2 := 1</div>
<div style="display: flex; align-items: center; justify-content: center; gap: 20px;">
<div style="text-align: center;">
<div style="background: #e3f2fd; padding: 8px 12px; border-radius: 4px; margin-bottom: 8px; font-weight: bold; color: #1976d2;">Before</div>
<div style="background: white; border: 2px solid #ccc; border-radius: 4px; padding: 8px;">
<div style="display: flex; gap: 4px; margin-bottom: 4px;">
<span style="padding: 4px 6px; background: #fff3e0; border-radius: 2px;">x0=0</span>
<span style="padding: 4px 6px; background: #fff3e0; border-radius: 2px;">x1=1</span>
<span style="padding: 4px 6px; background: #ffebee; border-radius: 2px;">x2=0</span>
</div>
<div style="text-align: center; font-weight: bold;">010</div>
</div>
</div>
<span style="font-size: 24px; color: #666;">→</span>
<div style="text-align: center;">
<div style="background: #e8f5e8; padding: 8px 12px; border-radius: 4px; margin-bottom: 8px; font-weight: bold; color: #2e7d32;">After</div>
<div style="background: white; border: 2px solid #4caf50; border-radius: 4px; padding: 8px;">
<div style="display: flex; gap: 4px; margin-bottom: 4px;">
<span style="padding: 4px 6px; background: #fff3e0; border-radius: 2px;">x0=0</span>
<span style="padding: 4px 6px; background: #fff3e0; border-radius: 2px;">x1=1</span>
<span style="padding: 4px 6px; background: #e8f5e8; border-radius: 2px; font-weight: bold;">x2=1</span>
</div>
<div style="text-align: center; font-weight: bold;">011</div>
</div>
</div>
</div>
</div>
<p style="font-style: italic; color: #666; margin: 10px 0;">Experiment with this assignment in the editor below:</p>
<netkat example="Sets x2 to 1, regardless of its original value. Watch how only the targeted bit changes in the trace.">x2 := 1</netkat>
<netkat exercise="Set x2 to 0">x2 := 0</netkat>
<h2>Sequential Composition</h2>
<p>Sequential composition, denoted by a semicolon <nk>;</nk>, combines two NetKAT expressions by executing them one after the other. The packet output by the first expression becomes the input to the second.</p>
<div style="background-color: #f8f9fa; border: 1px solid #dee2e6; border-radius: 6px; padding: 20px; margin: 15px 0; font-family: monospace;">
<div style="margin-bottom: 12px; font-weight: bold; color: #495057; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;">Sequential Flow: x2 == 1; x1 := 0</div>
<div style="display: flex; align-items: center; justify-content: center; flex-wrap: wrap; gap: 12px;">
<div style="text-align: center;">
<div style="background: #e3f2fd; padding: 8px 12px; border-radius: 4px; margin-bottom: 4px;">Input</div>
<div style="background: white; padding: 4px 8px; border: 1px solid #ccc; border-radius: 3px;">111</div>
</div>
<span style="font-size: 18px; color: #666;">→</span>
<div style="text-align: center;">
<div style="background: #fff3e0; padding: 8px 12px; border-radius: 4px; margin-bottom: 4px;">Test x2 == 1</div>
<div style="background: #e8f5e8; padding: 4px 8px; border-radius: 3px; color: #2e7d32;">✓ PASS</div>
</div>
<span style="font-size: 18px; color: #666;">→</span>
<div style="text-align: center;">
<div style="background: #f3e5f5; padding: 8px 12px; border-radius: 4px; margin-bottom: 4px;">Assign x1 := 0</div>
<div style="background: white; padding: 4px 8px; border: 1px solid #ccc; border-radius: 3px;">101</div>
</div>
<span style="font-size: 18px; color: #666;">→</span>
<div style="text-align: center;">
<div style="background: #e8f5e8; padding: 8px 12px; border-radius: 4px; margin-bottom: 4px;">Output</div>
<div style="background: white; padding: 4px 8px; border: 1px solid #ccc; border-radius: 3px;">101</div>
</div>
</div>
</div>
<p style="font-style: italic; color: #666; margin: 10px 0;">See this sequential flow in action in the editor below:</p>
<netkat example="First tests if x2 is 1, then assigns x1 to 0. Both the test and assignment must succeed for the packet to reach the output.">x2 == 1; x1 := 0</netkat>
<netkat exercise="Write a policy that tests x0 == 1, then sets x2 to 0">x0 == 1; x2 := 0</netkat>
<p>You can also chain multiple assignments together:</p>
<netkat example="Demonstrates chaining multiple assignments: first sets x0 to 1, then sets x2 to 0. Watch how the packet transforms through both steps.">x0 := 1; x2 := 0</netkat>
<h2>Choice (Union)</h2>
<p>Choice, denoted by a plus sign <nk>+</nk>, combines two NetKAT expressions and can be interpreted in two ways:</p>
<p><strong>Nondeterministic Interpretation:</strong> The network can choose to follow either path - it's an "either/or" decision where one path is selected.</p>
<p><strong>Multicast Interpretation:</strong> The network simultaneously sends copies of the packet down both paths - it's a "both" scenario. This is useful for modeling broadcast scenarios, load balancing, or redundant forwarding where you want a packet to reach multiple destinations.</p>
<div class="diagram-editor-container">
<div class="diagram-container">
<div class="diagram-box">
<svg width="120" height="80" viewBox="0 0 120 80">
<!-- Union: both circles are filled -->
<circle cx="40" cy="40" r="25" fill="#ce93d8"/>
<circle cx="80" cy="40" r="25" fill="#ce93d8"/>
<!-- Draw borders on top -->
<circle cx="40" cy="40" r="25" fill="none" stroke="#333" stroke-width="2"/>
<circle cx="80" cy="40" r="25" fill="none" stroke="#333" stroke-width="2"/>
<text x="30" y="45" font-size="12" fill="black" font-weight="bold">A</text>
<text x="86" y="45" font-size="12" fill="black" font-weight="bold">B</text>
</svg>
<div>
<strong>A + B (Union)</strong><br>
<span style="font-size: 14px; color: #666;">All traces from both sets</span>
</div>
</div>
</div>
<div class="editor-container">
<netkat example="Union: the result contains all traces from both expressions.">x0 := 1 + x1 := 1</netkat>
</div>
</div>
<p style="font-style: italic; color: #666; margin: 10px 0;">Watch the effect of branching in the editor below:</p>
<netkat example="Either sets x0 to 1 OR sets x1 to 1. The analyzer explores both possibilities, showing multiple traces.">x0 := 1 + x1 := 1</netkat>
<netkat exercise="Write a policy that either tests x0 == 1 or assigns x1 := 0">x0 == 1 + x1 := 0</netkat>
<h2 id="if-then-else">Encoding If-Then-Else</h2>
<p>NetKAT can express conditional logic (if-then-else) by combining tests, assignments, and choice. The general pattern for "if C then P1 else P2" is: <nk>(C; P1) + (not C; P2)</nk>.</p>
<h3>Test Negation (!)</h3>
<p>KATch<sup>2</sup> provides the <nk>!</nk> operator for negating test conditions. This operator can only be applied to test expressions (not arbitrary NetKAT programs):</p>
<div style="background-color: #f8f9fa; border: 1px solid #dee2e6; border-radius: 6px; padding: 15px; margin: 15px 0;">
<h4 style="margin-top: 0;">Test Negation Examples</h4>
<ul style="margin-bottom: 8px;">
<li><nk>!(x0 == 1)</nk> - True when x0 is NOT 1 (equivalent to <nk>x0 == 0</nk>)</li>
<li><nk>!(x0 == 1 & x1 == 1)</nk> - True when NOT both are 1</li>
<li><nk>!!(x0 == 1)</nk> - Double negation, same as <nk>x0 == 1</nk></li>
</ul>
<p style="margin-bottom: 0; font-style: italic; color: #666;">The negation is implemented using De Morgan's laws during desugaring.</p>
</div>
<netkat example="Test negation: accept packets where x0 is NOT 1">!(x0 == 1)</netkat>
<netkat example="Complex negation with De Morgan's law: !(A & B) = !A + !B">!(x0 == 1 & x1 == 1)</netkat>
<h3>If-Then-Else Syntactic Sugar</h3>
<p>Instead of manually encoding conditionals with choice operators, KATch<sup>2</sup> provides convenient <nk>if-then-else</nk> syntax:</p>
<div style="background-color: #f8f9fa; border: 1px solid #dee2e6; border-radius: 6px; padding: 15px; margin: 15px 0;">
<h4 style="margin-top: 0;">If-Then-Else Syntax</h4>
<nk>if condition then expr1 else expr2</nk>
<p style="margin-top: 8px;">This is automatically desugared to: <nk>(condition; expr1) + (!condition; expr2)</nk></p>
<p style="margin-bottom: 0; font-style: italic; color: #666;">The condition must be a test expression (something that filters packets).</p>
</div>
<netkat example="If-then-else syntax: set x1 based on x0's value">if x0 == 1 then x1 := 1 else x1 := 0</netkat>
<netkat example="Nested if-then-else with complex conditions">if x0 == 1 & x1 == 0 then
x2 := 1
else
if x0 == 0 then x2 := 0 else x2 := 1</netkat>
<div style="background-color: #f8f9fa; border: 1px solid #dee2e6; border-radius: 6px; padding: 20px; margin: 15px 0; font-family: monospace;">
<div style="margin-bottom: 12px; font-weight: bold; color: #495057; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;">If-Then-Else Decision: if x0 == 1 then x1 := 1 else x1 := 0</div>
<div style="display: flex; flex-direction: column; align-items: center; gap: 12px;">
<div style="background: #e3f2fd; padding: 8px 12px; border-radius: 4px; text-align: center;">
<strong>Input Packet</strong>
</div>
<div style="font-size: 18px; color: #666;">↓</div>
<div style="background: #fff3e0; padding: 10px 15px; border-radius: 4px; text-align: center;">
<strong>Test: x0 == 1?</strong>
</div>
<div style="display: flex; gap: 60px; align-items: center;">
<div style="display: flex; flex-direction: column; align-items: center; gap: 8px;">
<span style="font-size: 18px; color: #666;">↙</span>
<div style="background: #e8f5e8; padding: 6px 12px; border-radius: 3px; color: #2e7d32;">
<strong>YES (x0 == 1)</strong>
</div>
<div style="font-size: 18px; color: #666;">↓</div>
<div style="background: #f3e5f5; padding: 6px 10px; border-radius: 3px;">x1 := 1</div>
<div style="font-size: 12px; color: #666;">Then branch</div>
</div>
<div style="display: flex; flex-direction: column; align-items: center; gap: 8px;">
<span style="font-size: 18px; color: #666;">↘</span>
<div style="background: #ffebee; padding: 6px 12px; border-radius: 3px; color: #c62828;">
<strong>NO (!(x0 == 1))</strong>
</div>
<div style="font-size: 18px; color: #666;">↓</div>
<div style="background: #f3e5f5; padding: 6px 10px; border-radius: 3px;">x1 := 0</div>
<div style="font-size: 12px; color: #666;">Else branch</div>
</div>
</div>
<div style="font-size: 18px; color: #666;">↓</div>
<div style="background: #e8f5e8; padding: 8px 12px; border-radius: 4px; text-align: center;">
<strong>Result: x1 matches x0</strong>
</div>
</div>
</div>
<p style="font-style: italic; color: #666; margin: 10px 0;">Compare the manual encoding with the syntactic sugar:</p>
<netkat example="Manual encoding using choice and negation">(x0 == 1; x1 := 1) + (!(x0 == 1); x1 := 0)</netkat>
<netkat example="Same behavior using if-then-else syntax">if x0 == 1 then x1 := 1 else x1 := 0</netkat>
<netkat exercise="Use if-then-else to set x0 := 1 when x1 == 0, otherwise set x0 := 0">if x1 == 0 then x0 := 1 else x0 := 0</netkat>
<h2>Dup (Logging)</h2>
<p>The <nk>dup</nk> keyword is a special NetKAT operator that records the current packet state to the trace output without modifying it. This is extremely useful for observing the packet state at various points in your NetKAT expression.</p>
<div style="background-color: #f8f9fa; border: 1px solid #dee2e6; border-radius: 6px; padding: 20px; margin: 15px 0; font-family: monospace;">
<div style="margin-bottom: 12px; font-weight: bold; color: #495057; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;">Dup Logging: x1 := 1; dup; x2 := 1</div>
<div style="display: flex; align-items: center; justify-content: center; flex-wrap: wrap; gap: 12px;">
<div style="text-align: center;">
<div style="background: #e3f2fd; padding: 8px 12px; border-radius: 4px; margin-bottom: 4px;">Input</div>
<div style="background: white; padding: 4px 8px; border: 1px solid #ccc; border-radius: 3px;">000</div>
</div>
<span style="font-size: 18px; color: #666;">→</span>
<div style="text-align: center;">
<div style="background: #f3e5f5; padding: 8px 12px; border-radius: 4px; margin-bottom: 4px;">Assign x1 := 1</div>
<div style="background: white; padding: 4px 8px; border: 1px solid #ccc; border-radius: 3px;">010</div>
</div>
<span style="font-size: 18px; color: #666;">→</span>
<div style="text-align: center;">
<div style="background: #fff3e0; padding: 8px 12px; border-radius: 4px; margin-bottom: 4px;">dup (log)</div>
<div style="background: #fffacd; padding: 4px 8px; border: 1px solid #ddd; border-radius: 3px; font-style: italic;">010 logged</div>
</div>
<span style="font-size: 18px; color: #666;">→</span>
<div style="text-align: center;">
<div style="background: #f3e5f5; padding: 8px 12px; border-radius: 4px; margin-bottom: 4px;">Assign x2 := 1</div>
<div style="background: white; padding: 4px 8px; border: 1px solid #ccc; border-radius: 3px;">011</div>
</div>
<span style="font-size: 18px; color: #666;">→</span>
<div style="text-align: center;">
<div style="background: #e8f5e8; padding: 8px 12px; border-radius: 4px; margin-bottom: 4px;">Output</div>
<div style="background: white; padding: 4px 8px; border: 1px solid #ccc; border-radius: 3px;">011</div>
</div>
</div>
<div style="margin-top: 15px; padding: 10px; background: #f0f8ff; border-radius: 4px; border-left: 4px solid #4a90e2;">
<div style="font-weight: bold; margin-bottom: 5px; color: #4a90e2;">Trace Result:</div>
<div style="font-family: monospace; color: #333;">000 → 010 → 011</div>
<div style="font-size: 11px; color: #666; margin-top: 3px;">Three entries: input → logged state → final output</div>
</div>
</div>
<p style="font-style: italic; color: #666; margin: 10px 0;">See how dup creates intermediate trace entries in the editor below:</p>
<netkat example="Sets x1 to 1, logs the intermediate state with dup, then sets x2 to 1. Notice how the trace shows all three states.">x1 := 1; dup; x2 := 1</netkat>
<netkat exercise="Set x0 to 1, log with dup, then set x1 to 1, log again with dup">x0 := 1; dup; x1 := 1; dup</netkat>
<h2>Iteration (Kleene Star)</h2>
<p>Iteration, denoted by an asterisk <nk>*</nk> (Kleene star) after an expression <nk>e</nk> (i.e., <nk>e*</nk>), allows an expression to be executed zero or more times.</p>
<div style="background-color: #f8f9fa; border: 1px solid #dee2e6; border-radius: 6px; padding: 20px; margin: 15px 0; font-family: monospace;">
<div style="margin-bottom: 12px; font-weight: bold; color: #495057; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;">Iteration Paths: (x0:=1 + (x0==1;x1:=1) + (x1==1; x2:=1))*</div>
<div style="display: flex; flex-direction: column; align-items: center; gap: 8px;">
<div style="background: #e3f2fd; padding: 8px 12px; border-radius: 4px; text-align: center;">
<strong>Input: 000</strong>
</div>
<div style="font-size: 18px; color: #666;">↓</div>
<div style="display: flex; flex-direction: column; gap: 8px; align-items: center;">
<div style="display: flex; align-items: center; gap: 12px;">
<div style="background: #f3e5f5; padding: 6px 10px; border-radius: 3px; text-align: center; min-width: 80px;">
<div style="font-size: 12px; margin-bottom: 2px;">0 loops</div>
<div style="background: #e8f5e8; padding: 2px 6px; border-radius: 2px;">000</div>
</div>
<span style="color: #666;">‖</span>
<div style="background: #f3e5f5; padding: 6px 10px; border-radius: 3px; text-align: center; min-width: 80px;">
<div style="font-size: 12px; margin-bottom: 2px;">1 loop</div>
<div style="background: #e8f5e8; padding: 2px 6px; border-radius: 2px;">100</div>
</div>
<span style="color: #666;">‖</span>
<div style="background: #f3e5f5; padding: 6px 10px; border-radius: 3px; text-align: center; min-width: 80px;">
<div style="font-size: 12px; margin-bottom: 2px;">2 loops</div>
<div style="background: #e8f5e8; padding: 2px 6px; border-radius: 2px;">110</div>
</div>
<span style="color: #666;">‖</span>
<div style="background: #f3e5f5; padding: 6px 10px; border-radius: 3px; text-align: center; min-width: 80px;">
<div style="font-size: 12px; margin-bottom: 2px;">3+ loops</div>
<div style="background: #e8f5e8; padding: 2px 6px; border-radius: 2px;">111</div>
</div>
</div>
<div style="font-size: 11px; color: #666; text-align: center; margin-top: 4px;">
Nondeterministic iteration count produces several execution paths
</div>
</div>
<div style="font-size: 18px; color: #666;">↓</div>
<div style="background: #e8f5e8; padding: 8px 12px; border-radius: 4px; text-align: center;">
<strong>Multiple Possible Outputs</strong>
</div>
</div>
</div>
<p style="font-style: italic; color: #666; margin: 10px 0;">Explore the multiple iteration paths in the editor below:</p>
<netkat example="Iteratively set a bit to 1 if the previous bit was 1">((x0:=1 + (x0==1;x1:=1) + (x1==1; x2:=1)); dup)*</netkat>
<p>This shows multiple paths in the trace, corresponding to 0, 1, 2, etc., iterations, up to a limit defined by the analyzer to prevent infinite loops in the visualization.</p>
<netkat exercise="Write an iteration that nondeterministically modifies x0 in a loop and logs the intermediate states.">((x0 := 0 + x0 := 1); dup)*</netkat>
<h2>Logical Operators</h2>
<p>NetKAT expressions represent <strong>sets of traces</strong> (sequences of packet transformations). NetKAT's logical operators correspond to set operations on these trace sets, making them powerful tools for both constructing complex policies and verifying network properties.</p>
<h3>Union (+)</h3>
<p>We've already seen the union operator <nk>+</nk>. From a formal perspective, this represents the <strong>set union</strong> of traces from both expressions - the result contains all traces that are generated by either the left or right expression. This gives us a third interpretation alongside the nondeterministic and multicast views:</p>
<ul>
<li><strong>Nondeterministic:</strong> Choose one path</li>
<li><strong>Multicast:</strong> Send packets down both paths</li>
<li><strong>Set-theoretic:</strong> Union of two trace sets</li>
</ul>
<div class="diagram-editor-container">
<div class="diagram-container">
<div class="diagram-box">
<svg width="120" height="80" viewBox="0 0 120 80">
<!-- Union: both circles are filled -->
<circle cx="40" cy="40" r="25" fill="#ce93d8"/>
<circle cx="80" cy="40" r="25" fill="#ce93d8"/>
<!-- Draw borders on top -->
<circle cx="40" cy="40" r="25" fill="none" stroke="#333" stroke-width="2"/>
<circle cx="80" cy="40" r="25" fill="none" stroke="#333" stroke-width="2"/>
<text x="30" y="45" font-size="12" fill="black" font-weight="bold">A</text>
<text x="86" y="45" font-size="12" fill="black" font-weight="bold">B</text>
</svg>
<div>
<strong>A + B (Union)</strong><br>
<span style="font-size: 14px; color: #666;">All traces from both sets</span>
</div>
</div>
</div>
<div class="editor-container">
<netkat example="Union: the result contains all traces from both expressions.">x0 := 1 + x1 := 1</netkat>
</div>
</div>
<h3>Intersection (&)</h3>
<p>The intersection operator <nk>&</nk> represents the <strong>set intersection</strong> of traces. A trace appears in the result only if it's generated by both expressions.</p>
<div class="diagram-editor-container">
<div class="diagram-container">
<div class="diagram-box">
<svg width="120" height="80" viewBox="0 0 120 80">
<defs>
<clipPath id="intersect">
<circle cx="40" cy="40" r="25"/>
</clipPath>
</defs>
<!-- Draw base white fills first -->
<circle cx="40" cy="40" r="25" fill="white"/>
<circle cx="80" cy="40" r="25" fill="white"/>
<!-- Draw the intersection area on top -->
<circle cx="80" cy="40" r="25" fill="#ce93d8" clip-path="url(#intersect)"/>
<!-- Draw borders on top of everything -->
<circle cx="40" cy="40" r="25" fill="none" stroke="#333" stroke-width="2"/>
<circle cx="80" cy="40" r="25" fill="none" stroke="#333" stroke-width="2"/>
<text x="30" y="45" font-size="12" fill="black" font-weight="bold">A</text>
<text x="86" y="45" font-size="12" fill="black" font-weight="bold">B</text>
</svg>
<div>
<strong>A & B (Intersection)</strong><br>
<span style="font-size: 14px; color: #666;">Only traces in both sets</span>
</div>
</div>
</div>
<div class="editor-container">
<netkat example="Intersection: only traces that are in both sets appear in the result.">(x0 == 1) & (x1 == 0)</netkat>
</div>
</div>
<h3>Symmetric Difference / XOR (^)</h3>
<p>The XOR operator <nk>^</nk> represents the <strong>symmetric difference</strong> of trace sets - traces that are in one set but not both.</p>
<div class="diagram-editor-container">
<div class="diagram-container">
<div class="diagram-box">
<svg width="120" height="80" viewBox="0 0 120 80">
<defs>
<mask id="equalityMaskA2">
<rect width="120" height="80" fill="white"/>
<circle cx="62" cy="40" r="25" fill="black"/>
</mask>
<mask id="equalityMaskB2">
<rect width="120" height="80" fill="white"/>
<circle cx="58" cy="40" r="25" fill="black"/>
</mask>
</defs>
<!-- Draw base white fills first -->
<circle cx="58" cy="40" r="25" fill="white"/>
<circle cx="62" cy="40" r="25" fill="white"/>
<!-- Draw tiny XOR areas (almost empty) -->
<circle cx="58" cy="40" r="25" fill="#ce93d8" mask="url(#equalityMaskA2)"/>
<circle cx="62" cy="40" r="25" fill="#ce93d8" mask="url(#equalityMaskB2)"/>
<!-- Draw borders on top of everything -->
<circle cx="58" cy="40" r="25" fill="none" stroke="#333" stroke-width="2"/>
<circle cx="62" cy="40" r="25" fill="none" stroke="#333" stroke-width="2"/>
<text x="20" y="45" font-size="12" fill="black" font-weight="bold">A</text>
<text x="92" y="45" font-size="12" fill="black" font-weight="bold">B</text>
</svg>
<div>
<strong>A = B when A ^ B is empty</strong>
</div>
</div>
</div>
<div class="editor-container">
<netkat example="Equality verification: do these expressions generate the same trace set?">(x0 == 1; x1 == 1) ^ (x0 == 1 ; x1 := 1)</netkat>
</div>
</div>
<h3>Difference (-)</h3>
<p>The difference operator <nk>-</nk> represents the <strong>set difference</strong> - traces that are in the first set but not in the second set.</p>
<div class="diagram-editor-container">
<div class="diagram-container">
<div class="diagram-box">
<svg width="120" height="80" viewBox="0 0 120 80">
<defs>
<mask id="diffMask">
<rect width="120" height="80" fill="white"/>
<circle cx="80" cy="40" r="25" fill="black"/>
</mask>
</defs>
<!-- Draw base white fills first -->
<circle cx="40" cy="40" r="25" fill="white"/>
<circle cx="80" cy="40" r="25" fill="white"/>
<!-- Draw A minus intersection on top -->
<circle cx="40" cy="40" r="25" fill="#ce93d8" mask="url(#diffMask)"/>
<!-- Draw borders on top of everything -->
<circle cx="40" cy="40" r="25" fill="none" stroke="#333" stroke-width="2"/>
<circle cx="80" cy="40" r="25" fill="none" stroke="#333" stroke-width="2"/>
<text x="30" y="45" font-size="12" fill="black" font-weight="bold">A</text>
<text x="86" y="45" font-size="12" fill="black" font-weight="bold">B</text>
</svg>
<div>
<strong>A - B (Difference)</strong><br>
<span style="font-size: 14px; color: #666;">Traces in A but not in B</span>
</div>
</div>
</div>
<div class="editor-container">
<netkat example="Difference: traces in the first set but not the second.">(x0 == 1) - (x1 == 1)</netkat>
</div>
</div>
<h3>Using Logical Operators for Verification</h3>
<p>These set operations are particularly powerful for network verification:</p>
<h4>Subset Check (A ⊆ B)</h4>
<p>To verify that the trace set of A is a subset of the trace set of B, check if <nk>A - B</nk> is empty. If it's not empty, the analyzer will show you traces that are in A's set but not in B's set.</p>
<div class="diagram-editor-container">
<div class="diagram-container">
<div class="diagram-box">
<svg width="120" height="80" viewBox="0 0 120 80">
<defs>
<mask id="subsetVerifyMask">
<rect width="120" height="80" fill="white"/>
<circle cx="65" cy="40" r="30" fill="black"/>
</mask>
</defs>
<!-- Draw base white fills first -->
<circle cx="45" cy="40" r="20" fill="white"/>
<circle cx="65" cy="40" r="30" fill="white"/>
<!-- Draw tiny difference (almost empty) -->
<circle cx="45" cy="40" r="20" fill="#ce93d8" mask="url(#subsetVerifyMask)"/>
<!-- Draw borders on top of everything -->
<circle cx="45" cy="40" r="20" fill="none" stroke="#333" stroke-width="2"/>
<circle cx="65" cy="40" r="30" fill="none" stroke="#333" stroke-width="2"/>
<text x="10" y="45" font-size="12" fill="black" font-weight="bold">A</text>
<text x="100" y="45" font-size="12" fill="black" font-weight="bold">B</text>
</svg>
<div>
<strong>A ⊆ B when A - B is empty</strong>
</div>
</div>
</div>
<div class="editor-container">
<netkat example="Subset verification: is the trace set of (x0 == 1) ⊆ trace set of (x0 == 1 + x1 == 1)?">(x0 == 1) - (x0 == 1 + x1 == 1)</netkat>
</div>
</div>
<h4>Equality Check (A = B)</h4>
<p>To verify that two expressions generate the same trace set, check if <nk>A ^ B</nk> is empty. If it's not empty, the traces show you which traces appear in one set but not the other.</p>
<div class="diagram-editor-container">
<div class="diagram-container">
<div class="diagram-box">
<svg width="120" height="80" viewBox="0 0 120 80">
<defs>
<mask id="equalityMaskA2">
<rect width="120" height="80" fill="white"/>
<circle cx="62" cy="40" r="25" fill="black"/>
</mask>
<mask id="equalityMaskB2">
<rect width="120" height="80" fill="white"/>
<circle cx="58" cy="40" r="25" fill="black"/>
</mask>
</defs>
<!-- Draw base white fills first -->
<circle cx="58" cy="40" r="25" fill="white"/>
<circle cx="62" cy="40" r="25" fill="white"/>
<!-- Draw tiny XOR areas (almost empty) -->
<circle cx="58" cy="40" r="25" fill="#ce93d8" mask="url(#equalityMaskA2)"/>
<circle cx="62" cy="40" r="25" fill="#ce93d8" mask="url(#equalityMaskB2)"/>
<!-- Draw borders on top of everything -->
<circle cx="58" cy="40" r="25" fill="none" stroke="#333" stroke-width="2"/>
<circle cx="62" cy="40" r="25" fill="none" stroke="#333" stroke-width="2"/>
<text x="20" y="45" font-size="12" fill="black" font-weight="bold">A</text>
<text x="92" y="45" font-size="12" fill="black" font-weight="bold">B</text>
</svg>
<div>
<strong>A = B when A ^ B is empty</strong>
</div>
</div>
</div>
<div class="editor-container">
<netkat example="Equality verification: do these expressions generate the same trace set?">(x0 == 1; x1 == 0) ^ (x1 == 0 & x0 == 1)</netkat>
</div>
</div>
<div class="practice-section">
<h2>Additional Examples</h2>
<p>Click examples on the left to load them into the practice editor, or write your own expressions:</p>
<div class="practice-grid">
<div class="examples-column">
<h3>Examples (click to load):</h3>
<h4>Basic Operations:</h4>
<netkat target="practice-editor">x0 := 1</netkat>
<netkat target="practice-editor">x1 == 0</netkat>
<netkat target="practice-editor">x0 := 1; x1 := 0</netkat>
<h4>Choice Operations:</h4>
<netkat target="practice-editor">x0 := 1 + x1 := 1</netkat>
<netkat target="practice-editor">(x0 == 1; x1 := 1) + x1 := 0</netkat>
<h4>Iterations:</h4>
<netkat target="practice-editor">(x0 == 0; x0 := 1)*</netkat>
<netkat target="practice-editor">((x0 == 0; x0 := 1); dup)*</netkat>
<h4>Complex Policies:</h4>
<netkat target="practice-editor" lines="3">// Bit flipper with logging
(x0 == 0; x0 := 1 + x0 == 1; x0 := 0);
dup; x1 := 1</netkat>
<netkat target="practice-editor" lines="4">// Conditional assignment chain
x0 == 1;
(x1 == 0; x2 := 1 + x1 == 1; x2 := 0);
dup</netkat>
</div>
<div class="editor-column">
<h3>Practice Editor</h3>
<h4>Click examples on the left to load them:</h4>
<netkat id="practice-editor" lines="8" show-line-numbers">// Click an example to load it
x1 == 0
</netkat>
</div>
</div>
</div>
<div class="practice-section">
<h2>Additional Exercises</h2>
<p>Click exercises on the left to load them into the analysis editor with equivalence checking:</p>
<div class="practice-grid">
<div class="examples-column">
<h3>Additional Exercises:</h3>
<netkat exercise="Create a bit flipper: if x0 is 0 set it to 1, if x0 is 1 set it to 0" target="analysis-editor">(x0 == 0; x0 := 1) + (x0 == 1; x0 := 0)</netkat>
<netkat exercise="Write a policy that copies x0 to x1" target="analysis-editor">(x0 == 1; x1 := 1) + (x0 == 0; x1 := 0)</netkat>
<netkat exercise="Create a policy that accepts packets only if x0 and x1 are equal" target="analysis-editor">(x0 == 0; x1 == 0) + (x0 == 1; x1 == 1)</netkat>
<netkat exercise="Write a policy that swaps x0 and x1" target="analysis-editor">
(x0 == 0; x1 == 1; x0 := 1; x1 := 0) +
(x0 == 1; x1 == 0; x0 := 0; x1 := 1) +
(x0 == 0; x1 == 0) +
(x0 == 1; x1 == 1)
</netkat>
</div>
<div class="editor-column">
<h3>Exercise Editor:</h3>
<netkat id="analysis-editor" lines="8" show-line-numbers>// Click an exercise to load it
x1 == 0
</netkat>
</div>
</div>
<p>This concludes the basic NetKAT tutorial. Experiment with these constructs to build more complex network policies!</p>
<h2 id="sugar">Syntactic Sugar for Modeling Real Networks</h2>
<p>KATch<sup>2</sup> provides additional syntactic constructs to make it easier to work with real network packets, which often contain multi-bit fields like IP addresses, ports, and protocol numbers.</p>
<h3>Bit Range Tests and Mutations</h3>
<p>Instead of testing or modifying individual bits, you can work with ranges of bits that represent packet fields:</p>
<div style="background-color: #f8f9fa; border: 1px solid #dee2e6; border-radius: 6px; padding: 20px; margin: 15px 0;">
<h4 style="margin-top: 0;">Bit Range Syntax</h4>
<ul style="margin-bottom: 12px;">
<li><nk>x[start..end]</nk> - References bits from position <nk>start</nk> (inclusive) to <nk>end</nk> (exclusive)</li>
<li><nk>x[0..8] ~ 255</nk> - Tests if the first byte equals 255</li>
<li><nk>x[0..8] := 10</nk> - Sets the first byte to 10</li>
</ul>
<p style="margin-bottom: 0; font-style: italic; color: #666;">The bit range notation follows Rust-style syntax where the end index is exclusive.</p>
</div>
<netkat example="Test if the first byte (bits 0-7) equals 255">x[0..8] ~ 255</netkat>
<netkat example="Set the second byte to 192">x[8..16] := 192</netkat>
<h4>Working with Different Literal Formats</h4>
<p>Bit ranges can be tested or set using various literal formats:</p>
<netkat example="Binary literal: Test if a nibble equals binary 1010">x[0..4] ~ 0b1010</netkat>
<netkat example="Hexadecimal literal: Set a byte to hex FF">x[0..8] := 0xFF</netkat>
<netkat example="IP address literal: Test if bits represent an IP address">x[0..32] ~ 192.168.1.1</netkat>
<h3>Let Aliases for Bit Ranges</h3>
<p>To make policies more readable, you can create named aliases for bit ranges using the <nk>let</nk> syntax:</p>
<div style="background-color: #f8f9fa; border: 1px solid #dee2e6; border-radius: 6px; padding: 20px; margin: 15px 0;">
<h4 style="margin-top: 0;">Alias Syntax</h4>
<nk>let alias = &x[start..end] in expression</nk>
<p style="margin-top: 8px; margin-bottom: 0;">Creates an alias that can be used in tests and assignments within the expression.</p>
</div>
<netkat example="Create an alias 'ip' for the first 32 bits and test if it equals a specific IP">let ip = &x[0..32] in ip ~ 192.168.1.1</netkat>
<netkat example="Multiple aliases: define source and destination IPs">let src_ip = &x[0..32] in
let dst_ip = &x[32..64] in
src_ip ~ 10.0.0.1 & dst_ip ~ 10.0.0.2</netkat>
<netkat example="Alias with assignment: set a port number">let port = &x[64..80] in port := 8080</netkat>
<h4>Real-World Example: Simple Firewall Rule</h4>
<p>Here's how you might model a firewall rule that accepts HTTP traffic (port 80) from a specific subnet:</p>
<netkat example="Firewall rule: accept HTTP from 192.168.1.0/24" lines="4">let src_ip = &x[0..32] in
let dst_port = &x[48..64] in
// Accept if source is in 192.168.1.x and destination port is 80
(src_ip ~ 192.168.1.1 + src_ip ~ 192.168.1.2 + src_ip ~ 192.168.1.3) & dst_port ~ 80</netkat>
<h3>Let Bindings for Expressions</h3>
<p>Beyond bit range aliases, <nk>let</nk> can bind any NetKAT expression to a variable for reuse:</p>
<div style="background-color: #f8f9fa; border: 1px solid #dee2e6; border-radius: 6px; padding: 20px; margin: 15px 0;">
<h4 style="margin-top: 0;">Expression Binding Syntax</h4>
<nk>let var = expression1 in expression2</nk>
<p style="margin-top: 8px; margin-bottom: 0;">Binds <nk>expression1</nk> to <nk>var</nk>, which can be used in <nk>expression2</nk>.</p>
</div>
<netkat example="Bind a complex test to a variable">let is_local = x[0..8] ~ 192 & x[8..16] ~ 168 in
is_local; x[32..40] := 1</netkat>
<netkat example="Reuse an assignment sequence">let setup_defaults = x[0..8] := 10; x[8..16] := 0; x[16..24] := 0; x[24..32] := 1 in
x[32..40] ~ 80; setup_defaults</netkat>
<h4>Combining Features: NAT Example</h4>
<p>Here's a more complex example that combines bit ranges, aliases, and let bindings to model a simple NAT (Network Address Translation) rule:</p>
<netkat example="NAT rule: rewrite source IP for outgoing traffic" lines="6">let src_ip = &x[0..32] in
let dst_ip = &x[32..64] in
let is_private = src_ip ~ 192.168.1.1 + src_ip ~ 192.168.1.2 in
let is_external = dst_ip ~ 8.8.8.8 + dst_ip ~ 1.1.1.1 in
// If source is private and destination is external, rewrite source to public IP
is_private & is_external; src_ip := 203.0.113.1</netkat>
<h3>Pattern Matching with the ~ Operator</h3>
<p>KATch<sup>2</sup> supports flexible pattern matching for IP addresses and bit ranges using the <nk>~</nk> operator. This makes it easy to express common network matching patterns without writing complex bit-level tests.</p>
<h4>Exact IP Matching</h4>
<p>The simplest form matches an exact IP address:</p>
<netkat example="Match packets from a specific IP address">let src = &x[0..32] in
src ~ 192.168.1.100</netkat>
<h4>CIDR Notation (Prefix Matching)</h4>
<p>Use CIDR notation to match IP prefixes/subnets:</p>
<netkat example="Match all IPs in a /24 subnet">let src = &x[0..32] in
src ~ 192.168.1.0/24 + // Matches 192.168.1.0 - 192.168.1.255
src ~ 192.168.2.0</netkat>
<netkat example="Match multiple subnets">let dst = &x[32..64] in
// Match private networks
dst ~ 10.0.0.0/8 + dst ~ 172.16.0.0/12 + dst ~ 192.168.0.0/16</netkat>
<h4>IP Range Matching</h4>
<p>Match arbitrary IP ranges using the hyphen syntax:</p>
<netkat example="Match IP range">let src = &x[0..32] in
// Match IPs from 10.0.0.1 to 10.0.0.10
src ~ 10.0.0.1-10.0.0.10</netkat>
<p>This works efficiently even for very large ranges:</p>
<netkat example="Match large IP ranges efficiently">let ip = &x[0..32] in
// These large ranges are handled efficiently without expanding to millions of tests
ip ~ 10.0.0.0-10.255.255.255 + // 16 million IPs
ip ~ 172.16.0.0-172.31.255.255 + // 1 million IPs
ip ~ 192.168.0.0-192.168.255.255 // 65 thousand IPs</netkat>
<h4>Wildcard Masks</h4>
<p>For complex patterns, use wildcard masks (similar to Cisco ACLs):</p>
<netkat example="Wildcard mask matching">let dst = &x[32..64] in
// Match 192.168.X.1 where X can be any value
dst ~ 192.168.1.1 mask 0.0.255.0</netkat>
<h4>Pattern Matching with Different Literal Formats</h4>
<p>Patterns work with all literal formats, not just IP addresses:</p>
<netkat example="Pattern matching with hex and binary literals">let port = &x[0..16] in
let proto = &x[16..24] in
// Match port ranges using decimal
port ~ 1024-65535 & // Unprivileged ports
// Match using hex (e.g., protocol numbers)
proto ~ 0x06 + // TCP (6)
proto ~ 0x11 + // UDP (17)
proto ~ 0x01 // ICMP (1)</netkat>