Skip to content

Supporting Callbacks in LPython #1608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Smit-create opened this issue Mar 23, 2023 · 2 comments · Fixed by #1726
Closed

Supporting Callbacks in LPython #1608

Smit-create opened this issue Mar 23, 2023 · 2 comments · Fixed by #1726
Assignees
Labels
asr ASR related changes enhancement New feature or request

Comments

@Smit-create
Copy link
Collaborator

For providing functionalities like: #992 (comment) we need to support callbacks. A simple example is:

from ltypes import i32, Callable

def f(x: i32) -> i32:
    return x + 1

def g(func: Callable[[i32], i32], arg: i32):
    ret: i32
    ret = func(arg)
    print(ret)

g(f, 10)

Currently, this throws the following error:

Error

Internal Compiler Error: Unhandled exception
Traceback (most recent call last):
  File "/Users/thebigbool/repos/lpython/src/bin/lpython.cpp", line 1602
    return emit_asr(arg_file, lpython_pass_manager, runtime_library_dir,
  File "/Users/thebigbool/repos/lpython/src/bin/lpython.cpp", line 194
    r = LCompilers::LPython::python_ast_to_asr(al, lm, *ast, diagnostics,
  File "/Users/thebigbool/repos/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 6237
    if (res.ok) {
  File "/Users/thebigbool/repos/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 3814
    v.visit_Module(ast);
  File "/Users/thebigbool/repos/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 3302
    visit_stmt(*x.m_body[i]);
  File "/Users/thebigbool/repos/lpython/src/lpython/python_ast.h", line 1882
    void visit_stmt(const stmt_t &b) { visit_stmt_t(b, self()); }
  File "/Users/thebigbool/repos/lpython/src/lpython/python_ast.h", line 1750
    case stmtType::FunctionDef: { v.visit_FunctionDef((const FunctionDef_t &)x); return; }
  File "/Users/thebigbool/repos/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 3389
    ASR::ttype_t *arg_type = ast_expr_to_asr_type(x.base.base.loc, *x.m_args.m_args[i].m_annotation);
  File "/Users/thebigbool/repos/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 1540
    this->visit_expr(*s->m_slice);
  File "/Users/thebigbool/repos/lpython/src/lpython/python_ast.h", line 1909
    void visit_expr(const expr_t &b) { visit_expr_t(b, self()); }
  File "/Users/thebigbool/repos/lpython/src/lpython/python_ast.h", line 1815
    case exprType::Tuple: { v.visit_Tuple((const Tuple_t &)x); return; }
  File "/Users/thebigbool/repos/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 2674
    this->visit_expr(*x.m_elts[i]);
  File "/Users/thebigbool/repos/lpython/src/lpython/python_ast.h", line 1909
    void visit_expr(const expr_t &b) { visit_expr_t(b, self()); }
  File "/Users/thebigbool/repos/lpython/src/lpython/python_ast.h", line 1814
    case exprType::List: { v.visit_List((const List_t &)x); return; }
  File "/Users/thebigbool/repos/lpython/src/lpython/python_ast.h", line 1941
    void visit_List(const List_t & /* x */) { throw LCompilersException("visit_List() not implemented"); }
LCompilersException: visit_List() not implemented

@Smit-create
Copy link
Collaborator Author

So we need to add a type at the Python interface similar to typing.Callable for supporting Functions as the argument. Nextly, we move to the ASR level with FunctionType.

@Smit-create Smit-create self-assigned this Mar 23, 2023
@Smit-create Smit-create added enhancement New feature or request asr ASR related changes labels Mar 23, 2023
@certik
Copy link
Contributor

certik commented Mar 23, 2023

LFortran supports callbacks all the way. So we just need to improve the LPython frontend to generate the correct ASR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
asr ASR related changes enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants