verificationdemo
cynic 3 years ago
parent 037cc760ce
commit c762a8a7b3
  1. 24
      check.py
  2. BIN
      fullans.pkl
  3. 175
      lib.py
  4. 22
      lib/booleans.klambda
  5. 2
      lol.klambda
  6. 2
      main.py
  7. 19
      probtest.py
  8. 1
      test2.kl
  9. 3
      tst.py

@ -0,0 +1,24 @@
from sys import argv
from pickle import loads
from random import randint
from lib import step
from probtest import test
p = int(argv[1])
f = open("fullans.pkl", "rb")
a = loads(f.read())
f.close()
n = int((p/100)*(len(a)) + .5)
print(f"checking {p}% of program states, {n} states total...")
allgood = True
for i in range(n):
allgood = (a[i+1] == step(a[i]))
print (
f"""all checked program states verified to be accurate!
presuming 10% of states would have been faked, the sample is representative with a probability of ~{test(10, p)}""" if allgood else "found an erroneous state -- answer fabricated or program states corrupted"
)

Binary file not shown.

175
lib.py

@ -2,6 +2,181 @@
# BET UR MAD BAKADESU HAHAHAHHAHAHAHAH # BET UR MAD BAKADESU HAHAHAHHAHAHAHAH
# t. cirno # t. cirno
"""
SHITTY HACK SOLUTION:
>find deepest function-type-thing in code block
>simplify
>repeat until deepest == code block
REAL SOLUTION:
>mark all functions in code block unexecuted
>find deepest /UNEXECUTED/ function
>etc.
"""
best_depth = -1337
best_elem = None
#ahn~
def find_deepest(code):
if not isinstance(code, list): return code
global best_elem
global best_depth
best_depth = -1337
best_elem = code
def _find_deepest(code, depth):
global best_depth
global best_elem
if not any(map(lambda e: isinstance(e, list), code)) or (code[0][0] == "identifier" and code[0][1] in ["lambda", "cond", "id"]):
if depth > best_depth:
best_depth = depth
best_elem = code
else:
for i in code:
if isinstance(i, list):
_find_deepest(i, depth+1)
return best_elem
_find_deepest(code, 0)
return best_elem
def _eval(ctx):
from functools import reduce
subs = ctx
def _ident(name):
return ("identifier", name)
if ctx[0] == _ident("lambda"):
return ("lambda", (ctx[1], ctx[2]))
elif ctx[0] == _ident("cond"):
return _execute(ctx[2], lids) if _truthy(_execute(ctx[1], lids)) else _execute(ctx[3], lids)
elif ctx[0][1] == "id":
return ctx[1] if len(ctx[1:]) == 1 else ctx[1:]
if ctx[0][1][0] == ".":
return _execute(subs[1:], lids)
elif ctx[0] == _ident("miracle"):
return _box(getattr(eval(_destr(subs[1])), _destr(subs[2]))(*[(i if type(i) == type([]) else i[1]) for i in _fixarr(subs[3])]))
elif ctx[0] == _ident("r"):
return ("string", open(_destr(subs[1]), "r").read(int(subs[2][1])))
elif ctx[0] == _ident("w"):
open(_destr(subs[1]), "w").write(_destr(subs[2]))
return subs[2]
elif ctx[0] == _ident("def"):
ids[ctx[1]] = subs[2]
return ids[ctx[1]]
elif ctx[0] == _ident("+"):
return reduce(lambda a, b: (a[0], a[1]+b[1]), subs[1:])
elif ctx[0] == _ident("-"):
return reduce(lambda a, b: (a[0], a[1]-b[1]), subs[1:])
elif ctx[0] == _ident("*"):
return reduce(lambda a, b: (a[0], a[1]*b[1]), subs[1:])
elif ctx[0] == _ident("/"):
return reduce(lambda a, b: (a[0], a[1]/b[1]), subs[1:])
elif ctx[0] == _ident("^"):
return reduce(lambda a, b: (a[0], a[1]**b[1]), subs[1:])
elif ctx[0] == _ident("=="):
return ("number", 1.0 if subs[1] == subs[2] else 0.0)
elif ctx[0] == _ident("="):
return ("number", 1.0 if str(float(subs[1][1])) == str(float(subs[2][1])) else 0.0)
elif ctx[0] == _ident(">"):
return ("number", 1.0 if subs[1][1] > subs[2][1] else 0.0)
elif ctx[0] == _ident("<"):
return ("number", 1.0 if subs[1][1] < subs[2][1] else 0.0)
elif ctx[0] == _ident("conv"):
if subs[1][1] == "number":
return (subs[1][1], float(subs[2][1]))
elif subs[1][1] == "string":
return (subs[1][1], str(subs[2][1] if not isinstance(subs[2], list) else subs[2]))
elif subs[1][1] == "identifier":
return (subs[1][1], str(subs[2][1]))
return (subs[1][1], float(subs[2][1]) if subs[1][1] == "number" else str(subs[2][1]))
elif ctx[0] == _ident("all"):
ret = [_execute(subs[1], lids)]
for statement in subs[2:]:
ret.append(_execute(statement, lids))
return ret
elif ctx[0] == _ident("at"):
return subs[2][int(subs[1][1])]
elif ctx[0] == _ident("insert"):
subs[3].insert(int(subs[1][1]), subs[2])
return subs[3]
else:
#print(f"{subs[0]} is not a valid function")
return _execute(subs, lids)
def step(state):
def _recursereplace(i, fr, to):
if type(i) == type([]) and not(i == fr):
subs = list(
map(lambda a: _recursereplace(a, fr, to), i)
)
return subs
else:
return to if i == fr else i
deepest = find_deepest(state)
#print("deepest\n", deepest)
simplified = _eval(deepest)
#print("simplified\n", simplified)
state = _recursereplace(state, deepest, simplified)
#print(state)
#input()
return state
def play(program, pp):
import pickle
def _puter2human(shit):
hack = str(shit)
acc = ""
i = 0
while i < len(hack):
c = hack[i]
if c == "[":
acc += "("
elif c == "]":
acc += ")"
elif c == "(":
racer = i
while hack[racer] != ")": racer += 1
#print(hack[i:racer+1])
acc += str(eval(hack[i:racer+1])[1]) + " "
i += 1
return acc
def treefix(a):
if not isinstance(a, list): return a
t = list(map(lambda e: treefix(e), a))
return ("unexecuted", t)
#program = treefix(program)30
print("the program:", pp)
input()
states = []
for strand in program:
strandstates = []
while True:
strand = step(strand)
strandstates.append(strand)
if strand == find_deepest(strand):
strand = step(strand)
strandstates.append(strand)
break
states.append(strandstates)
#print(strand)
#print(find_deepest(strand))
#input()
f = open("fullans.pkl", "wb")
f.write(pickle.dumps(states[0]))
f.close()
for idx, i in enumerate(states[0]):
print(f"state {idx}: {_puter2human(i)}")
print("solution:", states[0][-1][1])
def execute(program): def execute(program):
import traceback, copy, sys import traceback, copy, sys
from functools import reduce from functools import reduce

@ -3,5 +3,23 @@
(def ! (lambda (val) (def ! (lambda (val)
(cond (val) (cond (val)
0 false
1))) true)))
(def ? (lambda (val)
(cond (val)
true
false)))
(def or (lambda (a b)
(? (a b))))
(def and (lambda (a b)
(? (!
((! a) (! b))))))
(def nand (lambda (a b)
(! (and a b))))
(def xor (lambda (a b)
(and (or a b) (! (and a b)))))

@ -0,0 +1,2 @@
INCLUDE:./lib/kokoro.klambda
<3:

@ -129,4 +129,4 @@ program = structurize(lex(tokenize(preprocess(sys.argv[1]))))
#print(program) #print(program)
lib.execute(program) lib.play(program, "".join(preprocess(sys.argv[1])).replace("RDANEELOLIVAW", " "))

@ -0,0 +1,19 @@
from random import randint
def test(fakepercent, checkpercent, total=100000):
a = []
for i in range(100):
a.append(True)
for i in range(fakepercent):
a[i] = False
timeswon = 0
for i in range(total):
won = False
for i in range(checkpercent):
b = randint(0, 99)
if not a[b]: won = True
if won: timeswon += 1
return timeswon/total

@ -0,0 +1 @@
(* (^ (+ (+ (/ 24 4) 4) (- (+ 5 5) 20)) 10) (- (^ 2 4) (- 0 (+ (* 2 3) (* 2 5)))))

@ -0,0 +1,3 @@
import ctypes
print(ctypes.CDLL(None).open)
Loading…
Cancel
Save