Code Monkey home page Code Monkey logo

Comments (2)

LPTK avatar LPTK commented on May 28, 2024

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:

if (fs1.size =/= fs2.size) N

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.

LPTK avatar LPTK commented on May 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.