luthfan's Blog

Instantiating generic functions into valid functions in ASR (3rd week)

luthfan
Published: 07/01/2022

I made some good progress this week in handling generic functions on LPython's ASR level.

What I did this week

I managed to get the type parameters in the generic functions to be instantiated by different function calls. Currently, I'm working with a very simple function f that can take two values x and y of type T and returns a value of type T.

T = TypeVar('T')

def f(x: T, y: T) -> T:
  return x + y

This function is called in two different places with different types. First with integers, second with floats.

print(f(1,2))
print(f(1.0,1.0))

This week I managed to have each function call generates a new overloaded function for f. It also checks the consistencies of the assigned types and the type parameters.

What is coming up next week

Next I have to also handle the function calls themselves, because they technically are calling overloaded functions. LPython actually have already supported function overloading, so what I need to do is to learn the compiler and apply that overloading to these generic function calls.

Did I get stuck anywhere

Nowhere especially for this week.

View Blog Post

Dealing with conflict and ASR (2nd week)

luthfan
Published: 06/24/2022

Hello again, here's a quick update on my second week with LPython!

What I did this week

It turned out that there were conflicts between my last week's work and the project's upstream, so the first thing I did this week was resolving that conflict. I also found another issue with existing integration tests, so some changes had to be made to make it possible for my work from last week to be submitted.

I also spent time to understand the differences between two AST visitors that build the ASR: a visitor that builds the symbol table and a visitor that transforms the statements.

What is coming up next week

I want to start implementing support to allow the following function declaration with type parameters:

def fun(x: T, y: T) -> T:
  return x+y

Did I get stuck anywhere

I didn't exactly understand the cause of the conflict at the beginning, but consulting with my mentor solved that issue.

View Blog Post

Adding type variable support to LPython (1st week)

luthfan
Published: 06/18/2022

Hello, here is a quick update of my first week with LPython project!

What I did this week

Unfortunately due to a personal issue, I had a very late start. I could not start until Thursday. Spent the day to reintroduce myself to the project and finally started making progress on Friday.

As planned, the first week was to give support for Python's type variable declaration T = TypeVar('T'). The syntax was already extended, so that's a okay. What I had to do then was to support the type variable in the underlying ASR (Abstract Semantic Representation) into which the Python AST will be translated. By the way, ASR is an IR from which we compile the program into lower languages like LLVM.

To do so, what I did was adding a new kind of type to the underlying ASR, the type variable. Then, while scanning through the AST, the type variable declaration T = TypeVar('T') is converted into a variable that is typed with an ASR type variable and then placed into the symbol table of the program, so that later on functions can identify these variables as type variables.

Slight note though, since I'm messing around with the ASR, the code generation for these type variables have to also be added later on.

What is coming up next week

Next week I will have to handle functions with type parameters. One important issue would be how to handle the type check on these functions, because they can't be type checked as usual where the base types are all present.

Did I get stuck anywhere

I was confused on whether to approach the issue by tinkering with the AST or going straight to ASR. However, since the ASR has many utilities for handling symbol table, I decided to go straight with ASR.

View Blog Post