unisonweb / unison

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

Function type inferred with additional generic ability parameters when recursing #5421

Open bbarker opened 4 weeks ago

bbarker commented 4 weeks ago

Describe and demonstrate the bug

Input:

baz: a -> b -> a -> b
baz a1 bIn a2 = bIn

zabb: (a -> b) -> a -> b
zabb fun a = fun (zaba fun (fun a))

zaba: (a -> b) -> b -> a
zaba fun b = let 
    b2 = zabb fun (zaba fun b)
    zaba fun b2

once: (a -> a) -> a -> a
once fun a = fun a

another: (a -> a) -> a -> a
another fun a = a

twice: (a -> a) -> a -> a
twice fun a = once fun (once fun a)

both: (a -> a) -> a -> a
both fun a = another fun (once fun a)

Output:

      another : (a ->{g} a) -> a ->{g} a
      bar     : (a ->{g} b) -> a ->{g} b
      baz     : a -> b -> a -> b
      both    : (a ->{g} a) -> a ->{g} a
      once    : (a ->{g} a) -> a ->{g} a
      twice   : (a ->{g} a) -> a ->{g} a
      zaba    : (a ->{g1, g} b) -> b ->{g1, g} a
      zabb    : (a ->{g1, g} b) -> a ->{g1, g} b

However, there are a couple of workarounds. The first is not including any type signature in the definition:

Output:

      another : fun -> a -> a
      baz     : a1 -> bIn -> a2 -> bIn
      both    : (i ->{g} t) -> i ->{g} t
      once    : (i ->{g} o) -> i ->{g} o
      twice   : (t ->{g} t) -> t ->{g} t
      zaba    : (i ->{g} o) -> o ->{g} i
      zabb    : (i ->{g} o) -> i ->{g} o

Another workaround that may work in some cases is to use type signatures but with the empty set of abilities specified ({}):

q : (a -> {} t) -> [t]
q x = List.map x []

Environment (please complete the following information):

Additional context

Originally discussed on discord

bbarker commented 2 weeks ago

I recently came across this with uncurry as well; had to do this:

uncurryPure: (a ->{} b ->{} c) -> (a, b) ->{} c
uncurryPure = uncurry

areAdsenseScripts: (Html, Html) -> Boolean
areAdsenseScripts = allFlip [
    uncurryPure <| srcContains "pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"
  , uncurryPure <| isScript
  ]