Designing user-defined restrictions (11th week)

Published: 08/29/2022

My apologies for posting this very late.

What I did this week

1. Merged the hard-coded restrictions

After making some fixes on the implementation, I had the hard-coded restriction that I explained in the previous post merged.

2. Designed user-defined restrictions

Ultimately, the restrictions on type parameters that we want for LPython is one that users can freely defined according to need. While the hard-coded restrictions can support generics to a certain degree, it cannot handle the various functions that users may define.

Take the mean example from the previous week. With user-defined restrictions, it turns into:

T = TypeVar('T')

def zero(x: T) -> T:

def plus(x: T, y: T) -> T:

def div(x: T, k: i32) -> f64:

def mean(x: list[T]) -> f64:
    k: i32 = len(x)
    if k == 0:
        return 0.0
    res: T
    res = zero(x[0])
    i: i32
    for i in range(k):
        res = plus(res, x[i])
    return div(res, k)

With the hard-coded restrictions, the mean function is limited to handling numbers. However, with user-defined restrictions, it would be possible to pass arguments of any types as long as they have the restriction functions plus, zero, and div provided.

What is coming up next week

I have to conduct a meeting with my mentor and evaluate the current design for user-defined restrictions. Then iterate again on the designs based on the meeting.

Did I get stuck anywhere

I did not have any issue this week as I was freely designing the grammar for the restrictions.