omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / seed / qualification / om2 / oracle / s4fuel / fuel.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # E14 fuel model (oracle, outside the delivered closure; Python 3.12.3). Fuel 65536 per function; each copy |
| 4 | # of an unrolled body and each inline expansion costs 1, charged when that copy/expansion starts |
| 5 | # (pre-order). The unit that would take the total above 65536 exhausts the fuel; its loop or call line is |
| 6 | # the error line. Prints, per fixture, the total cost or the exhausting line. |
| 7 | FUEL = 65536 |
| 8 | |
| 9 | def run(tree): |
| 10 | used = [0] |
| 11 | def walk(node): # node = (line, count, children) ; count copies, each charged then children walked |
| 12 | line, count, kids = node |
| 13 | for _ in range(count): |
| 14 | used[0] += 1 |
| 15 | if used[0] > FUEL: |
| 16 | raise StopIteration(line) |
| 17 | for k in kids: |
| 18 | walk(k) |
| 19 | try: |
| 20 | for t in tree: |
| 21 | walk(t) |
| 22 | except StopIteration as e: |
| 23 | return "exhausted at line %d" % e.value |
| 24 | return "total %d" % used[0] |
| 25 | |
| 26 | def nest(depth, first_line): |
| 27 | node = None |
| 28 | for d in reversed(range(depth)): |
| 29 | node = (first_line + d, 64, [node] if node else []) |
| 30 | return [node] |
| 31 | |
| 32 | print("fuel-d2", run(nest(2, 5))) |
| 33 | print("fuel-d3", run(nest(3, 5))) |
| 34 | print("fuel-d4", run(nest(4, 5))) |
| 35 | # fuel-inl: main (line 21) calls inline f1 once; f1 calls f2 64 times (lines 6..), each f2 calls f3 64 |
| 36 | # times, each f3 calls f4 64 times; f4 is empty. Lines are those of fuel-inl.om (see its notes). |
| 37 | def inl(): |
| 38 | f4 = [] |
| 39 | f3 = [(7, 64, [(8, 1, f4)])] |
| 40 | f2 = [(12, 64, [(13, 1, f3)])] |
| 41 | f1 = [(17, 64, [(18, 1, f2)])] |
| 42 | return [(22, 1, f1)] |
| 43 | print("fuel-inl", run(inl())) |