-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcallcc.py
More file actions
113 lines (83 loc) · 2.84 KB
/
callcc.py
File metadata and controls
113 lines (83 loc) · 2.84 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
import threading
class ContExit(SystemExit):
pass
class Continuation(object):
""" created by call/cc, passed into a new thread, and used to indicate when to resume the calling thread (if at all) """
def __init__(self):
self.event = threading.Event()
self.result = None
self.args = None
self.exc = None
def throw(self, e):
if self.event.is_set():
raise StandardError('Continuation not restartable, cannot throw exception')
self.exc = e
self.event.set()
threading.current_thread.daemon = True
raise ContExit()
def __call__(self, r, *arg):
""" restart the continuation, exit current thread"""
if self.event.is_set():
raise StandardError('Continuation not restartable, cannot resume')
# don't return single element tuple (like return foo)
if arg:
r = [r]
r.extend(arg)
self.ret(r)
threading.current_thread.daemon = True
raise ContExit()
def ret(self, r):
""" set the return value of the call/cc, and tell it to resume """
if not self.event.is_set():
self.result = r
self.event.set()
def value(self):
""" block until the continuation is invoked """
self.event.wait()
if not self.exc:
return self.result
else:
exc = self.exc
if not self.exc:
exc = StandardError('no result')
raise exc
def callcc(func,*args, **kwargs):
cont = Continuation()
if args and isinstance(args[0],Continuation):
# Calling a continuation with our current continuation - so don't need a thread
callee =args[0]
callee.ret(cont)
else:
# we are invoking a function, so we create a thread, and block
def run():
try:
cont.ret(func(cont, *args, **kwargs))
except StandardError as e:
import traceback; traceback.print_exc()
cont.throw(e)
t = threading.Thread(target=run)
t.start()
# wait until the contination is invoked
return cont.value()
def add_1(r, x):
r(x+1)
def mul_8(r, x):
r(x*8)
print callcc(add_1, 1)
print callcc(mul_8, 1)
def example(r, x):
add_1(lambda v: mul_8(r,v), x)
print callcc(example, 1)
def inner(cont):
print "inner, calling cont with current cont"""
main_cont, i =callcc(cont)
print "inner, got main_cont back, calling with",i
main_cont(i)
def outer(main_cont, inside, i):
print "outer, calling inner with current cont"""
inner_cont = callcc(inside)
print "calling inner_cont with main_cont, ",i
inner_cont(main_cont,i)
print "main, calling: outer -> inner -> outer -> inner"
r = callcc(outer, inner, "butt")
print "main, got ", r