示例#1
0
def expandToUnk(term: PPTerm, ntId: int) -> Optional[PPTerm]:
    """
    Generate a new term by replacing a "ntId"th NT from a "term" with a PPTermUnk
    """
    nt = ASTUtils.getNthNT(term, ntId)

    # Avoid generating Unk of type PPDimConst, PPDimVar, PPEumSort, or PPInt()
    if isinstance(nt.sort, PPDimConst) or isinstance(nt.sort, PPDimVar) or isinstance(nt.sort, PPEnumSort) or \
            isinstance(nt.sort, PPInt):
        return None

    unk = ASTDSL.mkUnk(nt.sort)

    # subst = unifyOne(unk.sort, nt.sort)
    #
    # if subst != []:
    #     print("non empty subst")
    #
    # termExpanded = None
    # if subst is not None:
    #     termUnified = applySubst(subst, term)
    #     termExpanded = ReprUtils.replaceNthNT(termUnified, ntId, unk)

    termNew = ReprUtils.replaceNthNT(term, ntId, unk)
    return termNew
示例#2
0
def expandToFuncApp(lib: FnLibrary, term: PPTerm, ntId: int,
                    fname: str) -> Optional[PPTerm]:
    resTerm = None
    # Not needed now as there are no lambda terms
    # libItem = ScopeUtils.getAVarInScope(lib, term, ntId, fname)
    libItem = lib.getWithLibPrefix(fname)
    assert libItem
    nt = ASTUtils.getNthNT(term, ntId)

    libItem = alphaConvertLibItem(libItem, term)
    # TODO: expandToVar passed nt.sort as second argument
    subst = unifyOne(nt.sort, libItem.sort.rtpe)

    if not gUseTypes:
        if subst is None:
            subst = []

    if subst is not None:
        nts = [PPTermNT('Z', arg_sort) for arg_sort in libItem.sort.args]
        fnApp = PPFuncApp(PPVar(libItem.name), nts)

        termUnified = applySubst(subst, term)
        fnAppUnified = applySubst(subst, fnApp)

        resTerm = ReprUtils.replaceNthNT(termUnified, ntId, fnAppUnified)

    return resTerm
def test_1():
    prog1 = PPFuncApp(
        fn=PPVar(name='lib.compose'),
        args=[
            PPTermUnk(name='nn_fun_csc4_2',
                      sort=PPFuncSort(
                          args=[PPSortVar(name='B')],
                          rtpe=PPTensorSort(
                              param_sort=PPReal(),
                              shape=[PPDimConst(value=1),
                                     PPDimConst(value=1)]))),
            PPTermUnk(name='nn_fun_csc4_3',
                      sort=PPFuncSort(args=[
                          PPListSort(
                              param_sort=PPTensorSort(param_sort=PPReal(),
                                                      shape=[
                                                          PPDimConst(value=1),
                                                          PPDimConst(value=1),
                                                          PPDimConst(value=28),
                                                          PPDimConst(value=28)
                                                      ]))
                      ],
                                      rtpe=PPSortVar(name='B')))
        ])

    prog2 = PPFuncApp(
        fn=PPVar(name='lib.compose'),
        args=[
            PPTermUnk(name='nn_fun_csc4_4',
                      sort=PPFuncSort(
                          args=[PPSortVar(name='C')],
                          rtpe=PPTensorSort(
                              param_sort=PPReal(),
                              shape=[PPDimConst(value=1),
                                     PPDimConst(value=1)]))),
            PPTermUnk(name='nn_fun_csc4_5',
                      sort=PPFuncSort(args=[
                          PPListSort(
                              param_sort=PPTensorSort(param_sort=PPReal(),
                                                      shape=[
                                                          PPDimConst(value=1),
                                                          PPDimConst(value=1),
                                                          PPDimConst(value=28),
                                                          PPDimConst(value=28)
                                                      ]))
                      ],
                                      rtpe=PPSortVar(name='C')))
        ])

    prog3 = PPFuncApp(fn=PPVar(name='lib.compose'), args=[prog1, prog2])

    cmts = [PPInt(), PPReal()]
    eprogs = instantiateSortVar(prog3, cmts, 2)
    assert len(eprogs) == 4
    for i, eprog in enumerate(eprogs):
        print(i, ReprUtils.repr_py(eprog))
        print(i, eprog)
        assert not isAbstract(eprog)
示例#4
0
def expandDimConst(term: PPTerm, ntId: int) -> Optional[PPTerm]:
    """
    Expand dimension constant to integer constants (Required for fold zeros)
    """
    nt = ASTUtils.getNthNT(term, ntId)
    if type(nt.sort) != PPDimConst:
        return None

    subTerm = PPIntConst(nt.sort.value)
    termExpanded = ReprUtils.replaceNthNT(term, ntId, subTerm)
    return termExpanded
示例#5
0
    def evaluate(self, program, output_type_s, unkSortMap = None,
                 io_examples_tr=None, io_examples_val=None, io_examples_test=None, dbg_learn_parameters=True) -> dict:

        is_graph = type(output_type_s) == PPGraphSort

        program_str = ReprUtils.repr_py(program)
        output_type = self.get_program_output_type(io_examples_val, output_type_s)

        unknown_fns_def = _get_unknown_fns_definitions(unkSortMap, is_graph)
        res = self.evaluate_(program = program_str, output_type=output_type, unknown_fns_def=unknown_fns_def,
                             io_examples_tr=io_examples_tr, io_examples_val=io_examples_val,
                             io_examples_test=io_examples_test,
                             dbg_learn_parameters=dbg_learn_parameters)
        return res
示例#6
0
def expandToVar(lib: FnLibrary, term: PPTerm, ntId: int,
                vname: str) -> Optional[PPTerm]:
    """
    Generate a new term by replacing a "ntId"th NT from a "term" with a variable (in scope) with name "fname"
    """
    nt = ASTUtils.getNthNT(term, ntId)
    # libItem = ScopeUtils.getAVarInScope(lib, term, ntId, vname)
    libItem = lib.getWithLibPrefix(vname)
    assert libItem

    libItem = alphaConvertLibItem(libItem, term)
    subst = unifyOne(libItem.sort, nt.sort)

    if not gUseTypes:
        if subst is None:
            subst = []

    termExpanded = None
    if subst is not None:
        termUnified = applySubst(subst, term)
        termExpanded = ReprUtils.replaceNthNT(termUnified, ntId,
                                              PPVar(libItem.name))

    return termExpanded
示例#7
0
def printCodeGen(t):
    print('assert(t == %s)' % str(t))
    print('printTerm(t)')
    print('#%s' % ReprUtils.repr_py_ann(t))
    print('')
示例#8
0
def expandEnum(term: PPTerm, ntId: int, subTerm: PPTerm) -> Optional[PPTerm]:
    nt = ASTUtils.getNthNT(term, ntId)
    termExpanded = ReprUtils.replaceNthNT(term, ntId, subTerm)
    return termExpanded
示例#9
0
def main():
    io_train, io_val = get_io_examples_count_digit_occ(5, 1200, 1200)
    # Task Name: count_digit_occ_5s
    prog = PPFuncApp(
        fn=PPVar(name='lib.compose'),
        args=[
            PPTermUnk(
                name='nn_fun_3254',
                sort=PPFuncSort(args=[
                    PPListSort(param_sort=PPListSort(param_sort=PPTensorSort(
                        param_sort=PPBool(),
                        shape=[PPDimConst(
                            value=1), PPDimConst(value=1)])))
                ],
                                rtpe=PPTensorSort(param_sort=PPReal(),
                                                  shape=[
                                                      PPDimConst(value=1),
                                                      PPDimConst(value=1)
                                                  ]))),
            PPFuncApp(fn=PPVar(name='lib.conv_l'),
                      args=[
                          PPFuncApp(fn=PPVar(name='lib.map_l'),
                                    args=[PPVar(name='lib.nn_fun_1')])
                      ])
        ])
    unkSortMap = {
        'nn_fun_3254':
        PPFuncSort(args=[
            PPListSort(param_sort=PPListSort(param_sort=PPTensorSort(
                param_sort=PPBool(),
                shape=[PPDimConst(
                    value=1), PPDimConst(value=1)])))
        ],
                   rtpe=PPTensorSort(
                       param_sort=PPReal(),
                       shape=[PPDimConst(value=1),
                              PPDimConst(value=1)]))
    }
    lib = FnLibrary()
    lib.addItems([
        PPLibItem(name='compose',
                  sort=PPFuncSort(args=[
                      PPFuncSort(args=[PPSortVar(name='B')],
                                 rtpe=PPSortVar(name='C')),
                      PPFuncSort(args=[PPSortVar(name='A')],
                                 rtpe=PPSortVar(name='B'))
                  ],
                                  rtpe=PPFuncSort(args=[PPSortVar(name='A')],
                                                  rtpe=PPSortVar(name='C'))),
                  obj=None),
        PPLibItem(name='repeat',
                  sort=PPFuncSort(args=[
                      PPEnumSort(start=2, end=50),
                      PPFuncSort(args=[PPSortVar(name='A')],
                                 rtpe=PPSortVar(name='A'))
                  ],
                                  rtpe=PPFuncSort(args=[PPSortVar(name='A')],
                                                  rtpe=PPSortVar(name='A'))),
                  obj=None),
        PPLibItem(name='map_l',
                  sort=PPFuncSort(
                      args=[
                          PPFuncSort(args=[PPSortVar(name='A')],
                                     rtpe=PPSortVar(name='B'))
                      ],
                      rtpe=PPFuncSort(
                          args=[PPListSort(param_sort=PPSortVar(name='A'))],
                          rtpe=PPListSort(param_sort=PPSortVar(name='B')))),
                  obj=None),
        PPLibItem(
            name='fold_l',
            sort=PPFuncSort(args=[
                PPFuncSort(args=[PPSortVar(name='B'),
                                 PPSortVar(name='A')],
                           rtpe=PPSortVar(name='B')),
                PPSortVar(name='B')
            ],
                            rtpe=PPFuncSort(args=[
                                PPListSort(param_sort=PPSortVar(name='A'))
                            ],
                                            rtpe=PPSortVar(name='B'))),
            obj=None),
        PPLibItem(name='conv_l',
                  sort=PPFuncSort(
                      args=[
                          PPFuncSort(args=[
                              PPListSort(param_sort=PPSortVar(name='A'))
                          ],
                                     rtpe=PPSortVar(name='B'))
                      ],
                      rtpe=PPFuncSort(
                          args=[PPListSort(param_sort=PPSortVar(name='A'))],
                          rtpe=PPListSort(param_sort=PPSortVar(name='B')))),
                  obj=None),
        PPLibItem(name='zeros',
                  sort=PPFuncSort(args=[PPDimVar(name='a')],
                                  rtpe=PPTensorSort(param_sort=PPReal(),
                                                    shape=[
                                                        PPDimConst(value=1),
                                                        PPDimVar(name='a')
                                                    ])),
                  obj=None),
        PPLibItem(name='nn_fun_1',
                  sort=PPFuncSort(args=[
                      PPTensorSort(param_sort=PPReal(),
                                   shape=[
                                       PPDimConst(value=1),
                                       PPDimConst(value=1),
                                       PPDimConst(value=28),
                                       PPDimConst(value=28)
                                   ])
                  ],
                                  rtpe=PPTensorSort(param_sort=PPBool(),
                                                    shape=[
                                                        PPDimConst(value=1),
                                                        PPDimConst(value=1)
                                                    ])),
                  obj=None),
        PPLibItem(name='nn_fun_2230',
                  sort=PPFuncSort(args=[
                      PPTensorSort(param_sort=PPReal(),
                                   shape=[
                                       PPDimConst(value=1),
                                       PPDimConst(value=1),
                                       PPDimConst(value=28),
                                       PPDimConst(value=28)
                                   ])
                  ],
                                  rtpe=PPTensorSort(param_sort=PPBool(),
                                                    shape=[
                                                        PPDimConst(value=1),
                                                        PPDimConst(value=1)
                                                    ])),
                  obj=None)
    ])
    fn_sort = PPFuncSort(args=[
        PPListSort(param_sort=PPTensorSort(param_sort=PPReal(),
                                           shape=[
                                               PPDimConst(value=1),
                                               PPDimConst(value=1),
                                               PPDimConst(value=28),
                                               PPDimConst(value=28)
                                           ]))
    ],
                         rtpe=PPTensorSort(
                             param_sort=PPReal(),
                             shape=[PPDimConst(value=1),
                                    PPDimConst(value=1)]))

    print(ReprUtils.repr_py_ann(prog))
    print(ReprUtils.repr_py_sort(lib.items['nn_fun_1'].sort))
    NeuralSynthesizer.is_evaluable(prog)
def main():
    # term = PPVar(name='x')
    # print(term)
    # print(doubleVarNames(term))
    # exit()
    # for term in TermsRepo.termsrepo:
    #     print('############')
    #     print(term)
    #     print(doubleVarNames(term))

    prog1 = PPFuncApp(
        fn=PPVar(name='lib.compose'),
        args=[
            PPTermUnk(name='nn_fun_csc4_2',
                      sort=PPFuncSort(
                          args=[
                              PPSortVar(name='B')],
                          rtpe=PPTensorSort(param_sort=PPReal(), shape=[PPDimConst(value=1), PPDimConst(value=1)]))),
            PPTermUnk(name='nn_fun_csc4_3',
                      sort=PPFuncSort(
                          args=[PPListSort(param_sort=PPTensorSort(param_sort=PPReal(),
                                                                   shape=[PPDimConst(value=1), PPDimConst(value=1),
                                                                          PPDimConst(value=28),
                                                                          PPDimConst(value=28)]))],
                          rtpe=PPSortVar(name='B')))])

    prog2 = PPFuncApp(
        fn=PPVar(name='lib.compose'),
        args=[
            PPTermUnk(name='nn_fun_csc4_4',
                      sort=PPFuncSort(
                          args=[
                              PPSortVar(name='C')],
                          rtpe=PPTensorSort(param_sort=PPReal(), shape=[PPDimConst(value=1), PPDimConst(value=1)]))),
            PPTermUnk(name='nn_fun_csc4_5',
                      sort=PPFuncSort(
                          args=[PPListSort(param_sort=PPTensorSort(param_sort=PPReal(),
                                                                   shape=[PPDimConst(value=1), PPDimConst(value=1),
                                                                          PPDimConst(value=28),
                                                                          PPDimConst(value=28)]))],
                          rtpe=PPSortVar(name='C')))])

    prog3 = PPFuncApp(
        fn=PPVar(name='lib.compose'),
        args=[prog1, prog2])

    prog4 = PPFuncApp(fn=PPVar(name='lib.compose'), args=[PPTermUnk(name='nn_fun_csc4_8', sort=PPFuncSort(
        args=[PPListSort(param_sort=PPSortVar(name='B_1'))],
        rtpe=PPTensorSort(param_sort=PPReal(), shape=[PPDimConst(value=1), PPDimConst(value=1)]))),
                                                          PPFuncApp(fn=PPVar(name='lib.map_l'), args=[
                                                              PPTermUnk(name='nn_fun_csc4_9', sort=PPFuncSort(args=[
                                                                  PPTensorSort(param_sort=PPReal(),
                                                                               shape=[PPDimConst(value=1),
                                                                                      PPDimConst(value=1),
                                                                                      PPDimConst(value=28),
                                                                                      PPDimConst(value=28)])],
                                                                                                              rtpe=PPSortVar(
                                                                                                                  name='B_1')))])])
    cmts = [PPInt(), PPReal()]
    eprogs = instantiateSortVar(prog4, cmts)
    for i, eprog in enumerate(eprogs):
        print(i, ReprUtils.repr_py(eprog))
        print(i, eprog)