[erlang-questions] Emulating phantom types maybe

Richard A. O'Keefe <>
Fri Aug 26 03:21:15 CEST 2016


Can I suggest taking a more fundamental view?

The basic presupposition here is that we want to have
values that are the same as some standard value but
tagged in some way, rather like the way Ada could do

type Upper_String is new String;

which would create a completely distinct Upper_String
type that is structurally isomorphic to String and
inherits all its operations, but can't be *mixed* with
String.

I want to ask the question "IS a string that's all
upper case REALLY a string?"  In general, I don't
think that's going to be true: the concatenation of
two strings is a string, but the concatenation of
two strings representing timestamps is not a string
representing a timestamp; a substring of a string is
a string, but a substring of a string representing a
geographic location is a string representing a
geographic location; the reversal of a string is a
string, but the reversal of a string representing a
British postcode is not a string representing a
British postcode, and so on.

I'm also thinking that Erlang is not a typeful language
in the way that Haskell or even SML is, so that leaning
hard on the type system might not be the best way to go.

In short, I'm suggesting that something like
{upper,String} or {lower,String} or whatever with a
run-time check required to get at the underlying string
would not only be something the type checker can help
with but might be a Good Idea anyway.

More generally, I note that C checkers like SPlint and
Carillon let you devise your own annotations to extend
the underlying type system.  Long term, something like
that might be nice for the Dialyzer, but there'll be a
lot of other things more urgent.


More information about the erlang-questions mailing list