3 ms·
> I don't know how rich Haskell's support for Value vs Reference types and memory layout are but this is something you can't just wave a compiler at since it's
by tathougies 8y ago
> I don't know how rich Haskell's support for Value vs Reference types and memory layout are but this is something you can't just wave a compiler at since it's highly dependent on your dataset and use cases.
Haskell has both value types (unboxed types) and reference types. By default, lazy reference types are the default. If you add a '!' before the types of fields in a record, you get direct pointers. If you add a '! {-# UNPACK -#}' or enable the '-funbox-strict-fields' option, you get directly embedded value types.
That being said, C has more first-class support for low-level manipulation, but if you wanted to, you could just add this kind of manipulation onto Haskell as a library. In my last company, we developed a library that used higher-kinded polymorphism and phantom types to basically make low-level memory manipulation a 'first class' value that could be reasoned about using Haskell's type system.
However, this required that you annotate your types appropriately. Nevertheless, code would end up compiling into highly efficient direct memory and register manipulation, while still having all of Haskell's typical type safety.
- vvanders 8y agoInteresting, thanks for the detailed breakdown. How's support for directly mapping chunks of memory to packed structures? We used to do a lot of in-place loading in games(yay for spinning, physical disks) so it being able to do blind casts was almost as important as value types themselves.
- tathougies 8y agoHaskell allows unsafe pointer arithmetic and manipulation (of course, the functions are all named 'unsafe'!). But, you can write safe interfaces on top of it. I have an example of an external sort using the standard fibonacci merge algorithm, which uses raw pointers and memory mapped IO under the hood, but which exposes a safe API. I've done some basic benchmarking, and it's about as fast as Postgres's sort on my machine, for simple binary doubles and ints. Here it is: https://github.com/tathougies/extsort/blob/master/Data/External/Sort.hs https://github.com/tathougies/extsort/blob/master/Data/Exter... . I think it's a good example of 'low-level' Haskell.
- nickpsecurity 8y agoIf looking into low-level Haskell, you might find House interesting given they did an OS in it: http://programatica.cs.pdx.edu/House/ http://programatica.cs.pdx.edu/House/ Although old, there's probably some useful concepts in there for balancing low-level needs against what Haskell gives you. I figure recent work in Haskell, Rust, and Idris might even be combined with their techniques for better results. Under HASP project, were designing a version of Haskell for systems programming called Habit, but one or more got poached into better project. Here's that language: http://hasp.cs.pdx.edu/habit-report-Nov2010.pdf http://hasp.cs.pdx.edu/habit-report-Nov2010.pdf