Skip to content

Initial implementation of Symbolic type #1591

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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions examples/expr2.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
def main0():
x: i32
x = (2+3)*5
def main():
x: symbolic = Symbolic('x')
Copy link
Contributor

@certik certik Mar 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from lpython import Symbolic
from sympy import Symbol
...
Suggested change
x: symbolic = Symbolic('x')
x: Symbolic = Symbol('x')

print(x)

main0()
main()
Copy link
Collaborator Author

@Thirumalai-Shaktivel Thirumalai-Shaktivel Mar 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, this doesn't work in CPython. I think we have to handle this in ltypes and make sure this works with CPython


# Not implemented yet in LPython:
#if __name__ == "__main__":
# main()
3 changes: 3 additions & 0 deletions src/libasr/ASR.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ expr

| IntrinsicFunctionSqrt(expr arg, ttype type, expr? value)

| SymbolicCall(expr arg, ttype type, expr? value)


-- `len` in Character:
-- >=0 ... the length of the string, known at compile time
Expand Down Expand Up @@ -359,6 +361,7 @@ ttype
| Const(ttype type)
| CPtr()
| TypeParameter(identifier param, dimension* dims)
| Symbolic()
| FunctionType(ttype* arg_types, ttype? return_var_type,
abi abi, deftype deftype, string? bindc_name, bool elemental,
bool pure, bool module, bool inline, bool static, ttype* type_params,
Expand Down
11 changes: 11 additions & 0 deletions src/libasr/asr_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,9 @@ static inline std::string type_to_str_python(const ASR::ttype_t *t,
ASR::TypeParameter_t *p = ASR::down_cast<ASR::TypeParameter_t>(t);
return p->m_param;
}
case ASR::ttypeType::Symbolic: {
return "Symbolic";
}
default : throw LCompilersException("Not implemented " + std::to_string(t->type));
}
}
Expand Down Expand Up @@ -1503,6 +1506,11 @@ inline int extract_dimensions_from_ttype(ASR::ttype_t *x,
m_dims = nullptr;
break;
}
case ASR::ttypeType::Symbolic: {
n_dims = 0;
m_dims = nullptr;
break;
}
case ASR::ttypeType::TypeParameter: {
ASR::TypeParameter_t* tp = ASR::down_cast<ASR::TypeParameter_t>(x);
n_dims = tp->n_dims;
Expand Down Expand Up @@ -2041,6 +2049,9 @@ inline bool types_equal(ASR::ttype_t *a, ASR::ttype_t *b,
case ASR::ttypeType::CPtr: {
return true;
}
case ASR::ttypeType::Symbolic: {
return true;
}
case (ASR::ttypeType::Real) : {
ASR::Real_t *a2 = ASR::down_cast<ASR::Real_t>(a);
ASR::Real_t *b2 = ASR::down_cast<ASR::Real_t>(b);
Expand Down
8 changes: 8 additions & 0 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,8 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
4, dims.p, dims.size()));
} else if (var_annotation == "CPtr") {
type = ASRUtils::TYPE(ASR::make_CPtr_t(al, loc));
} else if (var_annotation == "symbolic") {
type = ASRUtils::TYPE(ASR::make_Symbolic_t(al, loc));
} else if (var_annotation == "pointer") {
LCOMPILERS_ASSERT(n_args == 1);
AST::expr_t* underlying_type = m_args[0];
Expand Down Expand Up @@ -6175,6 +6177,12 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
tmp = intrinsic_node_handler.get_intrinsic_node(call_name, al,
x.base.base.loc, args);
return;
} else if (call_name == "Symbolic") {
LCOMPILERS_ASSERT(args.size() == 1)
tmp = ASR::make_SymbolicCall_t(al, x.base.base.loc,
args[0].m_value,ASRUtils::TYPE(ASR::make_Symbolic_t(
al, x.base.base.loc)), nullptr);
return ;
} else {
// The function was not found and it is not intrinsic
throw SemanticError("Function '" + call_name + "' is not declared and not intrinsic",
Expand Down