Skip to content

How about recursive function? #8

@rhysd

Description

@rhysd

Hi, thank you for interesting implementations.

These type systems are implemented for tiny language. It contains Var, Call, Fun and Let expressions. While reading your algorithm_w implementation, I noticed that they lack important functional programming feature: recursive functions.

For recursive functions, type inference sometimes needs to care about them. Before applying type inference, type variable for the function should be added to type environment because function body may contain applying itself (recursive call).

For example, assume let rec exists and let's say to write let rec f x = f 10; 42 in f true. When visiting the LetRec node, inferrer makes type variable for f to type environment before apply type analysis to function body f 10; 42 for recursive function call. And then applies type inference to f 10. In function body, the type of f is determined to int -> int. This works for monomorphic function. But in this case, f is a polymorphic function 'a -> int. But function type is determined before generalized. Hence f true will fail after because of int -> int type. I feel some trick is necessary to support recursive function. What do you think about this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions