Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically translate pure code into code that uses mutable arrays for efficiency?

This is a Haskell question, but I'd also be interested in answers about other languages. Is there a way to automatically translate purely functional code, written to process either lists or immutable arrays without doing any destructive updates, into code that uses mutable arrays for efficiency?

In Haskell the generated code would either run in the ST monad (in which case it would all be wrapped in runST or runSTArray) or in the IO monad, I assume.

I'm most interested in general solutions which work for any element type.

I thought I've seen this before, but I can't remember where. If it doesn't already exist, I'd be interested in creating it.

like image 277
Robin Green Avatar asked Oct 11 '22 23:10

Robin Green


1 Answers

Implementing a functional language using destructive updates is a memory management optimization. If an old value will no longer be used, it is safe to reuse the old memory to hold a new values. Detecting that a value will not be used anymore is a difficult problem, which is why reuse is still managed manually.

Linear type inference and uniqueness type inference discover some useful information. These analyses discover variables that hold the only reference to some object. After the last use of that variable, either the object is transferred somewhere else, or the object can be reused to hold a new value.

Several languages, including Sisal and SAC, attempt to reuse old array memory to hold new arrays. In SAC, programs are first converted to use explicit memory management (specifically, reference counting) and then the memory management code is optimized.

like image 109
Heatsink Avatar answered Oct 14 '22 04:10

Heatsink