Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unclear behavior with externals #77

Open
leostera opened this issue Mar 7, 2021 · 1 comment
Open

Unclear behavior with externals #77

leostera opened this issue Mar 7, 2021 · 1 comment
Labels
bug Something isn't working compiler Related to the OCaml to Erlang compiler enhancement New feature or request

Comments

@leostera
Copy link
Owner

leostera commented Mar 7, 2021

When writing FFIs, regardless of the structure of the code on the Caramel side, we want all of it to go away.

I'd like FFIs to be cheap, so I can throw an external pretty much anywhere and be able to call that function.

Here's some examples that are inconsistent right now. The first external is the only one that behaves correctly I think:

(* file: todo.ml *)
external uniform : unit -> float  = ""
external uniform_with_ceiling : int -> int  = "uniform"

module Rand = struct
  external uniform : unit -> float  = ""
  external uniform_with_ceiling : int -> int  = "uniform"
end

external rand_uniform : unit -> float  = "rand:uniform"
external rand_uniform_with_ceiling : int -> int  = "rand:uniform"

let run () =
  let a = uniform () in
  let b = uniform_with_ceiling 2 in
  let c = Rand.uniform () in
  let d = Rand.uniform_with_ceiling 2 in
  let e = rand_uniform () in
  let f = rand_uniform_with_ceiling 2 in
  0

Generates the following Erlang:

-spec run() -> integer().                                                        
run() ->                                                                         
  A = uniform(),                                                                 
  B = uniform_with_ceiling(2),                                                   
  C = todo__rand:uniform(),                                                      
  D = todo__rand:uniform(2),                                                     
  E = rand_uniform(),                                                            
  F = rand_uniform_with_ceiling(2),                                              
  0.

Whereas what I'd expect is:

-spec run() -> integer().
run() ->
  A = uniform(),
  B = uniform(2),
  C = uniform(),
  D = uniform(2),
  E = rand:uniform(),
  F = rand:uniform(2),
  0.

NOTE: I'd expect this to work only because we have no structure within the symbol name string.

I think we need to do some refactoring in the compiler to make it easier to understand what's happening with the FFIs, so we can guarantee a more uniform behavior.

@leostera leostera added bug Something isn't working enhancement New feature or request compiler Related to the OCaml to Erlang compiler labels Mar 7, 2021
@leostera leostera changed the title Unclear behavior when renaming FFIs Unclear behavior with externals Mar 7, 2021
@leostera
Copy link
Owner Author

leostera commented Mar 7, 2021

Current workaround is to make a new .ml file with the name of the module you want, and make it include only externals.

(* file: rand.ml *)
external uniform : unit -> float  = ""
external uniform_with_ceiling : int -> int  = "uniform"

Generates no .erl file ✅ but when called as Rand.uniform () translates to rand:uniform().

Example:

let run () = Rand.uniform ()
run() -> rand:uniform().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler Related to the OCaml to Erlang compiler enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant