Re:Basic Type-Level Programming in Haskell
by skybrian on Friday April 28, 2017 @11:32AM
(#14220219)
Is there a possibility that length-encoded vectors will be practical someday, or is it just a useful toy example for tutorials?
[ Parent
| on HN ]
It depends on how many safety guarantees you want in your code. If you want to be absolutely sure at compile time that eg.. your sort function will not change the number of elements in your list, or you need to be sure that you have read in the expected number of records from a file otherwise you error, then this sort of stuff is practical right now.
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...
Re:Basic Type-Level Programming in Haskell
by danidiaz on Friday April 28, 2017 @01:22PM
(#14221299)
In Haskell? There are packages like 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 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=GgD0KUxMaQs
Re:Basic Type-Level Programming in Haskell
by skybrian on Friday April 28, 2017 @02:24PM
(#14221818)
Are people using these packages much in practice? Any pitfalls?
Re:Basic Type-Level Programming in Haskell
by danidiaz on Friday April 28, 2017 @03:11PM
(#14222197)
That I don't know.
Basic example: A lot of operations on vectors can be compiled into much more efficient code when the length of the vectors is known at compile time.