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_e16.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # E16 fuel model (oracle, outside the delivered closure; Python 3.12.3). Fuel 65536 per function. An |
| 4 | # unrolled loop is charged K when it starts, at its own line, before any copy expands; an inline expansion |
| 5 | # is charged 1 when it starts, at the call line. Error 14 names the construct whose charge exceeds the fuel |
| 6 | # (total > 65536; a total of exactly 65536 is still within the fuel). |
| 7 | FUEL = 65536 |
| 8 | class Out(Exception): pass |
| 9 | |
| 10 | def run(items): |
| 11 | used = [0] |
| 12 | def charge(n, line): |
| 13 | used[0] += n |
| 14 | if used[0] > FUEL: |
| 15 | raise Out(line) |
| 16 | def walk(it): |
| 17 | kind, line, k, body = it |
| 18 | if kind == "loop": |
| 19 | charge(k, line) |
| 20 | for _ in range(k): |
| 21 | for b in body: walk(b) |
| 22 | else: # inline call |
| 23 | charge(1, line) |
| 24 | for b in body: walk(b) |
| 25 | try: |
| 26 | for it in items: walk(it) |
| 27 | except Out as e: |
| 28 | return "exhausted at line %d (used %d)" % (e.args[0], used[0]) |
| 29 | return "total %d" % used[0] |
| 30 | |
| 31 | def nest(depth, first): |
| 32 | node = [] |
| 33 | for d in reversed(range(depth)): |
| 34 | node = [("loop", first + d, 64, node)] |
| 35 | return node |
| 36 | |
| 37 | print("fuel-d2", run(nest(2, 5))) |
| 38 | print("fuel-d3", run(nest(3, 5))) |
| 39 | print("fuel-d4", run(nest(4, 5))) |
| 40 | f4 = [] |
| 41 | f3 = [("loop", 7, 64, [("call", 8, 0, f4)])] |
| 42 | f2 = [("loop", 12, 64, [("call", 13, 0, f3)])] |
| 43 | f1 = [("loop", 17, 64, [("call", 18, 0, f2)])] |
| 44 | print("fuel-inl", run([("call", 22, 0, f1)])) |