Quansight-Labs / python-moa

Python implementation of Mathematics of Arrays (MOA)
https://python-moa.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
23 stars 5 forks source link

Generalized replacement rules for python-moa #18

Open costrouc opened 5 years ago

costrouc commented 5 years ago

Would like to have a less programatic way of performing moa dnf reductions and onf.

Current implementation is as follows.

reduction_rules = {
       ((ast.NodeSymbol.PSI,), (None, ((ast.NodeSymbol.ASSIGN,),),)): _reduce_psi_assign,
       ....
}

def _reduce_psi_assign(context):
    """<i j> psi ... assign ... => <i j> psi ... assign <i j> psi ..."""
    return ast.create_context(
        ast=ast.Node((ast.NodeSymbol.ASSIGN,), context.ast.shape, (), (
            ast.Node((ast.NodeSymbol.PSI,), context.ast.shape, (), (ast.select_node(context, (0,)).ast, ast.select_node(context, (1, 0)).ast)),
            ast.Node((ast.NodeSymbol.PSI,), context.ast.shape, (), (ast.select_node(context, (0,)).ast, ast.select_node(context, (1, 1)).ast)))),
        symbol_table=context.symbol_table)

rule: list of operations transforming ast

How far can this take me?

cc @saulshanabrook would like to talk about approach

costrouc commented 5 years ago

Look at antlr approach for transformations

saulshanabrook commented 5 years ago

One idea is to have a function that in the wildcards, and return the match and the replacement.

If they were both strings, it could be like this:

@register_rule
def reduce_psi_assign(x, y):
     match = f"<i j> psi {x} assign {y}"
     replacement = f"<i j> psi {x} assign <i j> psi {y}"
     return match, replacement