
Basic Type-Level Programming in Haskell - runeks
http://www.parsonsmatt.org/2017/04/26/basic_type_level_programming_in_haskell.html
======
lodi
I always thought this kind of thing was abstruse and impractical until I read
_Type-Driven Development with Idris_ [0], which made basic dependent type
examples--like those demonstrated in the posted article--completely
straightforward. Turns out that this kind of thing isn't actually that hard
with the right language and tooling.

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...](https://www.manning.com/books/type-driven-development-with-idris)

[1] [https://manning-
content.s3.amazonaws.com/download/a/580d6ba-...](https://manning-
content.s3.amazonaws.com/download/a/580d6ba-451f-43c2-98aa-304705c5dab5/SampleCh01.pdf)

~~~
pjmlp
This was one of the reasons I felled in love with Turbo Pascal back in the
day.

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.

~~~
lodi
Yes, this was exactly my feeling reading through the Idris book. In the same
way that a few lines of elegant Haskell can sometimes describe functionality
that takes all kinds of technically-works-but-only-the-author-understands-it
C++ template abuse, a few lines of straightforward Idris can sometimes replace
all kinds of Haskell "type family abuse".

all kinds of s can be elegantly described in a few lines of Haskell,

------
imh
What's the difference in motivation between doing things this way vs with
functional dependencies?

~~~
evincarofautumn
Type families are more general than fundeps, and can lead to simpler code with
fewer extensions—FunctionalDependencies requires MultiParamTypeClasses, and
often FlexibleInstances, FlexibleContexts, and UndecidableInstances.

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.

------
Xophmeister
I'm not an experienced Haskell programmer, but I've never seen someone use
different names for the type and the type constructor, as in:

    
    
        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.

~~~
weavie
This seems to be the convention in Idris where

    
    
      data IntAndChar = IntAndChar Int Char
    

would not compile with

    
    
      Main.IntAndChar already defined.

------
runeks
This article hit the sweet spot for me on so many levels. It's so easy to find
very advanced/specialized Haskell tutorials, as well as beginner tutorials,
but this stuff -- bridging the two -- is exactly what I was looking for.
Please keep these coming, dearest author.

Favorite quote:

> For some reason, functions that operate on types are called type families.

------
skybrian
Is there a possibility that length-encoded vectors will be practical someday,
or is it just a useful toy example for tutorials?

~~~
danidiaz
In Haskell? There are packages like
[http://hackage.haskell.org/package/vector-
sized](http://hackage.haskell.org/package/vector-sized) One might need a
typechecker plugin like ghc-typelits-natnormalise
[http://hackage.haskell.org/package/ghc-typelits-
natnormalise](http://hackage.haskell.org/package/ghc-typelits-natnormalise) to
help GHC reason about the lengths.

These two presentations about dependent types in GHC are also good:
[https://www.youtube.com/watch?v=buVyfrU6QF4](https://www.youtube.com/watch?v=buVyfrU6QF4)
[https://www.youtube.com/watch?v=GgD0KUxMaQs](https://www.youtube.com/watch?v=GgD0KUxMaQs)

~~~
skybrian
Are people using these packages much in practice? Any pitfalls?

~~~
danidiaz
That I don't know.

------
gokusaaaan
Damn, this is exactly what I was looking for, thanks mate!!!

