hoist
This commit is contained in:
+6
-3
@@ -58,14 +58,16 @@ library
|
|||||||
-- cabal-fmt: expand src
|
-- cabal-fmt: expand src
|
||||||
exposed-modules:
|
exposed-modules:
|
||||||
Gyehoek.CPS.Close
|
Gyehoek.CPS.Close
|
||||||
|
Gyehoek.CPS.Contify
|
||||||
Gyehoek.CPS.Convert
|
Gyehoek.CPS.Convert
|
||||||
Gyehoek.CPS.Eval
|
Gyehoek.CPS.Eval
|
||||||
|
Gyehoek.CPS.Hoist
|
||||||
Gyehoek.CPS.Stackify
|
Gyehoek.CPS.Stackify
|
||||||
Gyehoek.CPS.Syntax
|
Gyehoek.CPS.Syntax
|
||||||
Gyehoek.Driver
|
Gyehoek.Driver
|
||||||
Gyehoek.Language
|
|
||||||
Gyehoek.GenSym
|
Gyehoek.GenSym
|
||||||
Gyehoek.Jalmot
|
Gyehoek.Jalmot
|
||||||
|
Gyehoek.Language
|
||||||
Gyehoek.Lift1
|
Gyehoek.Lift1
|
||||||
Gyehoek.Options
|
Gyehoek.Options
|
||||||
Gyehoek.Prelude
|
Gyehoek.Prelude
|
||||||
@@ -100,6 +102,7 @@ library
|
|||||||
, hashable
|
, hashable
|
||||||
, invertible-grammar
|
, invertible-grammar
|
||||||
, lens
|
, lens
|
||||||
|
, lucid
|
||||||
, megaparsec
|
, megaparsec
|
||||||
, mtl
|
, mtl
|
||||||
, optparse-applicative
|
, optparse-applicative
|
||||||
@@ -107,6 +110,7 @@ library
|
|||||||
, pretty-simple
|
, pretty-simple
|
||||||
, prettyprinter
|
, prettyprinter
|
||||||
, prettyprinter-ansi-terminal
|
, prettyprinter-ansi-terminal
|
||||||
|
, prettyprinter-lucid
|
||||||
, process
|
, process
|
||||||
, recursion-schemes
|
, recursion-schemes
|
||||||
, scientific
|
, scientific
|
||||||
@@ -117,8 +121,6 @@ library
|
|||||||
, typed-process
|
, typed-process
|
||||||
, unordered-containers
|
, unordered-containers
|
||||||
, vector
|
, vector
|
||||||
, lucid
|
|
||||||
, prettyprinter-lucid
|
|
||||||
|
|
||||||
hs-source-dirs: src
|
hs-source-dirs: src
|
||||||
default-language: GHC2024
|
default-language: GHC2024
|
||||||
@@ -171,6 +173,7 @@ test-suite doctest
|
|||||||
build-depends:
|
build-depends:
|
||||||
, base
|
, base
|
||||||
, gyehoek
|
, gyehoek
|
||||||
|
|
||||||
default-extensions: CPP
|
default-extensions: CPP
|
||||||
main-is: doctest.hs
|
main-is: doctest.hs
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
module Gyehoek.CPS.Contify
|
||||||
|
(
|
||||||
|
) where
|
||||||
|
|
||||||
|
import Gyehoek.Prelude
|
||||||
|
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
module Gyehoek.CPS.Hoist
|
||||||
|
( hoistProgram
|
||||||
|
) where
|
||||||
|
|
||||||
|
import Gyehoek.CPS.Syntax
|
||||||
|
import Gyehoek.Prelude
|
||||||
|
import qualified Data.HashMap.Strict as H
|
||||||
|
import Effectful.Writer.Static.Local
|
||||||
|
import Data.Foldable
|
||||||
|
|
||||||
|
|
||||||
|
type Hoist = Writer (HashMap Name Abs)
|
||||||
|
|
||||||
|
hoist :: Hoist :> es => Exp -> Eff es Exp
|
||||||
|
hoist = transformM \case
|
||||||
|
ExpLetRec bs m -> do
|
||||||
|
traverse_ (\(k,v) -> tell $ H.singleton k v) bs
|
||||||
|
pure m
|
||||||
|
e -> pure e
|
||||||
|
|
||||||
|
hoistProgram :: Program -> Eff es HoistedProgram
|
||||||
|
hoistProgram p = do
|
||||||
|
(body,bindings) <- runWriter $ traverseOf #body hoist p.body
|
||||||
|
pure $ MkHoistedProgram {body,bindings}
|
||||||
@@ -15,6 +15,7 @@ module Gyehoek.CPS.Syntax
|
|||||||
, Name(..)
|
, Name(..)
|
||||||
, Prim(..)
|
, Prim(..)
|
||||||
, Program(..)
|
, Program(..)
|
||||||
|
, HoistedProgram(..)
|
||||||
, Lit(..)
|
, Lit(..)
|
||||||
, Imm(..)
|
, Imm(..)
|
||||||
, Obj(..)
|
, Obj(..)
|
||||||
@@ -58,6 +59,7 @@ import qualified Data.InvertibleGrammar.Base as IG
|
|||||||
import Gyehoek.GenSym (Gen)
|
import Gyehoek.GenSym (Gen)
|
||||||
import Data.String (IsString)
|
import Data.String (IsString)
|
||||||
import Control.Applicative
|
import Control.Applicative
|
||||||
|
import qualified Data.HashMap.Strict as H
|
||||||
|
|
||||||
-- Data types
|
-- Data types
|
||||||
|
|
||||||
@@ -135,6 +137,7 @@ data Exp
|
|||||||
|
|
||||||
data Kexp
|
data Kexp
|
||||||
= KexpVar Name
|
= KexpVar Name
|
||||||
|
-- | Only to be used after contification.
|
||||||
| KexpKappa Kappa
|
| KexpKappa Kappa
|
||||||
deriving (Show, Generic, Data, Eq)
|
deriving (Show, Generic, Data, Eq)
|
||||||
|
|
||||||
@@ -152,6 +155,22 @@ data Program = MkProgram
|
|||||||
}
|
}
|
||||||
deriving (Show, Generic, Data)
|
deriving (Show, Generic, Data)
|
||||||
|
|
||||||
|
data HoistedProgram = MkHoistedProgram
|
||||||
|
{ bindings :: HashMap Name Abs
|
||||||
|
, body :: Lambda
|
||||||
|
}
|
||||||
|
deriving stock (Show, Generic, Data)
|
||||||
|
|
||||||
|
type instance Index HoistedProgram = Name
|
||||||
|
type instance IxValue HoistedProgram = Abs
|
||||||
|
|
||||||
|
instance Ixed HoistedProgram where ix j = #bindings . ix j
|
||||||
|
|
||||||
|
instance At HoistedProgram where at j = #bindings . at j
|
||||||
|
|
||||||
|
instance Each HoistedProgram HoistedProgram Abs Abs where
|
||||||
|
each = #bindings . each
|
||||||
|
|
||||||
makePrisms ''Kappa
|
makePrisms ''Kappa
|
||||||
makePrisms ''Exp
|
makePrisms ''Exp
|
||||||
makeFieldsId ''Exp
|
makeFieldsId ''Exp
|
||||||
@@ -307,6 +326,13 @@ instance S.DatumIso Kexp where
|
|||||||
instance S.DatumIso Program where
|
instance S.DatumIso Program where
|
||||||
datumIso = S.with \prog -> S.datumIso @Lambda >>> prog
|
datumIso = S.with \prog -> S.datumIso @Lambda >>> prog
|
||||||
|
|
||||||
|
instance S.DatumIso HoistedProgram where
|
||||||
|
datumIso = S.with \prog ->
|
||||||
|
S.letLike "letrec"
|
||||||
|
(S.datumIso @Name) (S.datumIso @Abs) (S.datumIso @Lambda)
|
||||||
|
>>> S.onTail (S.iso H.fromList H.toList)
|
||||||
|
>>> prog
|
||||||
|
|
||||||
|
|
||||||
-- quasiquoters
|
-- quasiquoters
|
||||||
|
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ import Control.Arrow ((>>>))
|
|||||||
import Gyehoek.Prelude
|
import Gyehoek.Prelude
|
||||||
import Gyehoek.Jalmot
|
import Gyehoek.Jalmot
|
||||||
import qualified Gyehoek.Sexp as S
|
import qualified Gyehoek.Sexp as S
|
||||||
|
import Gyehoek.CPS.Hoist (hoistProgram)
|
||||||
|
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
@@ -122,6 +123,9 @@ driver opts = do
|
|||||||
closedCps <- closeProgram cps
|
closedCps <- closeProgram cps
|
||||||
when opts.dumpClosed do
|
when opts.dumpClosed do
|
||||||
hPutStrLn FS.stdout =<< S.encodeWith S.datumIso closedCps
|
hPutStrLn FS.stdout =<< S.encodeWith S.datumIso closedCps
|
||||||
|
hoistedCps <- hoistProgram closedCps
|
||||||
|
when opts.dumpHoisted do
|
||||||
|
hPutStrLn FS.stdout =<< S.encodeWith S.datumIso hoistedCps
|
||||||
let rt_is p = is (_Just . p) opts.runtime
|
let rt_is p = is (_Just . p) opts.runtime
|
||||||
dumpOrRun opts.dumpStackified (rt_is #Stackify)
|
dumpOrRun opts.dumpStackified (rt_is #Stackify)
|
||||||
(stackifyProgram closedCps)
|
(stackifyProgram closedCps)
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ data Options = MkOptions
|
|||||||
, dumpCPS :: Bool
|
, dumpCPS :: Bool
|
||||||
, dumpParsed :: Bool
|
, dumpParsed :: Bool
|
||||||
, dumpStackified :: Bool
|
, dumpStackified :: Bool
|
||||||
|
, dumpHoisted :: Bool
|
||||||
, traceStackified :: Bool
|
, traceStackified :: Bool
|
||||||
, runtime :: Maybe Runtime
|
, runtime :: Maybe Runtime
|
||||||
, inspectWasm :: Bool
|
, inspectWasm :: Bool
|
||||||
@@ -61,6 +62,7 @@ parser = do
|
|||||||
dumpCPS <- switch (long "dump-cps")
|
dumpCPS <- switch (long "dump-cps")
|
||||||
dumpStackified <- switch (long "dump-stackified")
|
dumpStackified <- switch (long "dump-stackified")
|
||||||
dumpParsed <- switch (long "dump-parsed")
|
dumpParsed <- switch (long "dump-parsed")
|
||||||
|
dumpHoisted <- switch (long "dump-hoisted")
|
||||||
traceStackified <- switch (long "trace-stackified")
|
traceStackified <- switch (long "trace-stackified")
|
||||||
inspectWasm <- switch $ long "inspect-wasm" <> short 'p'
|
inspectWasm <- switch $ long "inspect-wasm" <> short 'p'
|
||||||
runtime <- option runtimeReader . fold $
|
runtime <- option runtimeReader . fold $
|
||||||
|
|||||||
Reference in New Issue
Block a user