Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are "vocabulary types", and how many exist?

Across programming languages, I've encountered similar composite types with different names:

  1. Optional / Maybe
  2. Any
  3. Variant / Sum
  4. Record / Product

People often use the term vocabulary type,
yet I've never seen a definition of what makes a type "vocabulary".

Does this term have a loose definition?
What does type theory and other programming languages have to say about vocabulary types?

Is everything above a vocabulary type? Are there more?

like image 623
Trevor Hickey Avatar asked Jul 20 '16 02:07

Trevor Hickey


1 Answers

My understanding of vocabulary with respect to programming and vocabulary type in general is something that gives to certain objects properties with well defined meaning.

Here are some examples in Haskell:

Consider Optional / Maybe type and a pure function that takes HTTP reply of a web server and extracts reply's code.

getCode :: String -> Int

Now suppose by the time we run this function we don't know if response was successful or not - code might not be present at all. How do we represent case when code is missing? We can assign some artificial value -1 or 0 or we can change whole function type to this:

getCode :: String -> Maybe Int

In addition Maybe forms a Monad, Functor, Applicative, Foldable and a bunch of other typeclasses in Haskell. Each typeclass adds extra abilities to manipulate value in question while respecting it's presence/absence.

Product / Sum type in Haskell are represented as pairs and Either a b. Again - defining something via Product or Sum adds a well defined meaning Product a b - both values must be present, Sum a b - one value must be present and adds a bunch of laws for free.

like image 58
user8242965 Avatar answered Nov 05 '22 16:11

user8242965