def intra_crd_match(dag: DAG, hierarchy: List[Tuple[Node, List[Optional[Node]]]], copy_type_: WordType, copy_dep_: str) -> ProofNet: hierarchy = list( map(lambda pair: (isolate_xs(get_crd_type(dag, fst(pair))), snd(pair)), hierarchy)) hierarchy = list(map(lambda h: list(zip(*h)), hierarchy)) hierarchy = list( filter(lambda pair: snd(pair) is not None, list(chain.from_iterable(hierarchy)))) hierarchy = list( map( lambda pair: (fst(pair), last_instance_of(get_crd_type(dag, snd(pair)))), hierarchy)) try: matches = list( map( lambda pair: (identify_missing(fst(pair), copy_type_, copy_dep_), identify_missing(snd(pair), copy_type_, copy_dep_)), hierarchy)) except AttributeError: ToGraphViz()(dag) print(hierarchy) import pdb pdb.set_trace() return reduce(lambda proof_, pair: match(proof_, fst(pair), snd(pair)), matches, set())
def get_copy_annotation(dag: DAG, edge: Edge) -> WordType: source = edge.source while True: conjunction = find_first_conjunction_above(dag, source) if conjunction is None: ToGraphViz()(dag) import pdb pdb.set_trace() missing_type = get_simple_argument(dag, edge.target) crd_type = get_crd_type(dag, conjunction) conjunction_daughters = get_conjunction_daughters(dag, conjunction) conjunction_daughters = list( map( lambda daughter: dag.exists_path(daughter, edge.source) or daughter == edge.source, conjunction_daughters)) if any(conjunction_daughters): break source = conjunction daughter_index = conjunction_daughters.index(True) xs, result = isolate_xs(crd_type), last_instance_of(crd_type) return identify_missing(xs[daughter_index], missing_type, edge.dep)
def annotate_dag(dag: DAG) -> Tuple[ProofNet, DAG]: proof = set() new_dag, idx = annotate_leaves(dag) new_dag = add_ghost_nodes(new_dag) try: if dag.edges: temp = iterate_simple_fringe(new_dag) if temp is not None: proof, new_dag = temp new_dag = delete_ghost_nodes(new_dag) copy_proof = match_copies_with_crds(new_dag) copy_gap_proof = match_copy_gaps_with_crds(new_dag) proof = merge_proofs(proof, [copy_proof, copy_gap_proof]) except ProofError as e: raise e root_type = new_dag.attribs[fst(list(new_dag.get_roots()))]['type'] if not isinstance(root_type, AtomicType): raise ProofError('Derived a complex type.') idx, conclusion = polarize_and_index(root_type.depolarize(), False, idx) proof = match(proof, root_type, conclusion) positives = set(map(fst, proof)) negatives = set(map(snd, proof)) immaterial = set( map( lambda type_: type_.index, filter( lambda type_: type_ == placeholder, map(lambda leaf: new_dag.attribs[leaf]['type'], filter(new_dag.is_leaf, dag.nodes))))) if set.union(positives, negatives, immaterial) != set(range(idx)): ToGraphViz()(new_dag) print( set(range(idx)).difference( set(map(fst, proof)).union(set(map(snd, proof))))) raise ProofError('Unmatched types.') return proof, new_dag
def refine_body(dag: DAG) -> DAG: bodies = list(dag.get_edges('body')) to_add, to_remove = set(), set() for body in bodies: common_source = dag.outgoing(body.source) match = list( filter(lambda edge: edge.dep in ('cmp', 'rhd', 'whd'), common_source)) if len(match) != 1: from LassyExtraction.viz import ToGraphViz import pdb ToGraphViz()(dag) pdb.set_trace() match = fst(match) new_dep = match.dep + '_body' to_add.add(Edge(source=body.source, target=body.target, dep=new_dep)) to_remove.add(body) return DAG(nodes=dag.nodes, edges=dag.edges.difference(to_remove).union(to_add), attribs=dag.attribs, meta=dag.meta)
from LassyExtraction.extraction import Extraction, _cat_dict, _pt_dict, _head_deps, _mod_deps, ExtractionError from LassyExtraction.lassy import Lassy from LassyExtraction.proofs import Prove, ProofError from LassyExtraction.transformations import Transformation from LassyExtraction.viz import ToGraphViz from typing import Optional, Union, Tuple, List from LassyExtraction.graphutils import DAG from LassyExtraction.proofs import ProofNet _lassy = Lassy() _transform = Transformation() _extract = Extraction(_cat_dict, _pt_dict, 'pt', _head_deps, _mod_deps) _prove = Prove() _viz = ToGraphViz() def test(size, start=0): meta = [{'src': _lassy[i][1]} for i in range(start, start + size)] transformed = list( chain.from_iterable( list( map( _transform, list( map(lambda i: _lassy[i][2], range(start, start + size))), meta)))) transformed = list(filter(lambda t: t is not None, transformed))
def annotate_simple_branch(dag: DAG, parent: Node) -> Tuple[ProofNet, WordType]: def simplify_crd(crd_type: WordType, arg_types_: List[WordType]) -> WordType: xs, result = isolate_xs(crd_type), last_instance_of(crd_type) if len(set(map(depolarize, arg_types))) > 1: raise ProofError('Non polymorphic conjunction.') if arg_types_ == xs: return crd_type else: xs, result = list(map(lambda x: x.result, xs)), result.result crd_type = reduce( lambda res_, arg_: ColoredType(arg_, res_, 'cnj'), reversed(xs), result) return simplify_crd(crd_type, arg_types_) # xs, result = list(map(get_functor_result, xs)), get_functor_result(result) # return reduce(lambda res_, arg_: ColoredType(arg_, res_, 'cnj'), reversed(xs), result) def is_gap_copy_parent(edge_: Edge) -> bool: return edge_.dep in _head_deps and is_copy( dag, edge_.target) and is_gap(dag, edge_.target, _head_deps) branch_proof = set() outgoing = dag.outgoing(parent) outgoing = set( filter( lambda edge: not is_copy(dag, edge.target) or is_gap_copy_parent( edge), outgoing)) head = list( filter( lambda out: out.dep in _head_deps and dag.attribs[out.target][ 'type'] != placeholder, outgoing)) if len(head) != 1: ToGraphViz()(dag) import pdb pdb.set_trace() head = fst(head) outgoing = list(filter(lambda out: out.dep not in _head_deps, outgoing)) mods = list(filter(lambda out: out.dep in _mod_deps, outgoing)) mods = order_nodes(dag, set(map(lambda out: out.target, mods))) args = list(filter(lambda out: out.dep not in _mod_deps, outgoing)) sorted_args = order_nodes(dag, set(map(lambda out: out.target, args))) args = sorted(args, key=lambda x: sorted_args.index(x.target)) arg_types = list( map(lambda out: get_simple_argument(dag, out.target), args)) arg_deps = list(map(lambda out: out.dep, args)) if dag.attribs[parent]['cat'] == 'conj': branch_output = simplify_crd(dag.attribs[head.target]['type'], arg_types) else: branch_output = get_simple_functor(dag, head.target) arg_proof, branch_output = align_args(branch_output, arg_types, arg_deps) mod_proof, branch_output = align_mods( branch_output, list(map(lambda node: get_simple_argument(dag, node), mods))) branch_proof = merge_proofs(branch_proof, (arg_proof, mod_proof)) return branch_proof, branch_output
def impose_order( conjunctions_: Nodes) -> List[Tuple[Node, List[Optional[Node]]]]: daughters = list(conjunctions_) daughters = list( zip( daughters, list( map(lambda conj: get_conjunction_daughters(dag, conj), daughters)))) daughters_connections = list( map( lambda pair: # the conjunction ( # the conjunction fst(pair), # the subset of conjunctions contained by each daughter list( map( lambda daughter: set( filter( lambda other: dag.exists_path( daughter, other) or daughter == other, conjunctions_)), snd(pair)))), daughters)) daughters_connections = list( map( lambda pair: ( fst(pair), # the subset of connections that have the most paths to node list( map( lambda daughter_connections: set( filter( lambda connection: not any( map( lambda other: len( dag.distinct_paths_to( other, node)) > len( dag.distinct_paths_to( connection, node)), daughter_connections)), daughter_connections)), snd(pair)))), daughters_connections)) daughters_connections = list( map( lambda pair: ( fst(pair), # the singular connection that lies lowest (not over any conjunction?) list( map( lambda daughter_connections: set( filter( lambda connection: not any( map( lambda other: other in dag. points_to(connection ), daughter_connections) ), daughter_connections)), snd(pair)))), daughters_connections)) if any( map( lambda conj: any( map( lambda conns: len(list(filter(lambda x: x, conns))) > 1, snd(conj))), daughters_connections)): ToGraphViz()(dag) raise ProofError('wtf') daughters_connections = list( map( lambda pair: (fst(pair), list( map( lambda daughter: fst(list(daughter)) if len(daughter) else None, snd(pair)))), daughters_connections)) return daughters_connections
def participating_conjunctions(dag: DAG, node: Node, exclude_heads: bool = False) \ -> List[Tuple[Node, List[Optional[Node]]]]: def impose_order( conjunctions_: Nodes) -> List[Tuple[Node, List[Optional[Node]]]]: daughters = list(conjunctions_) daughters = list( zip( daughters, list( map(lambda conj: get_conjunction_daughters(dag, conj), daughters)))) daughters_connections = list( map( lambda pair: # the conjunction ( # the conjunction fst(pair), # the subset of conjunctions contained by each daughter list( map( lambda daughter: set( filter( lambda other: dag.exists_path( daughter, other) or daughter == other, conjunctions_)), snd(pair)))), daughters)) daughters_connections = list( map( lambda pair: ( fst(pair), # the subset of connections that have the most paths to node list( map( lambda daughter_connections: set( filter( lambda connection: not any( map( lambda other: len( dag.distinct_paths_to( other, node)) > len( dag.distinct_paths_to( connection, node)), daughter_connections)), daughter_connections)), snd(pair)))), daughters_connections)) daughters_connections = list( map( lambda pair: ( fst(pair), # the singular connection that lies lowest (not over any conjunction?) list( map( lambda daughter_connections: set( filter( lambda connection: not any( map( lambda other: other in dag. points_to(connection ), daughter_connections) ), daughter_connections)), snd(pair)))), daughters_connections)) if any( map( lambda conj: any( map( lambda conns: len(list(filter(lambda x: x, conns))) > 1, snd(conj))), daughters_connections)): ToGraphViz()(dag) raise ProofError('wtf') daughters_connections = list( map( lambda pair: (fst(pair), list( map( lambda daughter: fst(list(daughter)) if len(daughter) else None, snd(pair)))), daughters_connections)) return daughters_connections incoming = list( filter(lambda edge: edge.dep not in _head_deps or not exclude_heads, dag.incoming(node))) parents = set(map(lambda edge: edge.source, incoming)) top = dag.first_common_predecessor(parents) if top is None or dag.attribs[top]['cat'] != 'conj': ToGraphViz()(dag) raise ProofError('Top is not a conj or no top.') conjunctions = _cats_of_type( dag, 'conj', dag.points_to(top).intersection(dag.pointed_by(node)).union({top})) conjunctions = set( filter(lambda conj: len(dag.distinct_paths_to(conj, node)) > 1, conjunctions)) conjunctions = sorted(impose_order(conjunctions), key=lambda pair: fst(pair) == top, reverse=True) return conjunctions