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

Morphir-elm compiler allows runtime polymorphism and type errors #1146

Open
edwardpeters opened this issue Feb 21, 2024 · 1 comment
Open
Labels
task Task level project item

Comments

@edwardpeters
Copy link

edwardpeters commented Feb 21, 2024

Describe the bug
The type inferencing algorithm used by morphir-elm make handles type variables in a way which allows users to compile polymorphic code.

To Reproduce
The following code compiles and runs:

cast : a -> b
cast x = x

foo : String -> String -> String
foo s1 s2 = String.concat [s1, s2]

bar : Int -> Int -> String
bar i1 i2 = foo (cast i1) (cast i2)

baz : String -> String
baz s = bar (cast s) (cast s)

baaz : a -> b -> String
baaz a b = String.concat[cast a, cast b]

baz runs. baaz also runs, specifically if fed two strings as input - otherwise it results in "Unable to compute", which in this case I believe indicated a runtime type error.

Expected behavior
As defined, cast should be rejected due to a unification error between a and b. (This is the behavior in native elm.)

Desktop (please complete the following information):

  • OS: OSX
  • Version: 2.89.0

Additional context
I believe this is due to the compiler not distinguishing between generic type variables and free type variables. When inference runs on cast, it therefor tries to unify a with b; as it treats them both as free, it thinks it is able to do so. When inference runs on calling code, it sees them as being independent, and can bind the input and output types to whatever it wants.

This is highly related to #1102 , but demonstrates broader type issues.

@edwardpeters
Copy link
Author

@AttilaMihaly This is the issue I mentioned the other week, which I think you felt deserved fixing in the compiler. Just confirmed that it remains in effect on the latest version.

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

No branches or pull requests

1 participant