JSAbrahams / mamba

🐍 The Mamba programming language, because we care about safety
MIT License
85 stars 3 forks source link

Unifying with fun args creates unnecessary union #404

Closed JSAbrahams closed 1 year ago

JSAbrahams commented 1 year ago

Description of Bug

When unifying variables with function arguments, the union is used. In some situations this makes sense, but ideally the type should be derived from where the variables is declared first.

How to Reproduce

Input

def factorial(x: Int) -> Int => match x
    0 => 1
    n => n * factorial(n - 1)

def num := input("Compute factorial: ")
if num.is_digit() then
    def result := factorial(Int(num))
    print("Factorial {num} is: {result}.")
else
    print("Input was not an integer.")

Creates

from typing import Union
def factorial(x: int) -> int:
    match x:
        case 0:
            return 1
        case n:
            return n * factorial(n - 1)

num: Union[flot, int, str] = input("Compute factorial: ")  # <- here

if num.is_digit():
    result = factorial(int(num))
    print(f"Factorial {num} is: {result}.")
else:
    print("Input was not an integer.")

Expected behavior

Should create

from typing import Union
def factorial(x: int) -> int:
    match x:
        case 0:
            return 1
        case n:
            return n * factorial(n - 1)

num: str = input("Compute factorial: ")  # <- here

if num.is_digit():
    result = factorial(int(num))
    print(f"Factorial {num} is: {result}.")
else:
    print("Input was not an integer.")