-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunix-huawei2022.links
1352 lines (1221 loc) · 28.9 KB
/
unix-huawei2022.links
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
# DISCLAIMER: THIS MODULE REQUIRES A SPECIAL BRANCH OF LINKS TO
# COMPILE: https://github.com/dhil/links/tree/multi-line-comments
# commit ddcc02d3 as the effect patterns are not yet available in
# master (you'll find that everything is implemented twice in this
# file, once with the "new" syntax, which is shadowed by an
# implementation using the "old" syntax).
## Prelude
typename Option(a) = [|None|Some:a|];
sig todo : (String) ~> a
fun todo(s) { error("TODO: " ^^ s) }
sig fail : () {Fail:Zero |_}-> a
fun fail() { switch (do Fail) { } }
sig lookup : (a, [(a, b)]) {Fail:Zero |_}~> b
fun lookup(k, kvs) {
switch (kvs) {
case [] -> fail()
case (k', v) :: kvs' ->
if (k == k') v
else lookup(k, kvs')
}
}
sig modify : (a, b, [(a, b)]) ~> [(a, b)]
fun modify(k, v, kvs) {
switch (kvs) {
case [] -> []
case (k', v') :: kvs' ->
if (k == k') (k, v) :: kvs'
else (k', v') :: modify(k, v, kvs')
}
}
sig remove : (a, [(a, b)]) ~> [(a, b)]
fun remove(k, kvs) {
switch (kvs) {
case [] -> []
case (k', v') :: kvs' ->
if (k == k') kvs'
else (k', v') :: remove(k, kvs')
}
}
sig has : (a, [(a, b)]) ~> Bool
fun has(k, kvs) {
switch (kvs) {
case [] -> false
case (k', _) :: kvs' ->
k == k' || has(k, kvs')
}
}
#!
#
# Composing UNIX with Effect Handlers
# An Introduction to Effect Handler Oriented Programming
# Daniel Hillerström
# Laboratory for Foundations of Computer Science
# The University of Edinburgh, Scotland, UK
#
# Huawei Research Centre Zürich, Switzerland
# October 13, 2022
#
# https://dhil.net/research/
#
#?
#!
#
# Effect handler oriented programming (EHOP)
#
# Key characteristics
# - Extensive use of effect handlers
# - High-degree of modularity
# - Extremely compositional
#
# Some languages that support EHOP:
# C/C++, Eff, Haskell, Koka, Links, Pyro, OCaml, Unison, Wasm
#
#?
#!
#
# What is an effect handler?
#
# Operational interpretation <--- THIS TALK
# Resumeable exceptions
# Programmable and composable operating systems
#
# Software engineering interpretation
# Builders for monads (monads as a design pattern)
#
# Functional programming interpretation
# Folds over computation trees
# Free interpreters
#
# Mathematical interpretation
# Homomorphisms between free algebraic models
#
#?
#!
#
# Objectives of this talk
#
# - Demonstrate the versatility of effect handler oriented programming
# - Explain Ritchie & Thompson's (1974) UNIX as the combination of
# textbook effects
# + Exceptions: Process termination
# + Dynamic binding: User environments
# + Nondeterminism: Time-sharing
# + State: File system
# + ...
#
#?
#!
#
# This talk at glance
#
# A model of UNIX with
# * support for multiple users,
# * time-sharing amongst processes,
# * and a file system.
#
# Self-imposed constraints
# * Interface cannot be changed
# * Everything has to be definable in the calculus
#
# Disclaimer: We'll make some gross simplifications. A richer model of
# UNIX can be found in my PhD dissertation
#
# (the idea of using delimited control to model operating systems is
# not new, see e.g. Kiselyov and Shan (2007), Wand (1980))
#
#?
#
#
# The key idea
#
# *System calls* are an interface, implemented by an *operating system*
#
# *Effectful operations* are an interface, implemented by an *effect handler*
#
#
#
#
# The key idea
#
# *System calls* are an interface, implemented by an *operating system*
# = =
# *Effectful operations* are an interface, implemented by an *effect handler*
#
#
#
#
# What is an operating system? (very abstractly)
#
# An operating system responds to a collection of system calls
#
# Example tasks:
# - Signalling errors
# - Scheduling processes
# - Reading/writing I/O
#
#
#
# What is an effect handler? (very abstractly)
#
# An effect handler responds a collection of abstract operation calls
#
# Example tasks:
# - Signalling errors
# - Scheduling processes
# - Reading/writing I/O
#
#
#
#
#
#
#
#
# What is an effect handler? (very abstractly)
#
# An effect handler responds a collection of abstract operation calls
#
# Example tasks:
# - Signalling errors
# - Scheduling processes
# - Reading/writing I/O
#
# Thus an effect handler is an operating system (credit James McKinna)
# (Kiselyov and Shan (2007) used delimited continuations to model
# operating systems)
#
#
#
#
# Objectives of this talk
#
# - Demonstrate the versatility of handlers
# - Explain operating systems as the combination of
# + Exceptions
# + Dynamic binding
# + Nondeterminism
# + State
#
#
#
#
# What is UNIX?
#
# UNIX is an operating system designed by Ritchie and Thompson (1974)
#
# Components
# - Commands (system calls)
# + I/O interaction, user session login, inter-process
# communication, etc
# - Kernel (interpreter)
# + Handling of I/O, managing user sessions, scheduling of
# processes
# - Development environment
# + Compiler tool-chains (e.g. `cc`)
# - Documentation
# + manual pages (e.g. `man`)
#
#
#
#
# Key characteristics of UNIX (Ritchie & Thompson, 1974)
#
# - Support for multiple user sessions
# - Time-sharing between processes
# - "Everything is a file"
#
#
#{
#!
#
# Basic I/O: Performing writes
#
typename File = String;
typename FileDescr = Int;
sig stdout : FileDescr
var stdout = 1;
sig echo : (String) {Write:(FileDescr, String) -> ()}-> ()
fun echo(cs) { do Write(stdout, cs) }
#?
#}
typename File = String;
typename FileDescr = Int;
sig stdout : FileDescr
var stdout = 1;
sig echo : (String) {Write:(FileDescr, String) -> () |%}-> ()
fun echo(cs) { do Write(stdout, cs) }
#{
#!
#
# Basic I/O: Handling writes
#
sig basicIO : ( () {Write:(FileDescr, String) -> ()}-> a ) -> (a, File)
fun basicIO(m) {
handle(m()) {
case ans -> (ans, "")
case <Write(_, cs) => (resume : (()) -> (a, File) )> ->
var (ans, file) = resume(());
(ans, file ++ cs)
}
}
#?
#}
sig basicIO : ( () {Write:(FileDescr, String) -> () |%}-> a ) { |%}-> (a, File)
fun basicIO(m) {
handle(m()) {
case Return(ans) -> (ans, "")
case Write(_, cs, resume) ->
var (ans, file) = resume(());
(ans, cs ^^ file)
}
}
#{
#!
#
# Basic I/O: Example
#
sig example0 : () -> ((), File)
fun example0() {
basicIO(fun() {
echo("Hello"); echo("World")
})
}
#?
#}
sig example0 : () { |%}-> ((), File)
fun example0() {
basicIO(fun() {
echo("Hello"); echo("World")
})
}
#
#
# Dynamic semantics of handlers
#
# (ret) handle(V) { case x -> N case ... }
# ~> N[V/x]
#
# (op) handle(E[do Op(V)]) { case <Op(p) => r> -> N case ... }
# ~> N[V/p
# ,fun(x){ handle(E[x]) { case <Op(p) => r> -> N case ... }}/r]
# (if Op \notin E)
#
#
#{
#!
#
# Exceptions: Premature exits
#
sig exit : (Int) {Exit:(Int) -> Zero}-> a
fun exit(n) { switch (do Exit(n)) { } }
#?
#}
sig exit : (Int) {Exit:(Int) -> Zero |%}-> a
fun exit(n) { switch (do Exit(n)) { } }
#{
#!
#
# Handling exits
#
sig status : (() {Exit:(Int) -> Zero}-> a) -> Int
fun status(m) {
handle(m()) {
case ans -> 0
case <Exit(n)> -> n
}
}
#?
#}
sig status : (() {Exit:(Int) -> Zero |%}-> a) { |%}-> Int
fun status(m) {
handle(m()) {
case Return(_) -> 0
case Exit(n, _) -> n
}
}
#{
#!
#
# Handling exits: Example
#
sig example1 : () -> (Int, File)
fun example1() {
basicIO(fun() {
status(fun() {
echo("dead"); exit(1); echo("code")
})
})
}
#?
#}
sig example1 : () { |%}-> (Int, File)
fun example1() {
basicIO(fun() {
status(fun() {
echo("dead"); exit(1); echo("code")
})
})
}
#{
#!
#
# Does the ordering of handlers matter?
#
sig example1' : () -> Int
fun example1'() {
status(fun() {
basicIO(fun() {
echo("dead"); exit(1); echo("code")
})
})
}
#?
#}
sig example1' : () { |%}-> Int
fun example1'() {
status(fun() {
basicIO(fun() {
echo("dead"); exit(1); echo("code")
})
})
}
#{
#!
#
# Dynamic binding: User-specific environments (1)
#
typename User = [|Alice|Bob|Root|];
sig whoami : () {Ask:String}-> String
fun whoami() { do Ask }
#?
#}
typename User = [|Alice|Bob|Root|];
sig whoami : () {Ask:String |%}-> String
fun whoami() { do Ask }
#{
#!
#
# Dynamic binding: User-specific environments (2)
#
sig env : (User, () {Ask:String}-> a) -> a
fun env(user, m) {
handle(m()) {
case ans -> ans
case <Ask => resume> ->
switch (user) {
case Alice -> resume("alice")
case Bob -> resume("bob")
case Root -> resume("root")
}
}
}
sig example2 : () -> String
fun example2() {
env(Root, whoami)
}
#?
#}
sig env : (User, () {Ask:String |%}-> a) { |%}-> a
fun env(user, m) {
handle(m()) {
case Return(x) -> x
case Ask(resume) ->
switch (user) {
case Alice -> resume("alice")
case Bob -> resume("bob")
case Root -> resume("root")
}
}
}
sig example2 : () { |%}-> String
fun example2() {
env(Root, whoami)
}
#!
#
# Aside: Dynamic binding with delimited continuations
#
# The idea of dynamic binding dates back to at least McCarthy (1960)
#
# Kiselyov, Shan, and Sabry (2006) demonstrated dynamic binding can be
# simulated with delimited continuations
#
#?
#{
#!
#
# User session management
#
sig su : (User) {Su:(User) -> ()}-> ()
fun su(user) { do Su(user) }
sig sessionmgr : (User, () {Ask:String, Su:(User) -> ()}-> a) -> a
fun sessionmgr(user, m) {
env(user, fun() {
handle(m()) {
case ans -> ans
case <Su(user') => resume> ->
env(user', fun() { resume(()) })
}
})
}
#?
#}
sig su : (User) {Su:(User) -> () |%}-> ()
fun su(user) { do Su(user) }
sig sessionmgr : (User, () {Ask:String, Su:(User) -> () |%}-> a) { |%}-> a
fun sessionmgr(user, m) {
env(user, fun() {
handle(m()) {
case Return(ans) -> ans
case Su(user', resume) ->
env(user', fun() { resume(()) })
}
})
}
#{
#!
#
# Multiple user sessions example
#
sig example3 : () -> (Int, File)
fun example3() {
basicIO(fun() {
sessionmgr(Root, fun() {
status(fun() {
su(Alice); echo(whoami()); echo(" ");
su(Bob); echo(whoami()); echo(" ");
su(Root); echo(whoami())
})
})
})
}
#?
#}
sig example3 : () { |%}-> (Int, File)
fun example3() {
basicIO(fun() {
sessionmgr(Root, fun() {
status(fun() {
su(Alice); echo(whoami()); echo(" ");
su(Bob); echo(whoami()); echo(" ");
su(Root); echo(whoami())
})
})
})
}
#!
#
# Nondeterminism: Multi-tasking (1)
#
# From the man pages.
#
# Description
# fork() creates a new process by duplicating the calling process. The
# new process is referred to as the child process. The calling process
# is referred to as the parent process.
#
# Return value
# On success, the PID of the child process is returned in the parent,
# and 0 is returned in the child.
#
#?
#!
#
# Nondeterminism: Multi-tasking (2)
#
# Fork idiom
#
# if (fork() > 0) parent's code
# else child's code
#
# Let's simplify fork such that it returns a boolean: true for parent,
# false for child.
#
#?
#{
#!
#
# Nondeterminism: Multi-tasking (3)
#
sig fork : () {Fork:Bool}-> Bool
fun fork() { do Fork }
sig nondet : (() {Fork:Bool}-> a) -> [a]
fun nondet(m) {
handle(m()) {
case ans -> [ans]
case <Fork => resume> -> resume(true) ++ resume(false)
}
}
#?
#}
sig fork : () {Fork:Bool |_}-> Bool
fun fork() { do Fork }
sig nondet : (() {Fork:Bool |%}-> a) { |%}-> [a]
fun nondet(m) {
handle(m()) {
case Return(ans) -> [ans]
case Fork(resume) -> resume(true) ++ resume(false)
}
}
#{
#!
#
# Nondeterminism: Example (1)
#
sig ritchie : () {Write:(FileDescr, String) -> ()}-> ()
fun ritchie() {
echo("UNIX is basically ");
echo("a simple operating system, ");
echo("but ");
echo("you have to be a genius to understand the simplicity.\n")
}
sig hamlet : () {Write:(FileDescr, String) -> ()}-> ()
fun hamlet() {
echo("To be, or not to be,\n");
echo("that is the question:\n");
echo("Whether 'tis nobler in the mind to suffer\n")
}
#?
#}
sig ritchie : () {Write:(FileDescr, String) -> () |%}-> ()
fun ritchie() {
echo("UNIX is basically ");
echo("a simple operating system, ");
echo("but ");
echo("you have to be a genius to understand the simplicity.\n")
}
sig hamlet : () {Write:(FileDescr, String) -> () |%}-> ()
fun hamlet() {
echo("To be, or not to be,\n");
echo("that is the question:\n");
echo("Whether 'tis nobler in the mind to suffer\n")
}
#{
#!
#
# Nondeterminism: Example (2)
#
sig example4 : () -> ([Int], File)
fun example4() {
basicIO(fun() {
nondet(fun() {
sessionmgr(Root, fun() {
status(fun() {
if (fork()) {
su(Alice);
ritchie()
} else {
su(Bob);
hamlet()
}
})
})
})
})
}
#?
#}
sig example4 : () { |%}-> ([Int], File)
fun example4() {
basicIO(fun() {
nondet(fun() {
sessionmgr(Root, fun() {
status(fun() {
if (fork()) {
su(Alice);
ritchie()
} else {
su(Bob);
hamlet()
}
})
})
})
})
}
#
#
# Mathematically well-founded nondeterminism
#
# The handler `nondet` is _exactly_ the handler Plotkin and Pretnar (2013)
# give for nondeterminism
# It satisfies the usual (semi-lattice) equations for nondeterministic choice, i.e.
#
# if (fork()) M else M = M
# if (fork()) M else N = if (fork()) N else M
# if (fork()) L else { if (fork()) M else N } = if (fork()) { if (fork()) L else M } else N
#
#
#{
#!
#
# Interrupting processes
#
sig interrupt : () {Interrupt:()}-> ()
fun interrupt() { do Interrupt }
# Process reification
typename Pstate(a::Type, e::Eff)
= [|Done:a
|Paused:() -e-> Pstate(a, e)|];
sig reifyProcess : (() {Interrupt:() |e}-> a) -e-> Pstate(a, e)
fun reifyProcess(m) {
handle(m()) {
case ans -> Done(ans)
case <Interrupt => resume> -> Paused(fun() { resume(()) })
}
}
#?
#}
sig interrupt : () {Interrupt:() |%}-> ()
fun interrupt() { do Interrupt }
typename Pstate(a,e::Eff)
= forall q::Presence.
[|Done:a
|Paused:() {Interrupt{q} |e}-> Pstate(a, { |e})|];
sig reifyProcess : (() {Interrupt:() |%}-> a) { |%}-> Pstate(a, { |%})
fun reifyProcess(m) {
handle(m()) {
case Return(ans) -> Done(ans)
case Interrupt(resume) -> Paused(fun() { resume(()) })
}
}
#{
#!
#
# Time-sharing via interrupts
#
sig schedule : ([Pstate(a, {Fork:Bool})]) -> [a]
fun schedule(ps) {
fun schedule(ps, done) {
switch (ps) {
case [] -> done
case Done(res) :: ps' ->
schedule(ps', res :: done)
case Paused(resume) :: ps' ->
schedule(ps' ++ nondet(resume), done)
}
}
schedule(ps, [])
}
sig timeshare : (() {Fork:Bool,Interrupt:()}-> a) -> [a]
fun timeshare(m) {
var p = Paused(fun() { reifyProcess(m) });
schedule([p])
}
#?
#}
sig schedule : ([Pstate(a, { Fork:Bool |%})]) { |%}~> [a]
fun schedule(ps) {
fun schedule(ps, done) {
switch (ps) {
case [] -> done
case Done(res) :: ps' ->
schedule(ps', res :: done)
case Paused(resume) :: ps' ->
schedule(ps' ++ nondet(resume), done)
}
}
schedule(ps, [])
}
sig timeshare : (() {Fork:Bool,Interrupt:() |%}-> a) { |%}-> [a]
fun timeshare(m) {
var p = Paused(fun() { reifyProcess(m) });
schedule([p])
}
#{
#!
#
# Injecting interrupts
#
# First idea: external source injects interrupts (Ahman and Pretnar (2021))
#
# Second idea: bundle interrupts with other operations
sig echo' : (FileDescr,String) {Interrupt:(), Write:(FileDescr,String) -> ()}-> ()
fun echo'(fd, cs) { interrupt(); do Write(fd, cs) }
#
# Third idea: overload interpretations of operations
sig interruptWrite : (() {Write:(FileDescr,String) -> ()}-> a)
{Interrupt:(),Write:(FileDescr,String) -> ()}-> a
fun interruptWrite(m) {
handle(m()) {
case ans -> ans
case <Write(fd, cs) => resume> ->
interrupt(); resume(do Write(fd, cs))
}
}
#?
#}
sig interruptWrite : (() {Write:(FileDescr,String) -> () |%}-> a)
{Write:(FileDescr,String) -> () |%}-> a
fun interruptWrite(m) {
handle(m()) {
case Return(res) -> res
case Write(fd, cs, resume) ->
interrupt(); resume(do Write(fd, cs))
}
}
#{
#!
#
# Time-sharing example
#
sig example5 : () -> ([Int], File)
fun example5() {
basicIO(fun() {
timeshare(fun() {
interruptWrite(fun() {
sessionmgr(Root, fun() {
status(fun() {
if (fork()) {
su(Alice);
ritchie()
} else {
su(Bob);
hamlet()
}
})
})
})
})
})
}
#?
#}
sig example5 : () { |%}-> ([Int], File)
fun example5() {
basicIO(fun() {
timeshare(fun() {
interruptWrite(fun() {
sessionmgr(Root, fun() {
status(fun() {
if (fork()) {
su(Alice);
ritchie()
} else {
su(Bob);
hamlet()
}
})
})
})
})
})
}
#{
#!
#
# State: File I/O
#
# Generic state handling
sig get : () {Get:s}-> s
fun get() { do Get }
sig put : (s) {Put:(s) -> ()}-> ()
fun put(st) { do Put(st) }
sig runState : (s, () {Get:() -> s,Put:(s) -> ()}-> a) -> (a, s)
fun runState(st0, m) {
var f = handle(m()) {
case ans -> fun(st) { (ans, st) }
case <Get => resume> -> fun(st) { resume(st)(st) }
case <Put(st') => resume> -> fun(_) { resume(())(st') }
};
f(st0)
}
#?
#}
sig get : () {Get:s |_}-> s
fun get() { do Get }
sig put : (s) {Put:(s) -> () |_}-> ()
fun put(st) { do Put(st) }
sig runState : (s, () {Get:() -> s,Put:(s) -> () |%}-> a) { |%}-> (a, s)
fun runState(st0, m) {
var f = handle(m()) {
case Return(x) -> fun(st) { (x, st) }
case Get(resume) -> fun(st) { resume(st)(st) }
case Put(st',resume) -> fun(_) { resume(())(st') }
};
f(st0)
}
#{
#!
#
# State: Example
#
sig incr : () {Get:Int,Put:(Int) -> ()}-> ()
fun incr() { put(get() + 1) }
sig example6 : () -> ((), Int)
fun example6() {
runState(41, incr)
}
#?
#}
sig incr : () {Get:Int,Put:(Int) -> () |%}-> ()
fun incr() { put(get() + 1) }
sig example6 : () { |%}-> ((), Int)
fun example6() {
runState(41, incr)
}
#!
#
# Basic Serial File System (BSFS)
#
# Directory I-List Data region
# +----------------+ +-------+ +--------------------------+
# | "hamlet" |------> | 2 |---> | "To be, or not to be..." |
# +----------------+ / +-------+ +--------------------------+
# | "richtie.txt" |------> | 1 |---> | "UNIX is basically..." |
# +----------------+ / +-------+ +--------------------------+
# | ... | | | ... | | ... |
# +----------------+ | +-------+ +--------------------------+
# | "stdout" |------> | 1 |---> | "" |
# +----------------+ | +-------+ +--------------------------+
# | ... | /
# +----------------+ /
# | "act3" |---
# +----------------+
#
# Simplifications:
# - Operating directly on inode pointers
# - Reads and writes will be serial
#
#?
#{
#!
#
# BSFS structures
#
typename INode = (loc:Int,lno:Int);
typename IList = [(Int, INode)]; # INode index -> INode
typename Directory = [(String, Int)]; # Filename -> INode index
typename DataRegion = [(Int, File)]; # Loc -> File
typename FileSystem = (dir:Directory,dregion:DataRegion,inodes:IList
,lnext:Int ,inext:Int );
sig fsys0 : FileSystem
var fsys0 = ( dir = [("stdout", 0)]
, inodes = [(0, (loc=0, lno=1))]
, dregion = [(0, "")]
, lnext = 1, inext = 1 );