Datasets:
Formats:
json
Languages:
English
Size:
< 1K
Tags:
recursive-self-improvement
program-synthesis
term-rewriting
automated-reasoning
formal-methods
benchmark
Download research/verifier.py from PureOne/Nhais-DQ-Recursive-Search-Contraction: direct link, hf CLI and curl.
- Browser
- Download file 3.27 kB
-
https://huggingface.co/datasets/PureOne/Nhais-DQ-Recursive-Search-Contraction/resolve/main/research/verifier.py
- Command line
-
hf download hf://datasets/PureOne/Nhais-DQ-Recursive-Search-Contraction/research/verifier.py
-
curl -L -o verifier.py https://huggingface.co/datasets/PureOne/Nhais-DQ-Recursive-Search-Contraction/resolve/main/research/verifier.py
3.27 kB
| #!/usr/bin/env python3 | |
| """Independent finite checker. Does NOT import the learner or guard compiler. | |
| Checks exact operation equivalence, strict shortlex orientation, and every | |
| transition of the generated forbidden-factor DFA against direct suffix logic. | |
| All truth claims are scoped to the declared finite primitive maps. | |
| """ | |
| from __future__ import annotations | |
| import hashlib,json | |
| from pathlib import Path | |
| def evaluate(word, maps): | |
| result=[] | |
| for i in range(len(maps[0])): | |
| x=i | |
| for op in word: | |
| if type(op) is not int or not 0 <= op < len(maps): | |
| raise ValueError('invalid symbol') | |
| x=maps[op][x] | |
| result.append(x) | |
| return result | |
| def verify(payload: dict) -> dict: | |
| try: | |
| maps=payload['generators']; n=len(maps[0]); k=len(maps) | |
| if any(len(g)!=n or any(type(v) is not int or not 0<=v<n for v in g) for g in maps): | |
| raise ValueError('invalid domain') | |
| laws=payload['rules']; lefts=set(); checks=0 | |
| for law in laws: | |
| l=tuple(law['lhs']);r=tuple(law['rhs']) | |
| if not l or (len(r),r)>=(len(l),l):raise ValueError('not shortlex decreasing') | |
| if evaluate(l,maps)!=evaluate(r,maps):raise ValueError('false equivalence') | |
| lefts.add(l);checks+=n*(len(l)+len(r)) | |
| dfa=payload['guard']; prefixes=[tuple(x) for x in dfa['prefixes']] | |
| expected={()} | |
| for word in lefts: | |
| expected.update(word[:i] for i in range(1,len(word)+1)) | |
| if len(prefixes)!=len(expected) or set(prefixes)!=expected or prefixes[0]!=(): | |
| raise ValueError('malformed states') | |
| index={w:i for i,w in enumerate(prefixes)} | |
| table=dfa['transition'];bad=dfa['reject'] | |
| if len(table)!=len(prefixes) or len(bad)!=len(prefixes): | |
| raise ValueError('dimension mismatch') | |
| edge_checks=0 | |
| for state,prefix in enumerate(prefixes): | |
| should_reject=any(prefix[-j:] in lefts for j in range(1,len(prefix)+1)) | |
| if type(bad[state]) is not bool or bad[state]!=should_reject: | |
| raise ValueError('wrong rejection flag') | |
| if len(table[state])!=k:raise ValueError('wrong alphabet width') | |
| for a in range(k): | |
| w=prefix+(a,) | |
| suffix=next((w[i:] for i in range(len(w)+1) if w[i:] in index),()) | |
| if type(table[state][a]) is not int or table[state][a]!=index[suffix]: | |
| raise ValueError('wrong transition') | |
| edge_checks+=1 | |
| digest=hashlib.sha256(json.dumps([table,bad],separators=(',',':')).encode()).hexdigest() | |
| if digest!=dfa['sha256']: raise ValueError('hash mismatch') | |
| return {'accepted':True,'laws':len(laws),'states':len(prefixes), | |
| 'exact_instruction_steps':checks,'transition_checks':edge_checks} | |
| except (KeyError,IndexError,TypeError,ValueError,StopIteration) as ex: | |
| return {'accepted':False,'reason':str(ex)} | |
| if __name__=='__main__': | |
| import argparse | |
| p=argparse.ArgumentParser();p.add_argument('certificate',type=Path);a=p.parse_args() | |
| result=verify(json.loads(a.certificate.read_text())) | |
| print(json.dumps(result,indent=2));raise SystemExit(0 if result['accepted'] else 1) | |