unisonweb / unison

A friendly programming language from the future
https://unison-lang.org
Other
5.74k stars 267 forks source link

Suffix-based name resolution uses definitions from the codebase instead of definitions from the current file #1578

Closed pchiusano closed 3 years ago

pchiusano commented 4 years ago

I don't have a good examples for this one, but if you have Bar.toList in your codebase and toList is a unique suffix, then define Foo.toList in your scratch file, toList will resolve to Bar.toList when I'd expect it to be ambiguous or perhaps prefer the local Foo.toList.

I've been bitten by this a few times and always find it very confusing.

aryairani commented 4 years ago

The file / not-yet-added distinction is pretty artificial IMO, I want a way to eliminate it.

Just have the file work like a temporarily added part of the codebase

chiroptical commented 4 years ago

I recently came across a motivating example. Trying to implement red-black tree a-la Okasaki leads to a situation where you can't match on a tree's color, a trivial example:

unique type Color = Red | Black
unique type RBTree a = Leaf | Tree Color (RBTree a) a (RBTree a)

use Color Red Black
use RBTree Leaf Tree

RBTree.isRed : RBTree a -> Boolean
RBTree.isRed = cases
  Tree Red _ _ _ -> true
  _ -> false

leads to:

  Sorry, you hit an error we didn't make a nice message for yet.

  Here is a summary of the Note:

    simple cause:
      PatternArityMismatch:
    loc=line 9, columns 3-17
    typ=Color -> RBTree a -> a -> RBTree a -> RBTree a
    args=1

    path:
      InMatch firstBody=line 9, columns 21-25
      InSynthesize e=match cdlsph512n wit...
      InCheck e=match cdlsph512n wit..., t=base.Boolean
      InCheck e=cases
    RBTree.Tree ..., t=RBTree a7 ->{𝕖4} base.Boolean
      InSynthesize e=(cases
    RBTree.Tree...
      InSynthesize e=let
    RBTree.isRed :...

If you remove the pattern match on the color, the code compiles. In this case, it is pretty easy to workaround. However, if you want to implement balance it becomes quite tedious. I am on Unison master:

git rev-parse --short HEAD
8b57038d

Let me know if I can provide any more details.

pchiusano commented 4 years ago

I've filed a separate issue (#1640) for that spurious pattern arity error (marked off topic above), since I just tried the same thing on an empty codebase and it's still happening. It looks like the cause is just some typechecking bug in pattern matching and it's definitely unrelated to suffix-based resolution.