leostera / caramel

:candy: a functional language for building type-safe, scalable, and maintainable applications
https://caramel.run
Apache License 2.0
1.06k stars 25 forks source link

Annotating types for function arguments compiles to wrong erlang #67

Closed michallepicki closed 3 years ago

michallepicki commented 3 years ago

Describe the bug When function arguments have their types specified in a function definition, the Erlang code produced is not valid. Currently only the function return type can be annotated without breaking.

To Reproduce

  1. Create a file main.ml containing:
    
    let print thing = Io.format "~0tp~n" [ thing ]

let add (x : int) (y : int) : int = x + y

let main _ = print (add 1 2)

2. Run command
```bash
$ caramel compile main.ml && escript main.erl
  1. See error
    Compiling main.erl      OK
    main.erl:11: function x/0 undefined
    main.erl:11: function y/0 undefined
    escript: There were compilation errors.

main.erl contains:

% Source code generated with Caramel.
-module(main).

-export([add/2]).
-export([main/1]).
-export([print/1]).

-spec print(_) -> ok.
print(Thing) -> io:format(<<"~0tp~n">>, [Thing | []]).

-spec add(integer(), integer()) -> integer().
add(_, _) -> erlang:'+'(fun x/0, fun y/0).

-spec main(_) -> ok.
main(_) -> print(add(1, 2)).

For some reason add arguments are ignored (_) and usages of the arguments are not valid (fun x/0 instead of just X).

Expected behavior

Erlang gets generated the same way as if the user didn't provide type annotations for the arguments:

-spec add(integer(), integer()) -> integer().
add(X, Y) -> erlang:'+'(X, Y).

Environment (please complete the following information):

michallepicki commented 3 years ago

I will look into that, possibly relates to https://github.com/AbstractMachinesLab/caramel/pull/38