tokiwa-software / fuzion

The Fuzion Language Implementation
https://fuzion-lang.dev
GNU General Public License v3.0
46 stars 11 forks source link

type inference for nested `Sequence` #2662

Open maxteufel opened 6 months ago

maxteufel commented 6 months ago
$ cat ex_seq.fz
ex_seq =>
  f (T type, a Sequence (Sequence T)) =>
    unit

  y := [[1,2,3].as_list, [4,5,6].as_list,[7,8,9].as_list].as_list

  f y

$ fz ex_seq.fz

ex_seq.fz:7:5: error 1: Incompatible types when passing argument in a call
  f y
----^
Actual type for argument #1 'a' does not match expected type.
In call to          : 'ex_seq.f'
expected formal type: 'Sequence (Sequence i32)'
actual type found   : 'ref list (list i32)'
assignable to       : 'Any',
                      'Sequence (list i32)',
                      'ref choice nil (Cons (list i32) (list (list i32)))',
                      'ref list (list i32)'
for value assigned  : 'y'
To solve this, you could change the type of the target 'a' to 'ref list (list i32)' or convert the type of the assigned value to 'Sequence (Sequence i32)'.

one error.
michaellilltokiwa commented 6 months ago

Type inference here is ambiguous since it may change if y would be used in another place where e.g. array (Sequence T)) would be expected. Does it work if [[1,2,3], [...]] is directly passed to f?

maxteufel commented 6 months ago

Type inference here is ambiguous since it may change if y would be used in another place where e.g. array (Sequence T)) would be expected. Does it work if [[1,2,3], [...]] is directly passed to f?

It doesn't work either. But maybe my phrasing is wrong, it is obviously perfectly fine if the type gets inferred to be list (list i32), but this should be assignment compatible with Sequence (Sequence i32).