-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_conts.py
More file actions
899 lines (815 loc) · 36.1 KB
/
test_conts.py
File metadata and controls
899 lines (815 loc) · 36.1 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
# -*- coding: utf-8 -*-
"""Continuations (call/cc for Python)."""
from ...syntax import macros, test, test_raises, error, fail # noqa: F401
from ...test.fixtures import session, testset, returns_normally
from ...syntax import macros, continuations, call_cc, multilambda, autoreturn, autocurry, let # noqa: F401, F811
from ...syntax import get_cc, iscontinuation
from ...collections import box, unbox
from ...ec import call_ec
from ...fploop import looped
from ...fun import withself
from ...funutil import Values
from ...tco import trampolined, jump
def runtests():
with testset("basic usage"):
with continuations:
def add1(x):
return 1 + x
test[add1(2) == 3]
def message(cc):
# The continuations system essentially deals with function composition,
# so we make a distinction between a single `tuple` return value and
# multiple-return-values.
#
# Use Values(...) to return multiple values from a function that you
# intend to `call_cc`.
return Values("hello", "there")
def baz():
m, n = call_cc[message()] # The cc arg is passed implicitly.
return [m, n]
test[baz() == ["hello", "there"]]
# The cc arg must be declared as the last one that has no default value,
# or declared as by-name-only. It's always passed by name.
#
# If the function is going to be used as a target for `call_cc[]`,
# multiple return values must be packed into a `Values`.
def f(a, b, cc):
return Values(2 * a, 3 * b)
test[f(3, 4) == Values(6, 12)]
x, y = f(3, 4)
test[x == 6 and y == 12]
def g(a, b):
# `f` packs its multiple return values into a `Values`,
# so we can use an unpacking assignment to extract them.
x, y = call_cc[f(a, b)]
return x, y
fail["This line should not be reached."] # pragma: no cover
test[g(3, 4) == (6, 12)]
# Unpacking into a star-target (as the last target) sends any
# remaining positional return values there, as a tuple.
xs, *a = call_cc[f(1, 2)]
test[xs == 2 and a == (6,)]
# an "and" or "or" return value may have a tail-call in the last item
with testset("tail call in logical expressions"):
with continuations:
# "or"
def h1(a, b):
x, y = call_cc[f(a, b)]
return None or f(3, 4) # the f from the previous "with continuations" block
test[h1(3, 4) == Values(6, 12)]
def h2(a, b):
x, y = call_cc[f(a, b)]
return True or f(3, 4) # noqa: SIM222 -- testing short-circuit with continuations
test[h2(3, 4) is True]
# "or" with 3 or more items (testing; handled differently internally)
def h3(a, b):
x, y = call_cc[f(a, b)]
return None or False or f(3, 4)
test[h3(3, 4) == Values(6, 12)]
def h4(a, b):
x, y = call_cc[f(a, b)]
return None or True or f(3, 4) # noqa: SIM222 -- testing short-circuit with continuations
test[h4(3, 4) is True]
def h5(a, b):
x, y = call_cc[f(a, b)]
return 42 or None or f(3, 4) # noqa: SIM222 -- testing short-circuit with continuations
test[h5(3, 4) == 42]
# "and"
def i1(a, b):
x, y = call_cc[f(a, b)]
return True and f(3, 4)
test[i1(3, 4) == Values(6, 12)]
def i2(a, b):
x, y = call_cc[f(a, b)]
return False and f(3, 4) # noqa: SIM223 -- testing short-circuit with continuations
test[i2(3, 4) is False]
# "and" with 3 or more items
def i3(a, b):
x, y = call_cc[f(a, b)]
return True and 42 and f(3, 4)
test[i3(3, 4) == Values(6, 12)]
def i4(a, b):
x, y = call_cc[f(a, b)]
return True and False and f(3, 4) # noqa: SIM223 -- testing short-circuit with continuations
test[i4(3, 4) is False]
def i5(a, b):
x, y = call_cc[f(a, b)]
return None and False and f(3, 4) # noqa: SIM223 -- testing short-circuit with continuations
test[i5(3, 4) is False]
# combination of "and" and "or"
def j1(a, b):
x, y = call_cc[f(a, b)]
return None or True and f(3, 4)
test[j1(3, 4) == Values(6, 12)]
with testset("let in tail position"):
with continuations:
def j2(a, b):
x, y = call_cc[f(a, b)]
return let[[c << a, # noqa: F821
d << b] in f(c, d)] # noqa: F821
test[j2(3, 4) == Values(6, 12)]
with testset("if-expression in tail position"):
with continuations:
def j3(a, b):
x, y = call_cc[f(a, b)]
return f(a, b) if True else None
test[j3(3, 4) == Values(6, 12)]
def j4(a, b):
x, y = call_cc[f(a, b)]
return None if False else f(a, b)
test[j4(3, 4) == Values(6, 12)]
with testset("integration with a lambda that has TCO"):
with continuations:
fact = trampolined(withself(lambda self, n, acc=1:
acc if n == 0 else jump(self, n - 1, n * acc)))
test[fact(5) == 120]
test[returns_normally(fact(5000))] # no crash
with testset("integration with @call_ec"):
with continuations:
def g(x, cc):
return 2 * x
@call_ec
def result(ec):
ec(g(21))
test[result == 42]
@call_ec
def result(ec):
return ec(42) # doesn't need the "return"; the macro eliminates it
test[result == 42]
test[call_ec(lambda ec: ec(42)) == 42]
# # ec doesn't work from inside a continuation, because the function
# # containing the "call_cc" actually tail-calls the continuation and exits.
# @call_ec
# def doit(ec):
# x = call_cc[g(21)]
# ec(x) # we're actually outside doit(); ec no longer valid
#
# # Even this only works the first time; if you stash the cc and
# # call it later (to re-run the continuation, at that time
# # result() will already have exited so the ec no longer works.
# # (That's just the nature of exceptions, used to implement ec.)
# @call_ec
# def result(ec):
# def doit():
# x = call_cc[g(21)]
# ec(x)
# r = doit() # don't tail-call it; result() must be still running when the ec is invoked
# return r
# test[result == 42]
with testset("integration with autocurry"):
def testcurrycombo():
with continuations:
from ...fun import curry # TODO: can't rename the import, unpythonic.syntax.util.sort_lambda_decorators won't detect it
# Currying here makes no sense, but we test that it expands correctly.
# We should get trampolined(curry(call_ec(...))), which produces the desired result.
test[call_ec(curry(lambda ec: ec(42))) == 42]
testcurrycombo()
# This version auto-inserts curry after the inner macros have expanded.
# This should work, too.
with autocurry:
with continuations:
test[call_ec(lambda ec: ec(42)) == 42]
with testset("call/cc example from On Lisp, p. 261, pythonified"):
with continuations:
k = None # kontinuation
def setk(*args, cc):
nonlocal k
k = cc # current continuation, i.e. where to go after setk() finishes
xs = list(args)
# - not "return list(args)" because that would be a tail call,
# and list() is a regular function, not a continuation-enabled one
# (so it would immediately terminate the TCO chain; besides,
# it takes only 1 argument and doesn't know what to do with "cc".)
return xs
def doit():
lst = ['the call returned']
more = call_cc[setk('A')]
return lst + more # The remaining stmts in the body are the continuation.
test[doit() == ['the call returned', 'A']]
# We can now send stuff into k, as long as it conforms to the
# signature of the assignment targets of the "call_cc".
test[k(['again']) == ['the call returned', 'again']]
test[k(['thrice', '!']) == ['the call returned', 'thrice', '!']]
with testset("multiple-return-values, starred assignment target"):
with continuations:
k = None # kontinuation
def setk(*args, cc): # noqa: F811, the previous one is no longer used.
nonlocal k
k = cc # current continuation, i.e. where to go after setk() finishes
return Values(*args) # multiple-return-values
def doit():
lst = ['the call returned']
*more, = call_cc[setk('A')]
return lst + list(more)
test[doit() == ['the call returned', 'A']]
# We can now send stuff into k, as long as it conforms to the
# signature of the assignment targets of the "call_cc".
test[k('again') == ['the call returned', 'again']]
test[k('thrice', '!') == ['the call returned', 'thrice', '!']]
with testset("integration with named return values"):
# Named return values aren't supported as assignment targets in a `call_cc[]`
# due to syntactic limitations. But they can be used elsewhere in continuation-enabled code.
with continuations:
def f1(x, y):
return Values(x=x, y=y) # named return values
def f2(*, x, y): # note keyword-only parameters
return x, y # one return value, a tuple (for multiple-return-values, use `Values(...)`)
# Think through carefully what this does: call `f1`, chain to `f2` as the continuation.
# The continuation is set here by explicitly providing a value for the implicit `cc` parameter.
#
# The named return values from `f1` are then unpacked, by the continuation machinery,
# into the kwargs of `f2`. Then `f2` takes those, and returns a tuple.
test[f1(2, 3, cc=f2) == (2, 3)]
with testset("top level call_cc"):
# A top-level "call_cc" is also allowed.
#
# In that case the continuation always returns None, because the original
# use site was not a function.
vals = 1, 2
with continuations:
k = None
def setk(*args, cc): # noqa: F811, the previous one is no longer used.
nonlocal k
k = cc
return Values(*args) # multiple-return-values
x, y = call_cc[setk(*vals)]
test[x, y == vals]
# end the block to end capture, and start another one to resume programming
# in continuation-enabled mode.
with continuations:
vals = 3, 4
test[k(*vals) is None]
vals = 5, 6
test[k(*vals) is None]
with testset("conditional top-level call_cc"):
with continuations:
x = call_cc[setk("yes") if 42 % 2 == 0 else None]
test[x == "yes"]
x = call_cc[None if 42 % 2 == 0 else setk("yes")]
test[x is None]
with testset("integration with multilambda"):
with multilambda, continuations:
out = []
f = lambda x: [out.append(x), x**2]
test[f(42) == 1764 and out == [42]]
with testset("depth-first tree traversal from On Lisp, p. 271"):
def atom(x):
return not isinstance(x, (list, tuple))
t1 = ["a", ["b", ["d", "h"]], ["c", "e", ["f", "i"], "g"]]
t2 = [1, [2, [3, 6, 7], 4, 5]]
out = ""
def dft(tree): # classical, no continuations
if not tree:
return
if atom(tree):
nonlocal out
out += tree
return
first, *rest = tree
dft(first)
dft(rest)
dft(t1)
test[out == "abdhcefig"]
with continuations:
saved = []
def dft_node(tree, cc):
if not tree:
return restart()
if atom(tree):
return tree
first, *rest = tree
ourcc = cc # capture our current continuation
# override default continuation in the tail-call in the lambda
saved.append(lambda: dft_node(rest, cc=ourcc))
return dft_node(first)
def restart():
if saved:
f = saved.pop()
return f()
else:
return "done"
out = ""
def dft2(tree):
nonlocal saved
saved = []
node = call_cc[dft_node(tree)]
if node == "done":
return "done"
nonlocal out # must be placed after call_cc[]; we write to out **in the continuation part**
out += node
return restart()
dft2(t1)
test[out == "abdhcefig"]
# The continuation version allows to easily walk two trees simultaneously,
# generating their cartesian product (example from On Lisp, p. 272):
def treeprod(ta, tb):
node1 = call_cc[dft_node(ta)]
if node1 == "done":
return "done"
node2 = call_cc[dft_node(tb)]
return [node1, node2]
out = []
x = treeprod(t1, t2)
while x != "done":
out.append(x)
x = restart()
test[out == [['a', 1], ['a', 2], ['a', 3], ['a', 6], ['a', 7], ['a', 4], ['a', 5],
['b', 1], ['b', 2], ['b', 3], ['b', 6], ['b', 7], ['b', 4], ['b', 5],
['d', 1], ['d', 2], ['d', 3], ['d', 6], ['d', 7], ['d', 4], ['d', 5],
['h', 1], ['h', 2], ['h', 3], ['h', 6], ['h', 7], ['h', 4], ['h', 5],
['c', 1], ['c', 2], ['c', 3], ['c', 6], ['c', 7], ['c', 4], ['c', 5],
['e', 1], ['e', 2], ['e', 3], ['e', 6], ['e', 7], ['e', 4], ['e', 5],
['f', 1], ['f', 2], ['f', 3], ['f', 6], ['f', 7], ['f', 4], ['f', 5],
['i', 1], ['i', 2], ['i', 3], ['i', 6], ['i', 7], ['i', 4], ['i', 5],
['g', 1], ['g', 2], ['g', 3], ['g', 6], ['g', 7], ['g', 4], ['g', 5]]]
# maybe more pythonic to make it a generator?
#
# We can define and use this outside the block, since at this level
# we don't need to manipulate cc.
#
# (We could as well define and use it inside the block.)
def treeprod_gen(ta, tb):
x = treeprod(t1, t2)
while x != "done":
yield x
x = restart()
out2 = list(treeprod_gen(t1, t2))
test[out2 == out]
# The most pythonic way, of course, is to define dft as a generator,
# since that already provides suspend-and-resume (a.k.a. single-shot continuations)...
def dft3(tree):
if not tree:
return
if atom(tree):
yield tree
return
first, *rest = tree
yield from dft3(first)
yield from dft3(rest)
test[list(dft3(t1)) == [x for x in "abdhcefig"]]
# McCarthy's amb operator is very similar to dft, if a bit shorter:
with testset("McCarthy's amb operator (the real deal)"):
with continuations:
stack = []
def amb(lst, cc):
if not lst:
return fail()
first, *rest = tuple(lst)
if rest:
# Note even the `lambda` below has an implicit `cc` parameter;
# hence we must name the current `cc` to something else to be
# able to use the value inside the `lambda`.
ourcc = cc
stack.append(lambda: amb(rest, cc=ourcc))
return first
def fail(): # noqa: F811, not redefining, the first one is a macro.
if stack:
f = stack.pop()
return f()
# testing
test[amb(()) is None]
def doit1():
c1 = call_cc[amb((1, 2, 3))]
c2 = call_cc[amb((10, 20))]
if c1 and c2:
return c1 + c2
test[doit1() == 11]
# How this differs from a comprehension is that we can fail()
# **outside** the dynamic extent of doit1. Doing that rewinds,
# and returns the next value. The control flow state is kept
# on the continuation stack just like in Scheme/Racket.
#
# (The last call_cc[] is the innermost loop.)
test[fail() == 21]
test[fail() == 12]
test[fail() == 22]
test[fail() == 13]
test[fail() == 23]
test[fail() is None]
def doit2():
c1 = call_cc[amb((1, 2, 3))]
c2 = call_cc[amb((10, 20))]
if c1 + c2 != 22: # we can require conditions like this
return fail()
return c1, c2
test[doit2() == (2, 20)]
test[fail() is None]
# Pythagorean triples, pythonic way (to generate a reference solution)
def pt_gen(maxn):
for z in range(1, maxn + 1):
for y in range(1, z + 1):
for x in range(1, y + 1):
if x * x + y * y != z * z:
continue
yield x, y, z
pts = list(pt_gen(20))
with continuations:
# Pythagorean triples.
count = 0
def pt(maxn):
# This generates 1540 combinations, with several nested tail-calls each,
# so we really need TCO here. Without TCO, nothing would return until
# the whole computation is done; it would blow the call stack very quickly.
# With TCO, it's just a case of "lambda, the ultimate goto".
z = call_cc[amb(range(1, maxn + 1))]
y = call_cc[amb(range(1, z + 1))]
x = call_cc[amb(range(1, y + 1))]
nonlocal count
count += 1
if x * x + y * y != z * z:
return fail()
return x, y, z
out = []
x = pt(20)
while x is not None:
out.append(x)
x = fail()
test[out == pts]
print(f"combinations tested for Pythagorean triples: {count:d}")
with testset("integration with autoreturn"):
with autoreturn, continuations:
stack = []
def amb(lst, cc): # noqa: F811, the previous one is no longer used.
if lst:
first, *rest = tuple(lst)
if rest:
ourcc = cc
stack.append(lambda: amb(rest, cc=ourcc))
first
else:
fail()
def fail():
if stack:
f = stack.pop()
f()
# testing
test[amb(()) is None]
def pt(maxn):
z = call_cc[amb(range(1, maxn + 1))]
y = call_cc[amb(range(1, z + 1))]
x = call_cc[amb(range(1, y + 1))]
if x * x + y * y == z * z:
x, y, z
else:
fail()
out = []
x = pt(20)
while x is not None:
out.append(x)
x = fail()
test[out == pts]
with testset("integration with autoreturn and autocurry simultaneously"):
with autocurry: # major slowdown, but works
with autoreturn, continuations:
stack = []
def amb(lst, cc): # noqa: F811, the previous one is no longer used.
if lst:
first, *rest = tuple(lst)
if rest:
ourcc = cc
stack.append(lambda: amb(rest, cc=ourcc))
first
else:
fail()
def fail():
if stack:
f = stack.pop()
f()
# testing
test[amb(()) is None]
def pt(maxn):
z = call_cc[amb(range(1, maxn + 1))]
y = call_cc[amb(range(1, z + 1))]
x = call_cc[amb(range(1, y + 1))]
if x * x + y * y == z * z:
x, y, z
else:
fail()
out = []
x = pt(20)
while x is not None:
out.append(x)
x = fail()
test[out == pts]
with testset("integration with @looped (unpythonic.fploop)"):
with continuations:
k = None
def setk(cc):
nonlocal k
k = cc
out = []
@looped
def s(loop, acc=0):
call_cc[setk()]
out.append(acc)
if acc < 10:
return loop(acc + 1)
return acc
test[tuple(out) == tuple(range(11))]
test[s == 10]
s = k() # k is re-captured at each iteration, so now acc=10...
test[tuple(out) == tuple(range(11)) + (10,)]
test[s == 10]
# To be able to resume from an arbitrary iteration, we need something like...
with continuations:
k = None
def setk(x, cc): # pass x through; as a side effect, set k
nonlocal k
k = cc
return x
out = []
@looped
def s(loop, acc=0):
acc = call_cc[setk(acc)]
out.append(acc)
if acc < 10:
return loop(acc + 1)
return acc
test[tuple(out) == tuple(range(11))]
test[s == 10]
s = k(5) # send in the new initial acc
test[tuple(out) == tuple(range(11)) + tuple(range(5, 11))]
test[s == 10]
# To always resume from the beginning, we can do something like this...
with continuations:
k = None
def setk(acc, cc):
nonlocal k
# because call_cc[] must be at the top level of a def,
# we refactor the "if" here (but see below).
if acc == 0:
k = cc
out = []
@looped
def s(loop, acc=0):
call_cc[setk(acc)]
out.append(acc)
if acc < 10:
return loop(acc + 1)
return acc
test[tuple(out) == tuple(range(11))]
test[s == 10]
s = k()
test[tuple(out) == 2 * tuple(range(11))]
test[s == 10]
# To eliminate the passing of acc into setk, let's use a closure:
with continuations:
k = None
out = []
@looped
def s(loop, acc=0):
def setk(cc):
nonlocal k
if acc == 0:
k = cc
call_cc[setk()]
out.append(acc)
if acc < 10:
return loop(acc + 1)
return acc
test[tuple(out) == tuple(range(11))]
test[s == 10]
s = k()
test[tuple(out) == 2 * tuple(range(11))]
test[s == 10]
# conditional call_cc[f(...) if p else g(...)]
# each of the calls f(...), g(...) may be replaced with None, which means
# proceed directly to the cont, setting assignment targets (if any) to None.
with testset("conditional call_cc syntax"):
with continuations:
k = None
def setk(cc):
nonlocal k
k = cc
out = []
@looped
def s(loop, acc=0):
call_cc[setk() if acc == 0 else None]
out.append(acc)
if acc < 10:
return loop(acc + 1)
return acc
test[tuple(out) == tuple(range(11))]
test[s == 10]
s = k()
test[tuple(out) == 2 * tuple(range(11))]
test[s == 10]
# As of 0.15.1, the preferred way of working with continuations is as follows.
#
# The pattern `k = call_cc[get_cc()]` covers the 99% common case where you
# just want to snapshot and save the control state into a local variable.
#
# See docstring of `unpythonic.syntax.get_cc` for more. It's a regular function
# that works together with the `call_cc` macro.
with testset("get_cc, the less antisocial little sister of call_cc"):
with continuations:
def append_stuff_to(lst):
lst.append("one")
k = call_cc[get_cc()]
lst.append("two")
return k
lst = []
k = append_stuff_to(lst)
test[lst == ["one", "two"]]
# invoke the continuation
k(k) # send `k` back in as argument so it the continuation sees it as its local `k`
test[lst == ["one", "two", "two"]]
# If your continuation needs to take arguments, `get_cc` can also make a parametric continuation:
with testset("get_cc with parametric continuation"):
with continuations:
def append_stuff_to(lst):
# Important: in the `get_cc` call, the initial values for
# the additional arguments, if any, must be passed positionally,
# due to `call_cc` syntax limitations.
k, x1, x2 = call_cc[get_cc(1, 2)]
lst.extend([x1, x2])
return k
lst = []
k = append_stuff_to(lst)
test[lst == [1, 2]]
# invoke the continuation, sending both `k` and our additional arguments.
k(k, 3, 4)
test[lst == [1, 2, 3, 4]]
# When invoking the continuation, the additional arguments can be passed
# in any way allowed by Python.
k(k, x1=5, x2=6)
test[lst == [1, 2, 3, 4, 5, 6]]
# You can also abuse `k` to pass an arbitrary object, if inside the
# continuation, you don't need a reference to the continuation itself.
# This is the lispy solution.
#
# Then you can `iscontinuation(k)` to check whether it is a continuation
# (first run, return value of `get_cc()`), or something else (second and
# further runs, a value sent in via the continuation).
#
# Whether this or the previous example is more pythonic is left as an
# exercise to the reader.
#
# In this solution, be careful, if you need to send in a continuation
# function for some reason. It is impossible to be 100% sure whether `k`
# is *the* continuation that should have been returned by *this* `get_cc`.
# If you need to send in a continuation function, box it (in a read-only
# `Some` box, even), to make it explicit that it's intended as data.
with testset("get_cc lispy style"):
with continuations:
# The pattern
#
# k = call_cc[get_cc()]
# if iscontinuation(k):
# return k
#
# creates a multi-shot resume point. See also `test_conts_multishot.py`.
def append_stuff_to(lst):
... # could do something useful here (otherwise, why make a continuation?)
k = call_cc[get_cc()]
# <-- the resume point is here, with `k` set to "the return value of the `call_cc`",
# i.e. the continuation during the first run, and whatever was sent in during later runs.
# In 0.15.1+, continuation functions created by the `call_cc[...]` macro are
# tagged, and can be detected using `unpythonic.syntax.iscontinuation`, which
# is a regular function:
if iscontinuation(k): # first run; just return the continuation
return k
# invoked via continuation, now `k` is input data instead of a continuation
x1, x2 = k
lst.extend([x1, x2])
return None
lst = []
k = append_stuff_to(lst)
k([1, 2]) # whatever object we send in becomes the local `k` in the continuation.
test[lst == [1, 2]]
k([3, 4])
test[lst == [1, 2, 3, 4]]
with testset("scoping, locals only"):
# This is the cleanest way to scope your local variables in continuations:
# just accept the fact that each continuation introduces a scope boundary.
with continuations:
def f():
# Original function scope
x = None
# Continuation 1 scope begins here
# (from the statement following `call_cc` onward, but including the `k1`)
k1 = call_cc[get_cc()]
if iscontinuation(k1):
# This `x` is local to continuation 1.
x = "cont 1 first time"
return k1, x
# Continuation 2 scope begins here
k2 = call_cc[get_cc()]
if iscontinuation(k2):
# This `x` is local to continuation 2.
x = "cont 2 first time"
return k2, x
# Still in continuation 2, so this is the `x` of continuation 2.
x = "cont 2 second time"
return None, x
k1, x = f()
test[x == "cont 1 first time"]
k2, x = k1(None) # when resuming, send `None` as the new value of variable `k1` in continuation 1
test[x == "cont 2 first time"]
k3, x = k2(None)
test[k3 is None]
test[x == "cont 2 second time"]
k2, x = k1(None) # multi-shotting from earlier resume point
test[x == "cont 2 first time"]
# TODO: This breaks the coverage analyzer, because 'name 'x' is assigned to before nonlocal declaration'.
# TODO: Fair enough, that's not standard Python. So let's just disable this for now.
# with testset("scoping, in presence of nonlocal"):
# # TODO: better example
# # It shouldn't matter in this particular example whether we declare the `x`
# # in the continuations `nonlocal`, because once the parent returns, the
# # only places that can access its locals *from that activation* are the
# # continuation closures *created by that activation*.
# with continuations:
# def f():
# # Original function scope
# x = None
#
# # Continuation 1 scope begins here
# # (from the statement following `call_cc` onward, but including the `k1`)
# k1 = call_cc[get_cc()]
# nonlocal x # <-- IMPORTANT
# if iscontinuation(k1):
# # This is now the original `x`.
# x = "cont 1 first time"
# return k1, x
#
# # Continuation 2 scope begins here
# k2 = call_cc[get_cc()]
# nonlocal x # <-- IMPORTANT
# if iscontinuation(k2):
# # This too is the original `x`.
# x = "cont 2 first time"
# return k2, x
#
# # Still the original `x`.
# x = "cont 2 second time"
# return None, x
#
# k1, x = f()
# test[x == "cont 1 first time"]
# k2, x = k1(None) # when resuming, send `None` as the new value of variable `k1` in continuation 1
# test[x == "cont 2 first time"]
# k3, x = k2(None)
# test[k3 is None]
# test[x == "cont 2 second time"]
#
# k2, x = k1(None) # multi-shotting from earlier resume point
# test[x == "cont 2 first time"]
# If you need to scope like `nonlocal`, use the classic solution: box the value,
# so you have no need to overwrite the name; you can replace the thing in the box.
#
# (Classic from before `nonlocal` declarations were a thing. They were added in 3.0;
# for historical interest, see https://www.python.org/dev/peps/pep-3104/ )
with testset("scoping, using a box"):
with continuations:
# poor man's execution trace
def make_tracing_box_updater(thebox, trace):
def update(value):
trace.append(f"old: {unbox(thebox)}")
thebox << value
trace.append(f"new: {unbox(thebox)}")
return value
return update
# If we wanted to replace the list instance later, we could pass the list in a box, too.
def f(lst):
# Now there is just one `x`, which is the box; we just update the contents.
# Original function scope
x = box("f")
lst.append(f"initial: {unbox(x)}")
update = make_tracing_box_updater(x, lst)
# Continuation 1 scope begins here
# (from the statement following `call_cc` onward, but including the `k1`)
k1 = call_cc[get_cc()]
if iscontinuation(k1):
return k1, update("k1 first")
update("k1 again")
# Continuation 2 scope begins here
k2 = call_cc[get_cc()]
if iscontinuation(k2):
return k2, update("k2 first")
update("k2 again")
return None, unbox(x)
trace = []
k1, x = f(trace)
test[x == "k1 first"]
test[trace == ['initial: f', 'old: f', 'new: k1 first']]
k2, x = k1(None) # when resuming, send `None` as the new value of variable `k1` in continuation 1
test[x == "k2 first"]
test[trace == ['initial: f', 'old: f', 'new: k1 first',
'old: k1 first', 'new: k1 again', 'old: k1 again', 'new: k2 first']]
k3, x = k2(None)
test[k3 is None]
test[x == "k2 again"]
test[trace == ['initial: f', 'old: f', 'new: k1 first',
'old: k1 first', 'new: k1 again', 'old: k1 again', 'new: k2 first',
'old: k2 first', 'new: k2 again']]
k2, x = k1(None) # multi-shotting from earlier resume point
test[x == "k2 first"]
test[trace == ['initial: f', 'old: f', 'new: k1 first',
'old: k1 first', 'new: k1 again', 'old: k1 again', 'new: k2 first',
'old: k2 first', 'new: k2 again',
'old: k2 again', 'new: k1 again', 'old: k1 again', 'new: k2 first']]
# ^^^^^^^^^^^^^^^ state as left by `k2` before the multi-shot
if __name__ == '__main__': # pragma: no cover
with session(__file__):
runtests()