Comments (2)
Good catch! The problem is in the normal forms used to do type simplification. It shows (1, 2, "hello",) | (true, 3,)
as equivalent to anything
, but the real least upper bound here, at east according to the half-implemented semantics mentioned in #69, would be {_1: 1 | true; _2: 2 | 3}
.
The specific problematic line is:
Pro-tip: here is how I debugged the issue:
:ns // show the unsimplified type (which is the one used in later checks)
res = if true then (1,2, "hello") else (true, 3)
//│ res: 'a | (1, 2, "hello",) | (true, 3,)
//│ = [ 1, 2, 'hello' ]
:d // show the constraining trace
res._1
//│ 0. Typing term res._1
//│ | 0. Typing term res
//│ | 0. : [α0]
//│ | CONSTRAIN [[α0]] <! {_1: α1}
//│ | where α0 :> [(1<int,number>, 2<int,number>, "hello"<string>)] | [([true<bool>], 3<int,number>)]
//│ | C [[α0]] <! {_1: α1}
//│ | | C [α0] <! {_1: α1}
//│ | | | C α0 <! {_1: α1}
//│ | | | | C [(1<int,number>, 2<int,number>, "hello"<string>)] <! {_1: α1}
//│ | | | | | C (1<int,number>, 2<int,number>, "hello"<string>) <! {_1: α1}
//│ | | | | | | C {_1: 1<int,number>, _2: 2<int,number>, _3: "hello"<string>} <! {_1: α1}
//│ | | | | | | | C 1<int,number> <! α1
//│ | | | | C [([true<bool>], 3<int,number>)] <! {_1: α1}
//│ | | | | | C ([true<bool>], 3<int,number>) <! {_1: α1}
//│ | | | | | | C {_1: [true<bool>], _2: 3<int,number>} <! {_1: α1}
//│ | | | | | | | C [true<bool>] <! α1
//│ 0. : α1
//│ Typed as: α1
//│ where: α1 :> [true<bool>] | 1<int,number>
[...]
Here we can see that the result of the if-then-else is a type variable α0
with two lower bounds, both of which are subtypes of the {_1: α1}
upper bound required to type the field selection.
from mlscript.
Fixed in #79
res = if true then (1,2, "hello") else (true, 3)
//│ res: Array["hello" | 1 | 2 | 3 | true] & {_1: 1 | true, _2: 2 | 3}
res._1
//│ res: 1 | true
res = if true then (1,2, "hello") else (true, 3)
res._3
//│ res: Array["hello" | 1 | 2 | 3 | true] & {_1: 1 | true, _2: 2 | 3}
//│ ╔══[ERROR] Type mismatch in field selection:
//│ ║ l.+2: res._3
//│ ║ ^^^^^^
//│ ╟── tuple literal of type `{_1: true, _2: 3}` does not have field '_3'
//│ ║ l.+1: res = if true then (1,2, "hello") else (true, 3)
//│ ║ ^^^^^^^^^
//│ ╟── but it flows into reference with expected type `{_3: ?a}`
//│ ║ l.+2: res._3
//│ ╙── ^^^
//│ res: "hello" | error
from mlscript.
Related Issues (20)
- Simplify the `with` construct semantics
- Implement new MLscript syntax
- Traits should be implemented as proper mixins
- The next step of code generation
- TypeScript Libraries Support HOT 18
- Add comprehensive contribution guide
- Add documentation to the MLscript code base
- Type errors in class definitions are swallowed in the web demo
- Add support for class type difference HOT 1
- UCS tracking issue HOT 7
- Add enumerated types (not ADTs)
- Iron out semantics of new object definition parameters and fields HOT 3
- Implement virtuality and constructor restrictions HOT 1
- User-defined operators can lead to insufficient fuel and constraint leakage HOT 3
- Potential infinite loop in type checking HOT 3
- Constructors can be called with invalid parameters HOT 1
- Wrong mixin typing
- Some generic object types are not lattice homomorphisms and those which are should not be forall-distributivity targets
- Infinite loop in expanding abstract class with self type HOT 4
- Quasiquote syntax support HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mlscript.