-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit implements RFC #670. We already have support for argument polymorphism, e.g.: ``` function vec_length(v: Vec<'X>): usize ``` Here we introduce ad hoc polymorphism, that additionally allows defining functions with the same name for different types, e.g.,: ``` function size(v: Set<'X>): usize {...} function size(v: Map<'X>): usize {...} ``` The compier uses the number of arguments and the type of the _first_ argument to disambiguate the callee: ``` // (1) function size(v: Vec<'X>): usize {...} // OK: different number of arguments function size(v: Vec<'X>, foo: 'Y): usize {...} // OK: different type of the first argument. function size(v: Set<'X>): usize {...} // ERROR: conflicts with (1) function size(v: Vec<bool>): usize {...} ``` This improves DDlog code in a coupe of ways: - We no longer need to prefix function names with type or module names - Once we support object-oriented function call notation, it will be nicer to write `x.length()` vs `x.vec_length()` - We will be able to get rid of the horrible hack with string conversions functions where the function name is computed by addint `2string` to the type name, and just name all of them `to_string()`. Implementation details: - Within a module, we always report conflicts regardless of whether conflicting functions are invoked anywhere in the program. When conflicting declarations appear in different modules, we postpone the check until the user makes a function call that cannot be unambiguously resolved to one of the candidates, i.e., we won't complain even if the user imports both modules unqualified unless they try calling the function. - Rust does not allow functions with the same name unless they appear inside different modules or impl blocks. Both options don't work well for us. Instead the compiler mangles names of polymorphic functions by adding the type of the first argument and the number of arguments to the name. This is invisible to the user, except in extern functions, which must be named-mangled manually. This is ugly and fragile; hence the preferred solution is to make sure that extern function names are unique, and define aliases to them in DDlog. E.g, in std.dl we keep old function names like string_substr and add new functions, e.g., substr(string,...) that invoke them. This commit also adds support for recursive functions.
- Loading branch information
Showing
54 changed files
with
2,322 additions
and
691 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.