| 1 | module scheme |
| 2 | |
| 3 | import reader |
| 4 | import arena |
| 5 | import context |
| 6 | |
| 7 | struct IntegerTailObservation { |
| 8 | text string |
| 9 | steps int |
| 10 | error string |
| 11 | effects string |
| 12 | } |
| 13 | |
| 14 | // Execute unchanged bytecode one instruction at a time as an independent |
| 15 | // reference. In particular, do not infer step costs from the proposed plan. |
| 16 | fn integer_tail_observe(source string, single bool, limit int) !IntegerTailObservation { |
| 17 | mut vm := new_with_options(Options{max_steps: limit, max_frames: 0})! |
| 18 | invocation := vm.next_context() |
| 19 | function := vm.new_source_program(reader.read_all(source)!, invocation, unsafe { nil }, '', false)! |
| 20 | original := function.instructions.clone() |
| 21 | mut machine := Machine{function: function, context: invocation, logical_context: invocation} |
| 22 | entry := vm.enter_program(mut machine)! |
| 23 | defer { vm.leave_entry(entry) } |
| 24 | mut message := '' |
| 25 | mut value := unspecified() |
| 26 | if single { |
| 27 | for !machine.finished { |
| 28 | assert machine.steps < 100000 |
| 29 | ins := machine.function.instructions[machine.ip] |
| 30 | machine.ip++ |
| 31 | machine.origin = ins.origin |
| 32 | machine.charge(limit) or { message = machine.located(err).msg(); break } |
| 33 | machine.execute(mut vm, ins) or { message = machine.located(err).msg(); break } |
| 34 | } |
| 35 | if message == '' { value = machine.stack[0] } |
| 36 | } else { |
| 37 | value = machine.run(mut vm) or { message = err.msg(); unspecified() } |
| 38 | } |
| 39 | assert function.instructions == original |
| 40 | effect := vm.lookup('effects') or { unspecified() } |
| 41 | return IntegerTailObservation{ |
| 42 | text: if message == '' { write(value)! } else { '' } |
| 43 | steps: machine.steps |
| 44 | error: message |
| 45 | effects: write(effect)! |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | fn integer_tail_compare(source string, expected string) ! { |
| 50 | fast := integer_tail_observe(source, false, 0)! |
| 51 | plain := integer_tail_observe(source, true, 0)! |
| 52 | assert fast.error == '' && fast.text == expected, source |
| 53 | assert fast == plain, source |
| 54 | } |
| 55 | |
| 56 | fn test_integer_tail_loop_both_branches_and_parallel_updates() { |
| 57 | for source, expected in { |
| 58 | '(define (f n a)(if (= n 0) a (f (- n 1)(+ a n))))(f 10 0)': '55' |
| 59 | '(define (f n a)(if (> n 0) (f (- n 1)(+ a n)) a))(f 10 0)': '55' |
| 60 | '(define (f n a)(if (<= n 0) a (f (- n 1)(* a n))))(f 6 1)': '720' |
| 61 | '(let loop ((n 10)(a 0))(if (= n 0) a (loop (- n 1)(+ a n))))': '55' |
| 62 | '(define (f end)(let loop ((n 0)(a 0))(if (> n end) a (loop (+ n 1)(+ a n)))))(f 10)': '55' |
| 63 | '(define bound 10)(define(f n a)(if (>= n bound) a (f (+ n 1)(+ a n))))(f 0 0)': '45' |
| 64 | '(define(f a b)(if (> a 30) (list a b) (f (+ a b)(+ a 1))))(f 1 2)': '(41 26)' |
| 65 | '(define same =)(define down -)(define combine +)(define(f n a)(if (same n 0) a (f (down n 1)(combine a n))))(f 10 0)': '55' |
| 66 | } { integer_tail_compare(source, expected)! } |
| 67 | } |
| 68 | |
| 69 | fn test_integer_tail_loop_exact_budget_boundaries_and_effect_prefixes() { |
| 70 | for source in [ |
| 71 | '(define effects 0)(define(f n a)(if (= n 0)(begin(set! effects a)a)(f (- n 1)(+ a n))))(f 3 0)', |
| 72 | '(define effects 0)(define(f n a)(if (> n 0)(f (- n 1)(+ a n))(begin(set! effects a)(car 1))))(f 3 0)', |
| 73 | '(define effects 0)(let loop((n 3)(a 9223372036854775806))(if (= n 0)(begin(set! effects a)a)(loop (- n 1)(+ a n))))', |
| 74 | ] { |
| 75 | for limit in 0 .. 180 { |
| 76 | fast := integer_tail_observe(source, false, limit)! |
| 77 | plain := integer_tail_observe(source, true, limit)! |
| 78 | assert fast == plain, '${limit}: ${source}: ${fast} != ${plain}' |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | fn test_integer_tail_loop_promotes_each_arithmetic_overflow_edge() { |
| 84 | for source, expected in { |
| 85 | '(define(f n a)(if (= n 0) a (f (- n 1)(+ a n))))(f 2 9223372036854775806)': '9223372036854775809' |
| 86 | '(define(f n a)(if (= n 0) a (f (- n 1)(- a n))))(f 2 -9223372036854775807)': '-9223372036854775810' |
| 87 | '(define(f n a)(if (= n 0) a (f (- n 1)(* a n))))(f 2 9223372036854775807)': '18446744073709551614' |
| 88 | '(define(f n a)(if (= n 0) a (f (- n 1)(* a -1))))(f 1 -9223372036854775808)': '9223372036854775808' |
| 89 | '(define(f n a)(if (< n -9223372036854775808) a (f (- n 1)(+ a 1))))(f -9223372036854775808 0)': '1' |
| 90 | '(define(f n a)(if (> n 9223372036854775807) a (f (+ n 1)(+ a n))))(f 9223372036854775807 0)': '9223372036854775807' |
| 91 | } { integer_tail_compare(source, expected)! } |
| 92 | } |
| 93 | |
| 94 | fn test_integer_tail_loop_live_operator_and_self_rebindings() { |
| 95 | mut vm := new_with_options(Options{max_steps:0,max_frames:0})! |
| 96 | vm.eval('(define(f n a)(if (= n 0) a (f (- n 1)(+ a n))))(define original f)')! |
| 97 | assert vm.eval('(f 10 0)')!.whole() == 55 |
| 98 | vm.eval('(define old-plus +)(set! +(lambda(a b) 7))')! |
| 99 | assert vm.eval('(f 10 0)')!.whole() == 7 |
| 100 | vm.eval('(set! + old-plus)(set! f(lambda(n a)(vector n a)))')! |
| 101 | assert write(vm.eval('(original 10 0)')!)! == '#(9 10)' |
| 102 | vm.eval('(set! f original)(define old-eq =)(set! =(lambda(a b)#t))')! |
| 103 | assert vm.eval('(f 10 0)')!.whole() == 0 |
| 104 | vm.eval('(set! = old-eq)(define old-minus -)(set! -(lambda(a b)0))')! |
| 105 | assert vm.eval('(f 10 0)')!.whole() == 10 |
| 106 | } |
| 107 | |
| 108 | fn test_integer_tail_loop_same_code_different_capture_environment() { |
| 109 | integer_tail_compare('(define (make n) (lambda (left sum) (if (= left 0) (+ sum n) (target (- left 1)(+ sum left))))) |
| 110 | (define first(make 100))(define target(make 1000))(first 3 0)', '1006')! |
| 111 | } |
| 112 | |
| 113 | fn test_integer_tail_loop_arbitrary_exit_and_continuation_replay() { |
| 114 | integer_tail_compare('(define effects 0)(call/cc(lambda(out)(let loop((n 10)(a 0)) |
| 115 | (if (= n 0)(begin(set! effects a)(out(vector a)))(loop (- n 1)(+ a n))))))', '#(55)')! |
| 116 | mut vm := new_with_options(Options{max_steps:0,max_frames:0})! |
| 117 | vm.eval('(define saved #f)(define result #f)(set! result (let((n(call/cc(lambda(k)(set! saved k)4)))) |
| 118 | (let loop((left n)(sum 0))(if (= left 0) sum (loop (- left 1)(+ sum left))))))')! |
| 119 | assert vm.lookup('result')!.whole() == 10 |
| 120 | gc_collect() |
| 121 | vm.eval('(saved 6)')! |
| 122 | assert vm.lookup('result')!.whole() == 21 |
| 123 | vm.eval('(saved 2)')! |
| 124 | assert vm.lookup('result')!.whole() == 3 |
| 125 | } |
| 126 | |
| 127 | fn test_integer_tail_loop_keeps_noninteger_errors_and_escaped_parameters() { |
| 128 | for source in [ |
| 129 | '(define(f n a)(if (= n 0) a (f (- n 1)(+ a n))))(f 2 #f)', |
| 130 | '(define(f n a)(if (= n 0) a (f (- n 1)(+ a n))))(f #f 0)', |
| 131 | '(define(f n a)(if (= n 0) missing (f (- n 1)(+ a n))))(f 2 0)', |
| 132 | '(let((f 0))(define(loop n a)(if (= n 0) a (f (- n 1)(+ a n))))(loop 2 0))', |
| 133 | ] { |
| 134 | fast := integer_tail_observe(source, false, 0)! |
| 135 | plain := integer_tail_observe(source, true, 0)! |
| 136 | assert fast.error != '' && fast == plain, source |
| 137 | } |
| 138 | integer_tail_compare('(define saved (list))(define(f n a)(if (= n 0) (map (lambda(g)(g)) saved) |
| 139 | (begin(set! saved(cons(lambda()a)saved))(f (- n 1)(+ a n)))))(f 4 0)', '(9 7 4 0)')! |
| 140 | } |
| 141 | |
| 142 | fn test_integer_tail_loop_vile_frontend_and_wind_callbacks() { |
| 143 | mut vm := new_with_options(Options{max_steps:0,max_frames:0})! |
| 144 | vm.eval_alt('work := fn(left,sum) { if left == 0 { sum } else { work(left - 1, sum + left) } }')! |
| 145 | assert vm.call(vm.lookup('work')!, [integer(10),integer(0)])!.whole() == 55 |
| 146 | integer_tail_compare('(define effects 0)(dynamic-wind(lambda()(set! effects 1)) |
| 147 | (lambda()(let loop((n 10)(a 0))(if (= n 0)(vector a effects)(loop (- n 1)(+ a n))))) |
| 148 | (lambda()(set! effects 2)))', '#(55 1)')! |
| 149 | } |
| 150 | |
| 151 | fn test_integer_tail_loop_effectful_recurrences_and_extra_locals_use_original_protocol() { |
| 152 | for source, expected in { |
| 153 | '(define effects 0)(define(f n a)(if (= n 0) (list a effects) (begin(set! effects (+ effects 1))(f (- n 1)(+ a n)))))(f 5 0)': '(15 5)' |
| 154 | '(define(f n a)(define next (- n 1))(if (= n 0) a (f next (+ a n))))(f 5 0)': '15' |
| 155 | '(define(f n a extra)(if (= n 0) a (f (- n 1)(+ a n) extra)))(f 5 0 42)': '15' |
| 156 | '(define(f n a . rest)(if (= n 0)(cons a rest)(f (- n 1)(+ a n) n)))(f 5 0 42)': '(15 1)' |
| 157 | '(define(g n a)(if (= n 0) a (f (- n 1)(+ a n))))(define(f n a)(if (= n 0) a (g (- n 1)(+ a n))))(f 5 0)': '15' |
| 158 | } { integer_tail_compare(source, expected)! } |
| 159 | } |
| 160 | |
| 161 | fn test_integer_tail_loop_unresolved_names_and_foreign_primitive_owners_keep_errors() { |
| 162 | for source in [ |
| 163 | '(define(f n a)(if (= n 0) a (missing (- n 1)(+ a n))))(f 3 0)', |
| 164 | '(define(f n a)(if (= n 0) a (f (- n 1)(missing a n))))(f 3 0)', |
| 165 | '(define(f n a)(if (= n 0) a (f (- n 1)(+ a missing))))(f 3 0)', |
| 166 | ] { |
| 167 | for limit in [0,30,40,50,60,70,80,90,100] { |
| 168 | fast := integer_tail_observe(source,false,limit)! |
| 169 | plain := integer_tail_observe(source,true,limit)! |
| 170 | assert fast.error != '' && fast == plain |
| 171 | } |
| 172 | } |
| 173 | mut vm := new_with_options(Options{max_steps:0,max_frames:0})! |
| 174 | mut other := new_with_options(Options{max_steps:0,max_frames:0})! |
| 175 | vm.eval('(define(f n a)(if (= n 0) a (f (- n 1)(+ a n))))')! |
| 176 | assert vm.eval('(f 5 0)')!.whole() == 15 |
| 177 | vm.define('+',other.lookup('+')!)! |
| 178 | mut message := '' |
| 179 | vm.eval('(f 5 0)') or { message=err.msg(); unspecified() } |
| 180 | assert message.contains('another VM') |
| 181 | } |
| 182 | |
| 183 | fn integer_tail_controlled_work(mut vm VM) !Value { return vm.eval('(f 10 0)') } |
| 184 | |
| 185 | fn test_integer_tail_loop_host_controls_remain_live_after_warm_call() { |
| 186 | mut vm := new_with_options(Options{max_steps:0,max_frames:0})! |
| 187 | vm.eval('(define(f n a)(if (= n 0) a (f (- n 1)(+ a n))))')! |
| 188 | assert vm.eval('(f 10 0)')!.whole() == 55 |
| 189 | mut background := context.background() |
| 190 | assert vm.with_context(mut background,integer_tail_controlled_work)!.whole() == 55 |
| 191 | mut canceled, cancel := context.with_cancel(mut background) |
| 192 | defer { cancel() } |
| 193 | cancel() |
| 194 | if _ := vm.with_context(mut canceled,integer_tail_controlled_work) { |
| 195 | assert false |
| 196 | } else { assert err is CancellationRequest } |
| 197 | assert vm.eval('(f 10 0)')!.whole() == 55 |
| 198 | } |
| 199 | |
| 200 | fn test_integer_tail_loop_bounded_owner_keeps_allocation_and_step_protocol() { |
| 201 | mut bounded := new_bounded(Options{max_steps:0,max_frames:0}, arena.ChunkLimits{max_live_bytes:32*1024*1024})! |
| 202 | defer { bounded.close() or { panic(err) } } |
| 203 | bounded.eval('(define(f n a)(if (= n 0) a (f (- n 1)(+ a n))))')! |
| 204 | assert bounded.eval('(f 10 0)')!.whole() == 55 |
| 205 | bounded.run(fn (mut vm VM) !Value { |
| 206 | vm.options = Options{max_steps:30,max_frames:0} |
| 207 | mut message := '' |
| 208 | vm.eval('(f 10 0)') or { message=err.msg(); unspecified() } |
| 209 | assert message.contains('step limit') |
| 210 | vm.options = Options{max_steps:0,max_frames:0} |
| 211 | return unspecified() |
| 212 | })! |
| 213 | assert bounded.eval('(f 10 0)')!.whole() == 55 |
| 214 | } |
| 215 | |
| 216 | fn test_integer_tail_loop_hygienic_library_fallback_does_not_freeze_shadowed_bindings() { |
| 217 | mut vm := new_with_options(Options{max_steps:0,max_frames:0})! |
| 218 | vm.eval('(define-library (tail guarded) (export sum) (import (scheme base)) |
| 219 | (begin (define(sum n a)(if (= n 0) a (sum (- n 1)(+ a n)))))) |
| 220 | (import (prefix (tail guarded) imported:))')! |
| 221 | assert vm.eval('(imported:sum 10 0)')!.whole() == 55 |
| 222 | vm.eval('(set! +(lambda(a b)999))(set! =(lambda(a b)#t))')! |
| 223 | gc_collect() |
| 224 | assert vm.eval('(imported:sum 10 0)')!.whole() == 55 |
| 225 | } |
| 226 | |
| 227 | fn test_integer_tail_all_update_pairs_keep_polarities_parallel_results_and_budgets() { |
| 228 | for source, expected in { |
| 229 | '(define(f n a)(if (= n 5) a (f (+ n 1)(+ a n))))(f 1 2)': '12' |
| 230 | '(define(f n a)(if (< n 5) (f (+ n 1)(+ a n)) a))(f 1 2)': '12' |
| 231 | '(define(f n a)(if (= n 5) a (f (+ n 1)(- a n))))(f 1 2)': '-8' |
| 232 | '(define(f n a)(if (< n 5) (f (+ n 1)(- a n)) a))(f 1 2)': '-8' |
| 233 | '(define(f n a)(if (= n 5) a (f (+ n 1)(* a n))))(f 1 2)': '48' |
| 234 | '(define(f n a)(if (< n 5) (f (+ n 1)(* a n)) a))(f 1 2)': '48' |
| 235 | '(define(f n a)(if (= n 0) a (f (- n 1)(+ a n))))(f 4 2)': '12' |
| 236 | '(define(f n a)(if (> n 0) (f (- n 1)(+ a n)) a))(f 4 2)': '12' |
| 237 | '(define(f n a)(if (= n 0) a (f (- n 1)(- a n))))(f 4 2)': '-8' |
| 238 | '(define(f n a)(if (> n 0) (f (- n 1)(- a n)) a))(f 4 2)': '-8' |
| 239 | '(define(f n a)(if (= n 0) a (f (- n 1)(* a n))))(f 4 2)': '48' |
| 240 | '(define(f n a)(if (> n 0) (f (- n 1)(* a n)) a))(f 4 2)': '48' |
| 241 | '(define(f n a)(if (= n 16) a (f (* n 2)(+ a n))))(f 1 2)': '17' |
| 242 | '(define(f n a)(if (< n 16) (f (* n 2)(+ a n)) a))(f 1 2)': '17' |
| 243 | '(define(f n a)(if (= n 16) a (f (* n 2)(- a n))))(f 1 2)': '-13' |
| 244 | '(define(f n a)(if (< n 16) (f (* n 2)(- a n)) a))(f 1 2)': '-13' |
| 245 | '(define(f n a)(if (= n 16) a (f (* n 2)(* a n))))(f 1 2)': '128' |
| 246 | '(define(f n a)(if (< n 16) (f (* n 2)(* a n)) a))(f 1 2)': '128' |
| 247 | } { integer_tail_compare(source,expected)! } |
| 248 | for source, expected in { |
| 249 | '(define(f n a)(if (> n 9223372036854775807) a (f (* n 2)(+ a 1))))(f 4611686018427387904 0)': '1' |
| 250 | '(define(f n a)(if (> n 9223372036854775807) a (f (* n 2)(+ a 1))))(f 2305843009213693952 0)': '2' |
| 251 | } { integer_tail_compare(source,expected)! } |
| 252 | for source in [ |
| 253 | '(define(f n a)(if (= n 5) a (f (+ n 1)(+ a n))))(f 1 2)', |
| 254 | '(define(f n a)(if (= n 0) a (f (- n 1)(- a n))))(f 4 2)', |
| 255 | '(define(f n a)(if (= n 16) a (f (* n 2)(* a n))))(f 1 2)', |
| 256 | ] { |
| 257 | for limit in [19,38,77,99] { |
| 258 | fast:=integer_tail_observe(source,false,limit)! |
| 259 | plain:=integer_tail_observe(source,true,limit)! |
| 260 | assert fast==plain, '${source}/${limit}' |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |