gleam icon indicating copy to clipboard operation
gleam copied to clipboard

Improve type inference of recursive functions

Open lpil opened this issue 5 years ago • 6 comments

Gleam should be able to infer the types of this code:

pub fn call_with_self(f) {
  f(f)
}

Currently it cannot.

https://elixirforum.com/t/gleam-a-statically-typed-language-for-the-erlang-vm/20349/165

lpil avatar Dec 17 '19 15:12 lpil

so we can implement the let rec first? https://github.com/zjhmale/DHM/blob/master/src/hm/algw.clj#L139-L145

then the recursive function definition will be

pub rec fn fibonacci(n) {
    case n {
        0 | 1 -> 1
        _ ->  fibonacci(n -1) + fibonacci(n - 2) 
    }
}

there should always be a rec keyword before fn keyword

swr1bm86 avatar Feb 09 '20 09:02 swr1bm86

That should already work, all top level functions are implicitly letrec

lpil avatar Feb 09 '20 10:02 lpil

Thank you for sharing that existing language, very cool!

lpil avatar Feb 09 '20 10:02 lpil

hmm, so which part of type inference for recursion should we improve? I can not find any clue in the elixir forum thread.

swr1bm86 avatar Feb 09 '20 12:02 swr1bm86

Gleam should be able to infer the types of this code:

pub fn call_with_self(f) {
  f(f)
}

Currently it cannot. Sorry for the confusion, I have not been putting much detail into these issues.

lpil avatar Feb 09 '20 13:02 lpil

no worries will keep an eye on this issue then :)

swr1bm86 avatar Feb 09 '20 13:02 swr1bm86