ccoool
This commit is contained in:
@@ -59,8 +59,9 @@ import Data.Char
|
|||||||
import GHC.Generics
|
import GHC.Generics
|
||||||
-- Lift instances for the Core quasiquoters
|
-- Lift instances for the Core quasiquoters
|
||||||
import Language.Haskell.TH.Syntax (Lift)
|
import Language.Haskell.TH.Syntax (Lift)
|
||||||
import Lens.Micro.TH (makeLenses)
|
-- import Lens.Micro.TH (makeLenses)
|
||||||
import Lens.Micro
|
-- import Lens.Micro
|
||||||
|
import Control.Lens
|
||||||
----------------------------------------------------------------------------------
|
----------------------------------------------------------------------------------
|
||||||
|
|
||||||
data Expr b = Var Name
|
data Expr b = Var Name
|
||||||
@@ -152,6 +153,12 @@ makeLenses ''Program
|
|||||||
makeBaseFunctor ''Expr
|
makeBaseFunctor ''Expr
|
||||||
pure []
|
pure []
|
||||||
|
|
||||||
|
-- this is a weird optic, stronger than Lens and Prism, but weaker than Iso.
|
||||||
|
programTypeSigsP :: (Hashable b) => Prism' (Program b) (HashMap b Type)
|
||||||
|
programTypeSigsP = prism
|
||||||
|
(\b -> mempty & programTypeSigs .~ b)
|
||||||
|
(Right . view programTypeSigs)
|
||||||
|
|
||||||
type ExprF' = ExprF Name
|
type ExprF' = ExprF Name
|
||||||
|
|
||||||
type Program' = Program Name
|
type Program' = Program Name
|
||||||
|
|||||||
@@ -5,24 +5,47 @@ module Rlp2Core
|
|||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
import Control.Monad
|
import Control.Monad
|
||||||
import Control.Monad.Writer.CPS
|
import Control.Monad.Writer.CPS
|
||||||
|
import Control.Arrow
|
||||||
|
import Control.Applicative
|
||||||
import Lens.Micro
|
import Lens.Micro
|
||||||
import Lens.Micro.Internal
|
import Lens.Micro.Internal
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
import Data.Text qualified as T
|
import Data.Text qualified as T
|
||||||
import Data.HashMap.Strict qualified as H
|
import Data.HashMap.Strict qualified as H
|
||||||
|
import Data.Monoid (Endo(..))
|
||||||
|
import Data.Foldable
|
||||||
|
|
||||||
import Core.Syntax as Core
|
import Core.Syntax as Core
|
||||||
import Rlp.Syntax as Rlp
|
import Rlp.Syntax as Rlp
|
||||||
import Rlp.Parse.Types (RlpcPs)
|
import Rlp.Parse.Types (RlpcPs)
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
-- the rl' program is desugared by desugaring each declaration as a separate
|
||||||
|
-- program, and taking the monoidal product of the lot :3
|
||||||
|
|
||||||
rlpProgToCore :: RlpProgram RlpcPs -> Program'
|
rlpProgToCore :: RlpProgram RlpcPs -> Program'
|
||||||
rlpProgToCore = foldMapOf (progDecls . each) declToCore
|
rlpProgToCore = foldMapOf (progDecls . each) declToCore
|
||||||
|
|
||||||
declToCore :: Decl' RlpcPs -> Program'
|
declToCore :: Decl' RlpcPs -> Program'
|
||||||
|
|
||||||
declToCore (TySigD'' ns t) =
|
declToCore (TySigD'' ns t) = mempty &
|
||||||
mempty & programTypeSigs .~ H.fromList [ (n, typeToCore t) | n <- ns ]
|
programTypeSigs .~ H.fromList [ (n, typeToCore t) | n <- ns ]
|
||||||
|
|
||||||
|
declToCore (DataD'' n as ds) = fold . getZipList $
|
||||||
|
constructorToCore t' <$> ZipList [0..] <*> ZipList ds
|
||||||
|
where
|
||||||
|
-- create the appropriate type from the declared constructor and its
|
||||||
|
-- arguments
|
||||||
|
t' = foldl TyApp (TyCon n) (TyVar . dsNameToName <$> as)
|
||||||
|
|
||||||
|
declToCore (FunD'' n as e wh) = mempty &
|
||||||
|
|
||||||
|
constructorToCore :: Type -> Tag -> ConAlt RlpcPs -> Program'
|
||||||
|
constructorToCore t tag (ConAlt cn as) =
|
||||||
|
mempty & programTypeSigs . at cn ?~ foldr (:->) t as'
|
||||||
|
& programDataTags . at cn ?~ (tag, length as)
|
||||||
|
where
|
||||||
|
as' = typeToCore <$> as
|
||||||
|
|
||||||
typeToCore :: RlpType' RlpcPs -> Type
|
typeToCore :: RlpType' RlpcPs -> Type
|
||||||
typeToCore FunConT'' = TyFun
|
typeToCore FunConT'' = TyFun
|
||||||
|
|||||||
Reference in New Issue
Block a user