The Hazy Haskell Compiler

Are you sure you can free an STRef when you leave the runST? You can hide an STRef in an existential (if you have those) and smuggle it out. You can’t do anything very interesting with it, but it is reachable.

1 Like

I’m looking forward to running some code soon then.

1 Like

The only thing you can with an STRef without the the state token is compare them for equality and it shouldn’t be an issue to compare dead pointers for equality if they never move. It’s impossible to compare dead pointer to live ones too because the s parameters have to be the same in the Eq instance. I am a bit worried that LLVM or some other C optimizer would freak out if it saw dead pointer comparisons though.

Small update. I implemented putStrLn (and Text.pack, Text.putStrLn) so now this example works.

module Main where

type Peano :: *
data Peano = Zero | Succ Peano

length :: Peano -> String
length (Succ peano) = '+' : length peano
length Zero = ""

class Double a where
  double :: a -> a

instance Double Peano where
  double (Succ peano) = Succ (Succ (double peano))
  double Zero = Zero

quadruple :: (Double a) => a -> a
quadruple peano = double (double peano)

three, twelve :: Peano
three = Succ (Succ (Succ Zero))
twelve = quadruple three

main :: IO ()
main = putStrLn (length twelve)

Here’s is what the generated code looks like: Peano.js - Pastebin.com

Also, this is in already Readme, but let me reiterate the process of generating this.

$ cp -R runtime/ output
$ cabal run hazy -- -I library/runtime/ library/base -o output
Configuration is affected by the following files:
- cabal.project
$ cabal run hazy -- -I library/runtime/ -I library/base/ Peano.hs -o output
Configuration is affected by the following files:
- cabal.project
$ esbuild output/index.mjs --bundle --format=esm --platform=node --outfile=Peano.js

  Peano.js  4.6kb

Done in 13ms
$ node Peano.js
++++++++++++
$
7 Likes