tkachuk-labs

tkachuk-labs

Parametric generics, shrinking and negation in typespecs

Hello! Basically I want to express couple of types/specs. Type parametricity is working for types:

@type functor(a) :: functor(a)
@type either(a, b) :: {:error, a} | {:ok, b}

But is not working for specs. This will not compile for some reason:

@spec fmap((a -> b), functor(a)) :: functor(b)

I don’t want to define a and b. I want them to be generic - just some type a, and some type b which might be not equal to type a.

Another question is how to do shrinking and negation on type level, for example:

@type thunk ???
@type not_a_function ???
@type curried :: (term -> curried | thunk | not_a_function)
@spec curry(function) :: curried | thunk

where thunk is a function of arity equal to zero, and not_a_function is any type except function.

Thanks for your attention.

Most Liked

al2o3cr

al2o3cr

Typespecs are not Haskell-style types; trying to reason about them like they are is going to lead to confusion.

In @spec fmap((a -> b), t(a)) :: t(b) when a: term(), b: term(), term is acting as a bound - and a very loose one at that, as literally anything will satisfy it. This spec will be satisfied if fmap is given any one-arity function and any value, and promises that it will return any value.

The Erlang documentation for types can help explain this - the : operator in that when (spelled :: in Erlang) has a particular meaning and it isn’t equality:

Currently, the :: constraint (read as «is a subtype of») is the only guard constraint that can be used in the when part of a -spec attribute.

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New

Other popular topics Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

We're in Beta

About us Mission Statement