Basic Type-Level Programming in Haskell
parsonsmatt.org
parsonsmatt.org
I highly recommend buying the book and checking out Idris if you found this article interesting. Read the free "overview" chapter[1] at least.
[0] https://www.manning.com/books/type-driven-development-with-i...
[1] https://manning-content.s3.amazonaws.com/download/a/580d6ba-...
Algol derived languages, while not as powerful as ML ones regarding the type system offer quite a few possibilities to ensure safer code via type definitions, ranges, enumerations and ADTs (Abstract Data Types not to confuse with Algebraic data types).
C++ also allows for this, but requires a bit of boilerplate with templates, classes and operator overloading, which is way more work than a few type declarations in Haskell or Idris.
all kinds of s can be elegantly described in a few lines of Haskell,
I think for Haskell specifically that http://haskellbook.com is the best.
Fundeps are pretty clear for some simple examples (e.g., a container’s element type is uniquely determined by the container type), but in general I find it easier to think in terms of type-level functions than dependencies between types.
data IntAndChar = MkIntAndChar Int Char
They belong to different namespaces, so you can use the same name: data IntAndChar = IntAndChar Int Char
Indeed, I believe this is preferred as it reduces the programmer's cognitive load. data IntAndChar = IntAndChar Int Char
would not compile with Main.IntAndChar already defined.Favorite quote:
> For some reason, functions that operate on types are called type families.
Of course you can just stick these checks in your code anyway..
x = loadstuff
if x.length == 10
etc.. but here you are relying on people putting that check in. All functions that have x passed into it then either have to either trust that the loading function made the length check or they have to check the length themselves.With a stronger type system, you can just put your assumptions into the type system and then if your assumptions are wrong or some code changes to break those assumptions, you will know straight away - even if it is 10 years down the line when junior comes along for his summer holiday work experience and needs to make a change some where...
These two presentations about dependent types in GHC are also good: https://www.youtube.com/watch?v=buVyfrU6QF4 https://www.youtube.com/watch?v=GgD0KUxMaQs