luthfan's Blog

Adding more generic examples (7th week)

luthfan
Published: 07/31/2022

What I did this week

- Generic array types

I added support for generic array types. In the following simple example, T[:] signifies the type of one-dimensional array T.

T = TypeVar('T')

def f(lst: T[:], i: T) -> T:
    lst[0] = i
    return lst[0]

It is then possible to have different function calls involving different types of arrays:

f([1],2)
f([1.0],2.0)

To implement this, the type substitution checks between the formal parameter lst: T[:] and the argument [1] (i32[:]) match the type parameter T with i32 and to also check that the dimensions are equal.

- Merging TemplateFunction with Function

To distinguish generic functions from functions, I added a new syntactic construct TemplateFunction into LPython's intermediate representation ASR. However, this construct appears to be unnecessary and can be handled by the original Function construct by adding another field where type parameters are stored.

On a separate PR, I removed TemplateFunction from the grammar and modified the definition for Function.

What is coming up next week

There are tests that the compiler does not yet pass. So I will have to solve those first.

Did I get stuck anywhere

There was no issue specifically this week.

View Blog Post

Completing the generics early prototype (6th week)

luthfan
Published: 07/23/2022

What I did this week

I completed an early prototype of generic function support in LPython. Given the following simple generic function and its function calls:

T = TypeVar('T')

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

f(1,2)
f('a','b')

The compiler can correspondingly instantiate two functions each for the integer call f(1,2) and character call f('a','b'). The function body is properly generated for the instantiated functions.

Further, the program can also be compiled into LLVM. This is done simply by ignoring the type parameter declaration T = TypeVar('T') as well as the functions involving type parameters.

For reference, all my progress is still kept as an open pull request. This will be merged once we finalize the overall design of generic functions.

What is coming up next week

1. Making decisions on the design for generic functions.
2. Making other examples and adding support for other statements and expressions for generic functions.

Did I get stuck anywhere

While rebuilding the body of the instantiated functions from the generic function, I met more problems with the ASR (LPython's intermediate representation) verification. One particular issue was that it was impossible to understand the problem simply by looking at the structure of the generated ASR. I solved this issue by looking at the other existing visitor classes that also build function bodies and try to mimic their process.

View Blog Post

Getting the generated ASR for generic functions (5th week)

luthfan
Published: 07/15/2022

What I did this week

Following on the suggestion from my mentor, I reimplemented the generic function instantiation to use the already existing visitors within LPython compiler. My mentor also helped with some issues with the ASR verification which we solved by adding verification support for the constructs I added into the ASR.

This week I also worked on including another important missing piece of the instantiated generic functions, the function body.

What is coming up next week

1. Finishing up the function body of instantiated generic functions.
2. Work on LLVM compilation support for the generic functions.

Did I get stuck anywhere

I was not aware of the verification step for the ASR during the compilation and my mentor explained and helped with the implementation.

View Blog Post

Revisiting function generation (4th week)

luthfan
Published: 07/11/2022

What I did this week

I'm afraid there is not much progress this week because I've been visiting relatives. I've improved the function generation implemented last week.

What is coming up next week

Finishing up the initial example case for generics. This means getting an ASR without triggering any errors.

Did I gets stuck anywhere

I had a personal meeting with my mentor to discuss the progress far and I found a simpler way to implement the work than what I am doing right now.

 

View Blog Post

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