elpd

elpd

I have just seen ElixirConf 2023 - José Valim - The foundations of the Elixir type system , which is very interesting.

I do not know much about typing system. Regarding the union vs intersection of function types, there was something I did not grasp. For me it seems more correct the combined type is a union.

integer() -> integer()
def negate(n) when is_integer(n), do: -n

boolean() -> boolean()
def negate(b) when is_boolean(b), do: not b

The negate “symbol function” I thought would look like this:

negate: (integer() -> ineteger()) or (bool() -> bool())

So when the typing verification process start walking the code, if it arrives to an expression like negate(12) then it can go to the typing structure attached to the negate function symbol, lookup the matched typing from the options, since its or then find the first one matched, and use that to continue the verification and typing inference.

a = negate(12)
b = a + "word" 
# type error

How would the type walkthrough check would work? What intersection would mean?

Showing Posts 1 to 2

ityonemo

ityonemo

I don’t know how the elixir typesystem decides what “constitutes a type”

But if you take it to mean a “type is a subset of values that might not constitute a crash when passed as a value to any given function” it makes sense. If you search for “covariance” vs “contravariance” you will get a technical explanation (see paragraph 4 of: Type variance - Wikipedia)

But think of it this way:

integer() → integer() is all functions guaranteed to not crash when you pass an integer and returns an integer.

boolean() → boolean() is all functions guaranteed to not crash when you pass an boolean and returns a boolean.

if you did a union of those, then a function that doesn’t crash when you pass a boolean, but DOES crash when you pass an integer, is part of the union.

You really do want to type specify intersection because we have a stronger guarantee (the set of functions it’s a member of is smaller than either of the two descriptions). This is a function that doesn’t crash when you pass integer AND doesn’t crash when you pass a boolean. The AND relationship is captured by the intersection operator.

gldubc

gldubc

Hi! To be precise, for us,

integer() -> integer()

is functions that, when given an integer, are guaranteed – if they don’t crash – to return an integer (and not something else).

The intuition still holds though: a function that returns integers when given integers, and integers when given booleans has type integer() -> integer(). So it would be part of the union.

About crashes: typechecking does prevent them – if you annotate all your functions and avoid using dynamic(). If you do though, then crashes may happen but the guarantee remains that result of the function will be of the right type.

Also @elpd , when typing the application negate(12), we look at the type of its argument, and then at the each of the arrows in the intersection. Then, for each arrow, if the type of the argument can appear in the input type of the arrow, we add its return type to a union that is going to be the type of the application. In this case, only integer(). T

But if you give something that may be an integer or a boolean to negate, both arrows will be selected and the application will have type integer() or boolean(). This is how the walkthrough is done.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

wmnnd
Hey folks, I wanted to share with you a project I’ve been working on for a while: It’s an Open Source (AGPLv3) newsletter tool. Think Ma...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
sntran
In my third year of college, around 2010, I found Erlang. A few years later, I found Elixir. I liked the ideas at once: many small proces...
New
Ganna
Hi everyone, I have 8 years of production experience with Erlang/OTP and I’m just starting to get to know Elixir. What would you learn f...
New
johnkirkham
A note on form. Joe Armstrong, co-creator of the Erlang programming language, died in April 2019. What follows is a work of narrative non...
New
gtcode
I’ve been tinkering with Lynx (by José) via hold_ledger. I used AI to write the code, a separate specification, the tests, and the Lean p...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews