Skip to content
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

Wrong inferred type for values of dictionary #59

Closed
caterinaurban opened this issue Jul 28, 2017 · 6 comments
Closed

Wrong inferred type for values of dictionary #59

caterinaurban opened this issue Jul 28, 2017 · 6 comments
Labels

Comments

@caterinaurban
Copy link
Owner

The type inferred for env in the following program is wrong:

class Aexp:
    def eval(self, env):
        return 0

class IntAexp(Aexp):
    def __init__(self, i):
        self.i = i

    def eval(self, env):
        return self.i

class BinopAexp(Aexp):
    def __init__(self, op, left, right):
        self.op = op
        self.left = left
        self.right = right

    def eval(self, env):
        left_value = self.left.eval(env)
        right_value = self.right.eval(env)
        if self.op == '/':
            value = left_value / right_value
            return value
        else:
            raise RuntimeError('unknown operator: ' + self.op)

class Statement:
    def eval(self, env):
        pass

class AssignStatement(Statement):
    def __init__(self, name, aexp):
        self.name = name
        self.aexp = aexp

    def eval(self, env):
        value = self.aexp.eval(env)
        env[self.name] = value

"""
x = 0
x = 1 / 2
"""

env = dict()
i = AssignStatement('x', IntAexp(0))
d = BinopAexp('/', IntAexp(1), IntAexp(2))
f = d.eval(env)
a = AssignStatement('x', d)
a.eval(env)
print(env)

The type should be Dict[str, float] but Dict[str, int] is inferred instead.

@caterinaurban caterinaurban changed the title Inference infers wrong type Wrong inferred type for values of dictionary Jul 28, 2017
@mostafa-abdullah
Copy link
Contributor

The reason for this is that the type of self.aexp in AssignmentStatement is of type Aexp, and method eval in Aexp returns an int.

This will be solved after merging the covariance/contravariance PR, because in this case the return type of eval in Aexp will be a supertype of the return type of any overriding method.

@caterinaurban
Copy link
Owner Author

I tried the covariance/contravariace PR and it returns unsat...

@mostafa-abdullah
Copy link
Contributor

Yes it should give unsat because the return type of eval in Aexp is still int.

Changing return 0 to return 1.0 will give sat

@caterinaurban
Copy link
Owner Author

The program is perfectly correct, I do not think we should change it. The inference should just be able to infer type float for eval in Aexp. We probably have a type equality somewhere where we should have a subtyping relation instead.

@mostafa-abdullah
Copy link
Contributor

Done

@caterinaurban
Copy link
Owner Author

This issue was moved to caterinaurban/Typpete#2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants