by some_thoughts · 3y ago · 0 comments
negate :: ((integer() -> integer()) and (true -> false) and (false -> true) and (a -> a) when a: not(integer() or boolean()))
harder to reason about quickly than say this
negate :: (int -> int) & (true -> false) & (false -> true) & (a -> a) when a: ~(int | boolean)
Is there a reason that syntax was chosen? Is it just for showcasing purposes or is that basically just how the language works?
You will for sure be able to drop the outer parens in your case and you _may_ be able to drop them on the function types too (to be decided). Ending up with something like this:
$ (integer -> integer) and (true -> false) and (false -> true) and (a -> a) when a: not(integer or boolean)
def negate(arg)
We will also allow intersections to be broken across multiple declarations: $ integer -> integer
$ true -> false
$ false -> true
$ a -> a when a: not(integer or boolean)
def negate(arg)Consider these two function signatures:
map :: ([a] -> [a])
map :: ([int] -> [int])
Furthermore the syntax that Elixir uses here let's you do something like
map :: (list(a) -> list(b))
list_to_other_data_structure :: (list(a) -> other_data_structure(a))
It all reminds me a bit of how Haskell does it https://medium.com/functional/haskell-basic-types-and-type-v...
I'm now curious to know if/how the above 'generics' would be expressed in TypeScript/Python/Go without a similar 'type constructor' syntax construct?
map :: forall a. ([a] -> [a])
or
map :: <a> ([a] -> [a])
Anyway coming from ocaml I don't aesthetically like this but coming from ocaml I have no grounds to talk shit about any language's aesthetic choices. The parens make sense to me when I think of them as type constructors, rather than concrete expressions of the type itself.
If you look at any erlang api doc, you’ll see predicate-like type specs. Conveniently, it naturally supports generics by just adding “parameters” to the predicates, so a dict would have type.
It works fine, and makes things less ambiguous, as erlang “types” can contain enumerations of values, in the original spec it’s clear that `integer()` is a type and `false` is a value, in your version not so much.