I want to have a language where I am able to specify low level aspects of the system (among other things), things like how much stack is allocated and how heap memory management works. I am a programming language dilettante, so I have much to learn.
However I also think functional languages are a good thing, and I don’t want to give up the simplicity of programming it affords. So I had the idea of making constructors and normal function scope syntactic sugar for functions. We need low level memory functions to do these tasks.
So we’d have special functions like
!unsafeAlloc :: Int -> Scope -> Block
!unsafeAddRef :: Block -> Scope -> ()
!unsafeDeref :: Block -> Scope -> ()
Which all programs would be expected to specify, although there will be system calls they can just assign them to if they want.
The ! meaning that garbage collection is not used on this function. Defining constructors would also create a non memory managed variant with the block of memory used for the construction as the last .
So assuming a Data like the archetypal Natural with an S constructor
h = S n
would be syntactic sugar for
h = !S n (!unsafeAlloc (size S) currentScope)
unsafeAlloc should add a reference to the currentScope as well.
Scopes would also be syntactic sugar. They would require setting up the scope object at the beginning of the function and mapping unsafeDeref over all the blocks when the scope was closed. Ordering would be done by means of the scope monad. So something along the lines of
do
currentScope <- newScope;
---- code here---
!fmap (unsafeDeref) currentScope;
destruct currentScope
In fact the memory manager should really be a monad to keep it pure, as all requests might not be granted. However making it all monadic code would restrict the ability to reorder functions for parallel code.
I’m sure that there are probably a few gotchas that I am not seeing and my thinking is still a bit muddled, but that is the core of the idea.
Are there any simple haskell/ML ish languages I could play around with to see how memory management works there?