Commit graph

819 commits

Author SHA1 Message Date
Brian Carroll
59472d3310 Slightly improve pretty printing of inc statement 2021-12-30 09:01:22 +00:00
ayazhafiz
1f81a598f7 Presence constraint flag -> Enum 2021-12-23 19:40:18 -06:00
ayazhafiz
b97ff380e3 Presence constraints for tag union types
This work is related to restricting tag union sizes in input positions.
As an example, for something like

```
\x -> when x is
    A M -> X
    A N -> X
    A _ -> X
```

we'd like to infer `[A [M, N]* ]` rather than the `[A, [M, N]* ]*` we
infer today. Notice the difference is that the former type tells us we
only accepts `A`s, but the argument of the `A` can be `M`, `N` or
anything else (hence the `_`).

So what's the idea? It's an encoding of the "must have"/"might have"
design discussed in https://github.com/rtfeldman/roc/issues/1758. Let's
take our example above and walk through unification of each branch.

Suppose `x` starts off as a flex var `t`.

```
\x -> when x is
    A M -> X
```

Now we introduce a new kind of constraint called a "presence"
constraint. It says "t has at least [A [M]]". I'll notate this as `t +=
[A [M]]`. When `t` is free as it is here, this is equivalent to `t ~
[A [M]]`.

```
\x -> when x is
    ...
    A N -> X
```

At this branch we introduce the presence constraint `[A [M]] += [A [N]]`.
Notice that there's two tag unions we care about resolving here - one is
the toplevel one that says "I have an `A ...` inside of me", and the
other one is the tag union that's the tyarg to `A`. They are distinct
and at different depths.

For the toplevel one, we first figure out if the number of tags in the
union needs to expand. It does not - we're hoping to resolve the type
`[A [M, N]]`, which only has `A` in the toplevel union. So, we don't
need to do anything extra there, other than the merge the nested tag
unions.

We recurse on the shared tags, and now we have the presence constraint
`[M] += [N]`. At this point it's important to remember that the left and
right hand types are backed by type variables, so this is really
something like `t11 [M] += t12 [N]`, where `[M]` and `[N]` are just what
we know the variables `t11` and `t12` to be at this moment. So how do we
solve for `t11 [M, N]` from here? Well, we can encode this constraint as
a type variable definition and a unification constraint we already know
how to solve:

```
New definition: t11 [M]a    (a fresh)
New constraint: a ~ t12 [N]
```

That's it; upon unification, `t11 [M, N]` falls out.

Okay, last step.

```
\x -> when x is
    ...
    A _ -> X
```

We now have `[A [M, N]] += [A a]`, where `a` is a fresh unbound
variable. Again nothing has to happen on the toplevel. We walk down and
find `t11 [M, N] += t21 a`. This is actually called an "open constraint"; we
differentiate it at the time we generate constraints because it follows
syntactically from the presence of an `_`, but it's semantically
equivalent to the presence constraint `t11 [M, N] += t21 a`. It's just
called opening because literally the only way `t11 [M, N] += t21 a` can
be true is if we set `t11 a`. Well, actually, we assume `a` is a tag
union, so we just make `t11` the open tag union `[M, N]a`. Since `a` is
unbound, this eventually becomes a wildcard and hence falls out `[M, N]*`.
Also, once we open a tag union with an open constraint, we never close
it again.

That's it. The rest falls out recursively. This gives us a really easy
way to encode these ordering constraints in the unification-based system
we have today with minimal additional intervention. We do have to patch
variables in-place sometimes, and the additive nature of these
constraints feels about out-of-place relative to unification, but it
seems to work well.

Resolves #1758
2021-12-23 19:40:18 -06:00
ayazhafiz
a626214852 Fix use of undeclared symbol
090a8923c5 changed `Located -> Loc`, but
5d7aff373c introduced another instance of
`Located` and didn't have the commit history of 090a8923c5
2021-12-23 17:58:12 -06:00
Folkert de Vries
5f7476d54f
Merge pull request #2266 from rtfeldman/joshuawarner32/loc
Parser refactor: always group (Row, Col) into Position
2021-12-24 00:02:13 +01:00
Folkert de Vries
db44d03e66
Merge pull request #2259 from rtfeldman/i/2227-record-layout-hang
Turn invalid record field types into runtime errors
2021-12-23 20:17:34 +01:00
Joshua Warner
22e2545fd6 format 2021-12-22 20:46:42 -08:00
Joshua Warner
f19220473a Rename Located -> Loc 2021-12-22 19:18:22 -08:00
ayazhafiz
ac54a5e024 Remove nonsene panic 2021-12-22 19:38:10 -06:00
ayazhafiz
ed64ff912a Implement List.dropIf
This was referenced in the `List` documentation and in the
[tutorial](./TUTORIAL.md), but wasn't actually implemented prior to this
commit!

Part of #2227
2021-12-22 12:34:48 -06:00
ayazhafiz
903fa7d363 Fix clippy warning: implement From over Into 2021-12-21 19:55:50 -06:00
ayazhafiz
576f1293fd Turn invalid record field types into runtime errors
By emitting a runtime error rather than panicking when we can't layout
a record, we help programs like

```
main =
    get = \{a} -> a
    get {b: "hello world"}
```

execute as

```
Mismatch in compiler/unify/src/unify.rs Line 1071 Column 13
Trying to unify two flat types that are incompatible: EmptyRecord ~ { 'a' : Demanded(122), }<130>

🔨 Rebuilding host...
── TYPE MISMATCH ───────────────────────────────────────────────────────────────

The 1st argument to get is not what I expect:

8│      get {b: "hello world"}
            ^^^^^^^^^^^^^^^^^^

This argument is a record of type:

    { b : Str }

But get needs the 1st argument to be:

    { a : a }b

Tip: Seems like a record field typo. Maybe a should be b?

Tip: Can more type annotations be added? Type annotations always help
me give more specific messages, and I think they could help a lot in
this case

────────────────────────────────────────────────────────────────────────────────

'+fast-variable-shuffle' is not a recognized feature for this target (ignoring feature)
'+fast-variable-shuffle' is not a recognized feature for this target (ignoring feature)
Done!
Application crashed with message

    Can't create record with improper layout

Shutting down
```

rather than the hanging

```
Mismatch in compiler/unify/src/unify.rs Line 1071 Column 13
Trying to unify two flat types that are incompatible: EmptyRecord ~ { 'a' : Demanded(122), }<130>

thread '<unnamed>' panicked at 'invalid layout from var: UnresolvedTypeVar(104)', compiler/mono/s
rc/layout.rs:1510:52
```

that was previously produced.

Part of #2227
2021-12-21 19:11:59 -06:00
Folkert
28b4388488 remove unneeded field 2021-12-05 15:25:46 +01:00
Richard Feldman
e6bec46898
Merge pull request #2109 from rtfeldman/refactor-passed-function
refactor passed (to higher order lowlevel) funcion
2021-12-01 08:17:39 -05:00
Chelsea Troy
0e5f82526a Account for SingleQuote in the case statements for the cli, plus some errata
+ Evidently I failed to finish fixing merge conflicts
+ Some of the types that the SingleQuote code mentioned didn't exist according to the build step. I looked around and switched them out for types it LOOKED like they were supposed to be, but someone should probably check this
+ Don't make commits like this; it's multiple unrelated changes thrown together. I'm still figuring out my way around here
2021-12-01 00:13:15 -06:00
Folkert
bec74c36c5 refactor passed (to higher order lowlevel) funcion 2021-11-30 21:50:27 +01:00
Brian Carroll
c2a2ff2957 Create Expr::to_pretty in mono IR 2021-11-30 09:57:26 +00:00
Brian Carroll
61575cea7e Generate calls to refcount procs from Wasm backend 2021-11-30 09:57:00 +00:00
Chelsea Troy
6cf755ad8d Resolve a bunnnch of merge conflicts 2021-11-29 23:14:29 -06:00
Folkert
ab1787937d shrink CallSpecId and UpdateModeId to u32; assuming a 4GiB file that should be more than enough 2021-11-28 22:59:23 +01:00
Folkert
aefe719e56 hook up update mode for reset/reuse 2021-11-28 14:25:51 +01:00
Folkert
0bdda2506c add update mode to reset and reuse 2021-11-28 14:13:02 +01:00
Folkert
1241d5ccbd make UpdateModeIds a proper type 2021-11-28 14:03:48 +01:00
Folkert
a1fd34feef remove empty layout types (list,str,dict,set) 2021-11-27 14:05:16 +01:00
Folkert
f96d60a13e Merge remote-tracking branch 'origin/trunk' into layout-builtin-numbers-refactor 2021-11-21 23:19:55 +01:00
Folkert
385224301e turn off ir printing 2021-11-21 22:30:05 +01:00
Folkert
2d4e6b414c pass a pointer width of 4 to wasm test gen 2021-11-21 21:37:15 +01:00
Folkert de Vries
520210da6b
Merge pull request #2041 from rtfeldman/refcount-comments
Add comments to ModifyRc enum
2021-11-21 19:30:20 +01:00
Folkert
64869ffb8b Merge remote-tracking branch 'origin/trunk' into layout-builtin-numbers-refactor 2021-11-21 19:22:32 +01:00
Folkert
7fc79c3d40 fix typo 2021-11-21 19:20:05 +01:00
Folkert
dc44eaac97 cleanup 2021-11-21 14:11:18 +01:00
Folkert
2033f1f430 remove usize in mono 2021-11-21 13:22:45 +01:00
Brian Carroll
01f81c5092 Add comments to ModifyRc enum 2021-11-21 07:55:09 +00:00
Richard Feldman
2db18890d2
Merge pull request #2026 from rtfeldman/mono-remove-solved-type
Mono remove solved type
2021-11-20 23:29:38 -05:00
Folkert
c4ec9aa898 working mono 2021-11-20 23:25:30 +01:00
Richard Feldman
49fd864a2f
Merge pull request #1996 from rtfeldman/add_list_all
adding List.all
2021-11-19 17:55:04 -05:00
Folkert
991420731d rename function 2021-11-19 23:49:09 +01:00
Folkert
38b5e627d7 enforce only one direct host-exposed specialization 2021-11-19 23:30:35 +01:00
Folkert
555a28af83 remove SolvedType from mono/src/ir.rs 2021-11-19 22:58:24 +01:00
Folkert
dddfddc835 refactor 2021-11-19 22:38:24 +01:00
Folkert
640b4e2d0b cleanup 2021-11-19 22:25:37 +01:00
Folkert
74f073b3c6 use HostSpecializations from file.rs 2021-11-19 22:20:50 +01:00
Folkert
5d9d2b7fea add specialize_host_specializations 2021-11-19 22:15:02 +01:00
Folkert
6baffdf6fb try using solvedtype in HostSpecializations (will remove this later) 2021-11-19 22:08:03 +01:00
Folkert
41806e4093 fix specialize_variable call 2021-11-19 21:32:47 +01:00
Folkert
b43101fca5 add specialize_suspended 2021-11-19 21:30:14 +01:00
Folkert
992016d2ae add HostSpecializations 2021-11-19 21:29:34 +01:00
Folkert
e54344ac8b storage subs for External specializations 2021-11-19 17:10:30 +01:00
Folkert
206c8889df Use StorageSubs for pending specializations 2021-11-19 13:22:17 +01:00
Michael Downey
d11bb93539
Merge branch 'trunk' into add_list_all 2021-11-18 16:09:24 -05:00