Finagle some types and imports

This commit is contained in:
Emi Simpson 2023-03-06 13:39:16 -05:00
parent 9d6da82dd4
commit 6f0d15ee3f
Signed by: Emi
GPG key ID: A12F2C2FFDC3D847
3 changed files with 25 additions and 23 deletions

View file

@ -12,12 +12,13 @@ from dataclasses import dataclass
from enum import auto, IntEnum from enum import auto, IntEnum
from re import compile, Pattern from re import compile, Pattern
from ir import IRNeg, IRProp, IRTerm, IRVar from build_oracle import oracle_table
from lex import Lexeme from ir import IRNeg, IRProp, IRTerm, IRVar, KnowledgeBase
from parse import Action from lex import Lexeme, tokenize
from parse import Action, parser
from tokens import * from tokens import *
from typing import Any, Callable, Collection, Mapping, Sequence, Tuple, TypeAlias from typing import Any, Callable, cast, Collection, Mapping, Sequence, Tuple, TypeAlias
class Variable(IntEnum): class Variable(IntEnum):
@ -137,15 +138,19 @@ class ASTProp:
if bound_type == IdentKind.Variable: if bound_type == IdentKind.Variable:
return Ok(IRVar(self.ident.matched_string)) return Ok(IRVar(self.ident.matched_string))
else: else:
arg_ir = sequence([t.make_ir(idents, False) for t in self.arguments]) return (sequence([t.make_ir(idents, False) for t in self.arguments])
return map_res(p(IRProp, self.ident.matched_string), arg_ir) <= cast(Callable[[Iterable[IRTerm]], Tuple[IRTerm, ...]], tuple))\
<= p(IRProp, self.ident.matched_string)
@cur2 @cur2
def make_ir( def make_ir(
idents: IdentBindings, idents: IdentBindings,
clauses: Sequence[Sequence[ASTTerm]], clauses: Sequence[Sequence[ASTTerm]],
) -> Result[Sequence[Sequence[IRTerm]], GenIrError]: ) -> Result[KnowledgeBase, GenIrError]:
return sequence([sequence([term.make_ir(idents, True) for term in clause]) for clause in clauses]) return map_res(
lambda kb_: FSet(FSet(clause) for clause in kb_),
sequence([sequence([term.make_ir(idents, True) for term in clause]) for clause in clauses])
)
def cons(stack: Sequence[Any]) -> Sequence[Any]: def cons(stack: Sequence[Any]) -> Sequence[Any]:
match stack: match stack:
@ -257,7 +262,7 @@ CSTerms := Comma <Term> <CSTerms>
:= ε := ε
""" """
def lex_and_parse(input: str) -> Result[Result[Sequence[Sequence[IRTerm]], GenIrError], Tuple[Lexeme[Tok], Collection[Tok]]]: def lex_and_parse(input: str) -> Result[Result[KnowledgeBase, GenIrError], Tuple[Lexeme[Tok], Collection[Tok]]]:
lexemes = unwrap_r(tokenize(LEX_TABLE, [Tok.Whitespace], Tok.Eof, input)) lexemes = unwrap_r(tokenize(LEX_TABLE, [Tok.Whitespace], Tok.Eof, input))
oracle_table_ = oracle_table(p_instance(Tok), p_instance(Variable), GRAMMAR) #type:ignore oracle_table_ = oracle_table(p_instance(Tok), p_instance(Variable), GRAMMAR) #type:ignore
@ -277,9 +282,6 @@ if __name__ == '__main__':
# from emis_funky_funktions import cur2, flip # from emis_funky_funktions import cur2, flip
# from build_oracle import print_oracle_table_enum, oracle_table # from build_oracle import print_oracle_table_enum, oracle_table
# print(print_oracle_table_enum(oracle_table(flip(cur2(isinstance))(Tok), GRAMMAR))) #type: ignore # print(print_oracle_table_enum(oracle_table(flip(cur2(isinstance))(Tok), GRAMMAR))) #type: ignore
from build_oracle import oracle_table
from parse import parser
from lex import tokenize
import sys import sys
if len(sys.argv) == 2: if len(sys.argv) == 2:

10
ir.py
View file

@ -178,6 +178,16 @@ Due to this generalization, `Clause_` does not necessarily benefit from hashabil
deduplication. It exists mostly to make inputing things easier, e.g. in doctests. deduplication. It exists mostly to make inputing things easier, e.g. in doctests.
""" """
KnowledgeBase: TypeAlias = FrozenSet[Clause]
KnowledgeBase_: TypeAlias = Collection[Clause_]
"""
A more general version of `KnowledgeBase`
`KnowledgeBase_` : `KnowledgeBase` :: `Clause_` : `Clause`
A superclass of `KnowledgeBase`
"""
sub_all: Callable[[Substitutions, IRTerm], IRTerm] = p(reduce, lambda t, s: t.subst(s)) #type:ignore sub_all: Callable[[Substitutions, IRTerm], IRTerm] = p(reduce, lambda t, s: t.subst(s)) #type:ignore
""" """
Perform a series of substitutions on a term Perform a series of substitutions on a term

View file

@ -4,17 +4,7 @@ from itertools import combinations, product
from operator import eq from operator import eq
from typing import Collection, FrozenSet, TypeAlias from typing import Collection, FrozenSet, TypeAlias
from ir import Clause, Clause_, IRNeg, IRProp, IRTerm, IRVar, Substitutions, sub_all, unify from ir import Clause, Clause_, IRNeg, IRProp, IRTerm, IRVar, KnowledgeBase, KnowledgeBase_, Substitutions, sub_all, unify
KnowledgeBase: TypeAlias = FrozenSet[Clause]
KnowledgeBase_: TypeAlias = Collection[Clause_]
"""
A more general version of `KnowledgeBase`
`KnowledgeBase_` : `KnowledgeBase` :: `Clause_` : `Clause`
A superclass of `KnowledgeBase`
"""
def terms_cancel(t1: IRTerm, t2: IRTerm) -> Option[Substitutions]: def terms_cancel(t1: IRTerm, t2: IRTerm) -> Option[Substitutions]:
""" """