This commit is contained in:
crumbtoo
2024-02-07 12:09:16 -07:00
parent 71170d6d42
commit 77d27dccde

View File

@@ -21,7 +21,7 @@ import Data.HashMap.Strict qualified as H
import Data.Monoid (Endo(..)) import Data.Monoid (Endo(..))
import Data.Foldable import Data.Foldable
import Data.Fix import Data.Fix
import Data.Maybe (fromJust) import Data.Maybe (fromJust, fromMaybe)
import Data.Functor.Bind import Data.Functor.Bind
import Debug.Trace import Debug.Trace
import Effectful.State.Static.Local import Effectful.State.Static.Local
@@ -73,19 +73,23 @@ declToCore (DataD'' n as ds) = fold . getZipList $
declToCore fd@(FunD'' n as e _) = mempty & programScDefs .~ [ScDef n' as' e''] declToCore fd@(FunD'' n as e _) = mempty & programScDefs .~ [ScDef n' as' e'']
where where
n' = dsNameToName n n' = dsNameToName n
(e',as') = mapAccumL caseify (extract e) (names `zip` as) -- e : verbatim function body
-- e' : function body w/ case-exprs matching pattern arguments
-- e'' : exprToCore e'
(e',as') = mapAccumL patArgsToCase (extract e) (names `zip` as)
e'' = runPureEff . runNameSupply n $ exprToCore e' e'' = runPureEff . runNameSupply n $ exprToCore e'
names = [ nolo $ "$x_" <> tshow n | n <- [0..] ] names = [ nolo $ "$x_" <> tshow n | n <- [0..] ]
tshow = T.pack . show tshow = T.pack . show
caseify :: RlpExpr RlpcPs -> (IdP' RlpcPs, Pat' RlpcPs) patArgsToCase :: RlpExpr RlpcPs -> (IdP' RlpcPs, Pat' RlpcPs)
-> (RlpExpr RlpcPs, Name) -> (RlpExpr RlpcPs, Name)
caseify e (x,p) = (e', x') where patArgsToCase e (x,p) = (e', x') where
x' = dsNameToName (extract x) x' = dsNameToName (extract x)
e' = CaseE (VarE <$> x) [(alt, [])] e' = CaseE (VarE <$> x) [(alt, [])]
alt = AltA p (nolo e) alt = AltA p (nolo e)
type NameSupply = Labeled "expr-name-supply" (State [IdP RlpcPs]) type NameSupply = Labeled NameSupplyLabel (State [IdP RlpcPs])
type NameSupplyLabel = "expr-name-supply"
exprToCore :: (NameSupply :> es) => RlpExpr RlpcPs -> Eff es Expr' exprToCore :: (NameSupply :> es) => RlpExpr RlpcPs -> Eff es Expr'
@@ -94,31 +98,22 @@ exprToCore (VarE n) = pure $ Var (dsNameToName n)
exprToCore (CaseE (unXRec -> e) as) = undefined exprToCore (CaseE (unXRec -> e) as) = undefined
-- TODO: where-binds -- TODO: where-binds
caseAltToCore :: (Alt RlpcPs, Where RlpcPs) -> Alter' caseAltToCore :: (NameSupply :> es)
caseAltToCore = undefined => (Alt RlpcPs, Where RlpcPs) -> Eff es Alter'
caseAltToCore (AltA (extract -> p) e, wh) = undefined
where
-- roseToCore :: Rose -> Expr' -> Alter' conToRose :: forall es. (NameSupply :> es) => Pat RlpcPs -> Eff es Rose
-- roseToCore (unFix -> Branch cn as) = alter
-- where
-- alter :: Alter'
-- alter = Alter (AltData cn) (treeToCore <$> as) (Var "expr")
-- -- foldFix :: Functor f => (f a -> a) -> Fix f -> a
-- treeToCore :: Tree Rose -> Expr' -> Expr'
-- treeToCore (Left n) = id
-- treeToCore (Right (n,cs)) = \e -> Case (Var n) [_]
conToRose :: forall es. (State [IdP RlpcPs] :> es) => Pat RlpcPs -> Eff es Rose
conToRose (ConP cn as) = Fix . Branch cn <$> patToForrest `traverse` as conToRose (ConP cn as) = Fix . Branch cn <$> patToForrest `traverse` as
where where
patToForrest :: Pat' RlpcPs -> Eff es (Tree Rose) patToForrest :: Pat' RlpcPs -> Eff es (Tree Rose)
patToForrest (VarP'' x) = pure $ Left (dsNameToName x) patToForrest (VarP'' x) = pure $ Left (dsNameToName x)
patToForrest p@(ConP'' _ _) = patToForrest p@(ConP'' _ _) =
Right <$> liftA2 (,) getName br Right <$> liftA2 (,) uniqueName br
where where
br = unwrapFix <$> conToRose (unXRec p) br = unwrapFix <$> conToRose (unXRec p)
getName = state $ fromJust . uncons @[IdP RlpcPs]
branchToCore :: Expr' -> Branch Alter' -> Alter' branchToCore :: Expr' -> Branch Alter' -> Alter'
branchToCore e (Branch cn as) = Alter (AltData cn) myBinds e' branchToCore e (Branch cn as) = Alter (AltData cn) myBinds e'
where where
@@ -132,7 +127,7 @@ branchToCore e (Branch cn as) = Alter (AltData cn) myBinds e'
e' = Case (Var $ dsNameToName n) [branchToCore e cs] e' = Case (Var $ dsNameToName n) [branchToCore e cs]
runNameSupply :: IdP RlpcPs -> Eff (NameSupply ': es) a -> Eff es a runNameSupply :: IdP RlpcPs -> Eff (NameSupply ': es) a -> Eff es a
runNameSupply n = runLabeled @"expr-name-supply" (evalState ns) where runNameSupply n = runLabeled @NameSupplyLabel (evalState ns) where
ns = [ "$" <> n <> "_" <> T.pack (show k) | k <- [0..] ] ns = [ "$" <> n <> "_" <> T.pack (show k) | k <- [0..] ]
-- | debug helper -- | debug helper
@@ -140,6 +135,13 @@ runNameSupply n = runLabeled @"expr-name-supply" (evalState ns) where
nameSupply :: [IdP RlpcPs] nameSupply :: [IdP RlpcPs]
nameSupply = [ T.pack $ "$x_" <> show n | n <- [0..] ] nameSupply = [ T.pack $ "$x_" <> show n | n <- [0..] ]
uniqueName :: (NameSupply :> es) => Eff es (IdP RlpcPs)
uniqueName = labeled @NameSupplyLabel @(State [IdP RlpcPs]) $
state @[IdP RlpcPs] (fromMaybe err . uncons)
where
err = error "NameSupply ran out of names! This shound never happen.\
\ The caller of runNameSupply is responsible."
constructorToCore :: Type -> Tag -> ConAlt RlpcPs -> Program' constructorToCore :: Type -> Tag -> ConAlt RlpcPs -> Program'
constructorToCore t tag (ConAlt cn as) = constructorToCore t tag (ConAlt cn as) =
mempty & programTypeSigs . at cn ?~ foldr (:->) t as' mempty & programTypeSigs . at cn ?~ foldr (:->) t as'