rc #13
@@ -207,8 +207,6 @@ namedConsCase :: Program'
|
|||||||
namedConsCase = [coreProg|
|
namedConsCase = [coreProg|
|
||||||
{-# PackData Nil 0 0 #-}
|
{-# PackData Nil 0 0 #-}
|
||||||
{-# PackData Cons 1 2 #-}
|
{-# PackData Cons 1 2 #-}
|
||||||
Nil = Pack{0 0};
|
|
||||||
Cons = Pack{1 2};
|
|
||||||
foldr f z l = case l of
|
foldr f z l = case l of
|
||||||
{ Nil -> z
|
{ Nil -> z
|
||||||
; Cons x xs -> f x (foldr f z xs)
|
; Cons x xs -> f x (foldr f z xs)
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
{-# LANGUAGE ImplicitParams #-}
|
{-# LANGUAGE ImplicitParams #-}
|
||||||
{-# LANGUAGE LambdaCase #-}
|
|
||||||
module Core2Core
|
module Core2Core
|
||||||
( core2core
|
( core2core
|
||||||
, gmPrep
|
, gmPrep
|
||||||
@@ -15,13 +14,15 @@ import Data.Maybe (fromJust)
|
|||||||
import Data.Set (Set)
|
import Data.Set (Set)
|
||||||
import Data.Set qualified as S
|
import Data.Set qualified as S
|
||||||
import Data.List
|
import Data.List
|
||||||
|
import Data.Foldable
|
||||||
import Control.Monad.Writer
|
import Control.Monad.Writer
|
||||||
import Control.Monad.State.Lazy
|
import Control.Monad.State.Lazy
|
||||||
import Control.Arrow ((>>>))
|
import Control.Arrow ((>>>))
|
||||||
import Data.Text qualified as T
|
import Data.Text qualified as T
|
||||||
import Data.HashMap.Strict (HashMap)
|
import Data.HashMap.Strict (HashMap)
|
||||||
import Numeric (showHex)
|
import Numeric (showHex)
|
||||||
import Lens.Micro.Platform
|
-- import Lens.Micro.Platform
|
||||||
|
import Control.Lens
|
||||||
import Core.Syntax
|
import Core.Syntax
|
||||||
import Core.Utils
|
import Core.Utils
|
||||||
----------------------------------------------------------------------------------
|
----------------------------------------------------------------------------------
|
||||||
@@ -29,13 +30,28 @@ import Core.Utils
|
|||||||
core2core :: Program' -> Program'
|
core2core :: Program' -> Program'
|
||||||
core2core p = undefined
|
core2core p = undefined
|
||||||
|
|
||||||
|
-- | G-machine preprocessing.
|
||||||
|
|
||||||
gmPrep :: Program' -> Program'
|
gmPrep :: Program' -> Program'
|
||||||
gmPrep p = p & appFloater (floatNonStrictCases globals)
|
gmPrep p = p & appFloater (floatNonStrictCases globals)
|
||||||
& tagData
|
& tagData
|
||||||
|
& defineData
|
||||||
where
|
where
|
||||||
globals = p ^.. programScDefs . each . _lhs . _1
|
globals = p ^.. programScDefs . each . _lhs . _1
|
||||||
& S.fromList
|
& S.fromList
|
||||||
|
|
||||||
|
-- | Define concrete supercombinators for all datatags defined via pragmas (or
|
||||||
|
-- desugaring)
|
||||||
|
|
||||||
|
defineData :: Program' -> Program'
|
||||||
|
defineData p = p & programScDefs <>~ defs
|
||||||
|
where
|
||||||
|
-- defs = ifoldMap' _ (p ^. programDataTags)
|
||||||
|
defs = p ^. programDataTags
|
||||||
|
. to (ifoldMap (\k (t,a) -> [ScDef k [] (Con t a)]))
|
||||||
|
|
||||||
|
-- | Substitute all pattern matches on named constructors for matches on tags
|
||||||
|
|
||||||
tagData :: Program' -> Program'
|
tagData :: Program' -> Program'
|
||||||
tagData p = let ?dt = p ^. programDataTags
|
tagData p = let ?dt = p ^. programDataTags
|
||||||
in p & programRhss %~ cata go where
|
in p & programRhss %~ cata go where
|
||||||
@@ -59,6 +75,7 @@ appFloater fl p = p & traverseOf programRhss fl
|
|||||||
& runFloater
|
& runFloater
|
||||||
& \ (me,floats) -> me & programScDefs %~ (<>floats)
|
& \ (me,floats) -> me & programScDefs %~ (<>floats)
|
||||||
|
|
||||||
|
-- TODO: move NameSupply from Rlp2Core into a common module to share here
|
||||||
runFloater :: Floater a -> (a, [ScDef'])
|
runFloater :: Floater a -> (a, [ScDef'])
|
||||||
runFloater = flip evalStateT ns >>> runWriter
|
runFloater = flip evalStateT ns >>> runWriter
|
||||||
where
|
where
|
||||||
@@ -88,7 +105,7 @@ floatNonStrictCases g = goE
|
|||||||
altBodies = (\(Alter _ _ b) -> b) <$> as
|
altBodies = (\(Alter _ _ b) -> b) <$> as
|
||||||
tell [sc]
|
tell [sc]
|
||||||
goE e
|
goE e
|
||||||
traverse goE altBodies
|
traverse_ goE altBodies
|
||||||
pure e'
|
pure e'
|
||||||
goC (f :$ x) = (:$) <$> goC f <*> goC x
|
goC (f :$ x) = (:$) <$> goC f <*> goC x
|
||||||
goC (Let r bs e) = Let r <$> bs' <*> goE e
|
goC (Let r bs e) = Let r <$> bs' <*> goE e
|
||||||
@@ -97,7 +114,7 @@ floatNonStrictCases g = goE
|
|||||||
goC (Var k) = pure (Var k)
|
goC (Var k) = pure (Var k)
|
||||||
goC (Con t as) = pure (Con t as)
|
goC (Con t as) = pure (Con t as)
|
||||||
|
|
||||||
name = state (fromJust . uncons)
|
name = state (fromJust . Data.List.uncons)
|
||||||
|
|
||||||
-- extract the right-hand sides of a list of bindings, traverse each
|
-- extract the right-hand sides of a list of bindings, traverse each
|
||||||
-- one, and return the original list of bindings
|
-- one, and return the original list of bindings
|
||||||
@@ -105,6 +122,7 @@ floatNonStrictCases g = goE
|
|||||||
travBs c bs = bs ^.. each . _rhs
|
travBs c bs = bs ^.. each . _rhs
|
||||||
& traverse goC
|
& traverse goC
|
||||||
& const (pure bs)
|
& const (pure bs)
|
||||||
|
-- ^ ??? what the fuck?
|
||||||
|
|
||||||
-- when provided with a case expr, floatCase will float the case into a
|
-- when provided with a case expr, floatCase will float the case into a
|
||||||
-- supercombinator of its free variables. the sc is returned along with an
|
-- supercombinator of its free variables. the sc is returned along with an
|
||||||
|
|||||||
Reference in New Issue
Block a user