Fl4m3Ph03n1x
Background
I am trying out polymorphic typing with dialyzer. As an example I am using the famous Option type (aka, Maybe Monad) that is now prevalent in many other languages these days.
defmodule Test do
@type option(t) :: some(t) | nothing
@type some(t) :: [{:some, t}]
@type nothing :: []
@spec validate_name(String.t()) :: option(String.t())
def validate_name(name) do
if String.length(name) > 0 do
[{:some, name}]
else
nil
end
end
end
As you can see, the function validate_name should return (by spec definition) [{:some, String.t}] | [].
The issue here is that in reality, the function is returning [{:some, String.t}] | nil. nil is not the same as empty list [].
Problem
Given this issue, I would expect dialyzer to complain. However it gladly accepts this erroneous spec:
$ mix dialyzer
Compiling 1 file (.ex)
Finding suitable PLTs
Checking PLT...
[:compiler, :currying, :elixir, :gradient, :gradualizer, :kernel, :logger, :stdlib, :syntax_tools]
PLT is up to date!
No :ignore_warnings opt specified in mix.exs and default does not exist.
Starting Dialyzer
[
check_plt: false,
init_plt: '/home/user/Workplace/fl4m3/grokking_fp/_build/dev/dialyxir_erlang-24.2.1_elixir-1.13.2_deps-dev.plt',
files: ['/home/user/Workplace/fl4m3/grokking_fp/_build/dev/lib/grokking_fp/ebin/Elixir.Book.beam',
'/home/user/Workplace/fl4m3/grokking_fp/_build/dev/lib/grokking_fp/ebin/Elixir.DealingWithListsOfLists.beam',
'/home/user/Workplace/fl4m3/grokking_fp/_build/dev/lib/grokking_fp/ebin/Elixir.Event.beam',
'/home/user/Workplace/fl4m3/grokking_fp/_build/dev/lib/grokking_fp/ebin/Elixir.FlatMapsVSForComprehensions.beam',
'/home/user/Workplace/fl4m3/grokking_fp/_build/dev/lib/grokking_fp/ebin/Elixir.ImmutableValues.beam',
...],
warnings: [:unknown]
]
Total errors: 0, Skipped: 0, Unnecessary Skips: 0
done in 0m1.09s
done (passed successfully)
Furthermore, no matter what I put in the else branch, the result is always a “happy dialyzer”.
Question
At this point, the only logical solution I can think of is that dialyzer is only concerned with the happy path. Meaning, it will ignore my else branch.
If dialzyer is only ever concerned with happy paths, then this would explain the issue (it is called success typing after all) but it also means it will totally miss a bunch of errors in my code.
- Is my assumption about dialyzer correct?
- Is there a way to make it more precise in finding errors, or is this a limitation of the algorithm used by dialyzer ? (and therefore no fix is possible)
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #elixirconf-us
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
You can try reversing the conditional and you’ll see it’s not about happy path or not (how would dialyzer even know what a happy path is). Dialyzer only takes issue if it there is no way your function will ever work.
For your
validate_name/1there’s plenty of inputs, which are ok. Basically anything, which is not""will produce a return value aligning with your typespec. If you remove the conditional and returnnilfor every case and suddenly dialyzer is no longer as happy. It detected that the only possible return isnil, which doesn’t align with your typespec.You can dig deeper by adding another function to your module:
This one dialyzer will complain about.
As you can see dialyzer never considered the
nothingbranch of your union a return value ofvalidate_name.To my knowledge this is because dialyzer inferred
validate_nameto returnnil | [{:some, binary}, ...], but the typespec tells it that it should be[] | [{:some, binary}, ...]. The overlap between both is just[{:some, binary}, ...]. That’s enough for dialyzer to be fine in regards tovalidate_name/1. Addingtest/0however adds a function, where the typespec and the success typeing have no possible overlap anymore. Therefore you get an error.The thing to understand here is that success typing is not all all concerned with exhaustive checks (at least with the default flags). It doesn’t care if your typespec has parts, which are off, for as long as there’s some “correct” parts to it. Correct meaning matching to the inferred type of dialyzer. So thinking your typespec is a 100% match just because dialyzer doesn’t complain is an incorrect expectation.
You can make dialyzer catch the issue by enabling the
:underspecs(there’s also:overspecs) flag. This will make dialyzer complain if parts of its inferred type is not covered by the typespec in code.Fl4m3Ph03n1x
So, if I understand correctly, as long as 1 path in my code leads to a successful execution that is compatible with the specs I provide, dialyzer will not complain.
There may be many paths that break, but as long as 1 works, dialzyer is happy.
In my specific case, because I have a branch that returns
[{:some, String.t}]dialyzer does not complain, because I have 1 branch that succeeds.Thanks!
I tried this and didn’t get the same result:
mix.exs
test.ex
dialyzer output
Given this example is slightly different (no more tuples inside the list) my understanding is that
underspecsshould still complain. I will however say that the only resource I could find on the matter was this mail:From it:
So, in my case, if I understand:
[] | [Any][Any]And in this case, my SpecOut >= RealOut. So
:underspecwill not complain ( it doesnt).I may very well be reading this all wrong. Keep that in mind.
I am just trying to understand why
underspecwont work in my sample.LostKobrakai
I guess this is your problem: An empty list is a valid value for both
some(t)andnothing.[t]means any list (even emtpy), but elements of the list need to be of typet. You likely want[t, ...], which means any non empty list, where elements are of typet.Fl4m3Ph03n1x
Still passes, even with
:underspec. Reverting back to the tuple{:some, t}didn’t make any difference.While the email specifies in mathematical detail what
underspecis supposed to do, I still cannot find a real life scenario where I would want to use it.I do think you are correct in the sense that
[] | [t, ...]is the issue. I don’t see how though, since[]means empty list, and[t, ....]means a list with at least 1 element.In my code,
nilis neither of those cases.The following code does raise a warning though:
Warning
But this is confusing. There is 1 path that leads to success. Dialyzer should not complain.
Yet, here it does because
nil !== 0.LostKobrakai
Seems like
:overspecsis the one you want, not:underspec(I tried both this morning and probably got them mixed up).With that flag I get this:
Fl4m3Ph03n1x
I see. Using
overspecsI fall into the clause:Where RealOut is
[] | [t] | niland SpecOut is[] | [t].Therefore, a contract violation is detected.
Now, the one final thing I don’t get is:
overspecflag in the first place?If having 1 successful run was all it took dialyzer to be happy, then the code from post:
Should not have thrown a warning, it should have passed.
LostKobrakai
Which flags was this run with?
Fl4m3Ph03n1x
Ignore me, it passes.
I had
overspecsenabled (by mistake).This truly does give me the impression the
elsebranch is being ignored by the default algorithm (with no flags), because I have case where it passes.To reiterate:
Makes a happy dialyzer if dialyzer has no flags.
Fl4m3Ph03n1x
Summary
After talking with many folks I have come to understand that as long as 1 path in my code leads to a successful execution that is compatible with the specs I provide, dialyzer will not complain.
There may be many paths that break, but as long as 1 works, dialzyer is happy.
In my specific case, because I have a branch that returns
[{:some, String.t}]dialyzer does not complain, because I have 1 branch that succeeds.This can be better summarized in the quote from Type Specifications and Eralng:
In fact, the article mentioned above, has an example case very similar to my own:
Which also does not trigger dialyzer. According to the article:
This is quite enlightening and I am convinced this is what is happening in my case.
Will dialyzer ever understand what is going on?
To be fair, this error can be caught by dialyzer if we use some flags, namelly
--overspecs(for this case) and its sister--underspecs(which we don’t need for this case).After some research I was able to find a mailing list that details the behaviour of these flags in a mathematical format:
From it:
And indeed if I run
mix dialyzer --overspecswith this sample, dialzyer does complain becasue:Where RealOut is
[] | [t] | niland SpecOut is[] | [t].Therefore, a contract violation is detected.
Error shown:
This was a wild ride through Dialyzer, and a revision I quite honestly needed. During the whole ordeal I learned about a couple of dialyzer flags and I refreshed my memory on success typing (which I absolutely needed to).
Thank you everyone for participating !
dimitarvp
Quite interesting. I’m definitely going to use the
--overspecsflag then because (a) it is more strict and (b) seems to give more readable error messages.Thanks for sharing!