From c762a8a7b314388419df9ff80bdb6bbbefb638d9 Mon Sep 17 00:00:00 2001 From: cynic Date: Mon, 2 Jan 2023 05:39:30 -0500 Subject: [PATCH] demo stuff --- check.py | 24 ++++++ fullans.pkl | Bin 0 -> 1146 bytes lib.py | 175 +++++++++++++++++++++++++++++++++++++++++++ lib/booleans.klambda | 22 +++++- lol.klambda | 2 + main.py | 2 +- probtest.py | 19 +++++ test2.kl | 1 + tst.py | 3 + 9 files changed, 245 insertions(+), 3 deletions(-) create mode 100644 check.py create mode 100644 fullans.pkl create mode 100644 lol.klambda create mode 100644 probtest.py create mode 100644 test2.kl create mode 100644 tst.py diff --git a/check.py b/check.py new file mode 100644 index 0000000..d31a7bd --- /dev/null +++ b/check.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" +) \ No newline at end of file diff --git a/fullans.pkl b/fullans.pkl new file mode 100644 index 0000000000000000000000000000000000000000..087092281bdc4dd1e9846bce2033f88f4722d25d GIT binary patch literal 1146 zcmY+DSwj;+41kL$w@?lhv}olViXbB3fl79aLm7MX3g%mEN(ZIr7%vp7=tB;xW4x&Gk^=K#7i(gy)mY~~ z3}bza4H_Hi1JG+zv4-l(m7_g^T%ON&QbbRT%|KDwMJeblCue6T-fHlBTozo8zOWC}kATP&JGShIxkH$L+XuBf{Xr-eHXpeUlG(j`)e#R8E zaaA(hJ>YS9#Bl{Q-F*ca=ms8OLO~X&-$@13>Xd?h7y$0(fr25(ffGIy;Uf_~7U8rP zK4GDkJQc|^FPX8DJW05*?s5*STTEB z75tju*9jlwY-oHH8eWTG(;MDc!&_sp)GeWI3$@794dVVy*1uzR|4v}WDX zu#W=!WMCHXSpa(;@I?ST!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): import traceback, copy, sys from functools import reduce diff --git a/lib/booleans.klambda b/lib/booleans.klambda index d85f417..15d5329 100644 --- a/lib/booleans.klambda +++ b/lib/booleans.klambda @@ -3,5 +3,23 @@ (def ! (lambda (val) (cond (val) - 0 - 1))) \ No newline at end of file + false + 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))))) \ No newline at end of file diff --git a/lol.klambda b/lol.klambda new file mode 100644 index 0000000..b121c2e --- /dev/null +++ b/lol.klambda @@ -0,0 +1,2 @@ +INCLUDE:./lib/kokoro.klambda +<3: \ No newline at end of file diff --git a/main.py b/main.py index e0a3eeb..95b2255 100644 --- a/main.py +++ b/main.py @@ -129,4 +129,4 @@ program = structurize(lex(tokenize(preprocess(sys.argv[1])))) #print(program) -lib.execute(program) +lib.play(program, "".join(preprocess(sys.argv[1])).replace("RDANEELOLIVAW", " ")) diff --git a/probtest.py b/probtest.py new file mode 100644 index 0000000..0133b79 --- /dev/null +++ b/probtest.py @@ -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 \ No newline at end of file diff --git a/test2.kl b/test2.kl new file mode 100644 index 0000000..68e6c6e --- /dev/null +++ b/test2.kl @@ -0,0 +1 @@ +(* (^ (+ (+ (/ 24 4) 4) (- (+ 5 5) 20)) 10) (- (^ 2 4) (- 0 (+ (* 2 3) (* 2 5))))) diff --git a/tst.py b/tst.py new file mode 100644 index 0000000..f24578b --- /dev/null +++ b/tst.py @@ -0,0 +1,3 @@ +import ctypes + +print(ctypes.CDLL(None).open) \ No newline at end of file