Typesafe Neural Networks in Haskell with Dependent Types
blog.jle.im
blog.jle.im
> what if we wanted to generate a network that has runtime-determined size (For example, getting the size from user input)? What if we wanted to load a pre-trained network whose size we don’t know? How can we manipulate our networks in a “dynamic” and generic way that still gives us all of the benefits of type-safe programming? We’ll found out next post!
https://hackage.haskell.org/package/base-4.7.0.2/docs/GHC-Ty...
There are type-level operators as well for arithmetic, but in my experience it’s a bit less pleasant than working with Peano Nats.
I can definitely do that with Singleton peano Nats pretty easily. I’ll end up with a function of type
SNat n -> Maybe (IsEven n)
Where IsEven n is some structural proof of even-ness data IsEven (n :: Nat) where
ZEven :: IsEven Z
SEven :: forall (n ::Nat). IsEven n -> IsEven (S (S n))
I wrote this on my phone, but I’m sure it’s possible and kinda easy to work with at that.I liked this article: https://ren.zone/articles/safe-money where they get e.g. an ExchangeRate "USD" "JPY" type from input that has a different type from ExchangeRate "EUR" "JPY" (and thus can't be used to convert euros, only US dollars).
However, I think it makes strange assumptions on its readership. For example:
> If you’re a non-Haskell programmer, this might all seem perfectly fine and normal, and you probably have only a slightly elevated heart rate. If you are a Haskell programmer, you are most likely already having heart attacks.
Well, for a non-Haskell programmer, the Haskell code snippets make very few sense, simply because they are not used to the syntax. And those non-Haskell programmers who are used to the ML-syntax are programming in SML, OCaml or F#, which all have a similar type system to Haskell's, and hence do get "elevated heart rate".
If this article is really meant to be readable by non-Haskell programmers, the example code snippets should also be provided in a more mainstream language with static types, such as C++, Java or perhaps JavaScript+TypeScript.
I didn't take that remark to be anything about the syntax, but rather how unsafe the code is, since we are dealing with matrices that have assumptions on their dimensions, essentially leaving the bugs for runtime — he follows this up with several bullet points explaining why the heart attack :)
Exactly that's the problem: The author is ignoring the syntax issue, that is, the fact that the vast majority of non-Haskell programmers won't be able to make much sense of the the code examples.
First of all, the definition of "readable by non-Haskell programmers" will always be somewhat subjective. Depending on their background, aptitude and motivation, many non-Haskell programmers can understand Haskell code just fine.
Setting that aside, the techniques described in this blog post simply wouldn't be possible to express in any of those languages. Just having static types is not sufficient. Even in vanilla Haskell, most type-level programming techniques are not possible to express, and certainly the type systems of Java etc are not capable of it (without significant extension).
really focusing on the meat of the article, eh?
Gloves off, boys and girls.