typed coreExpr quoter

This commit is contained in:
crumbtoo
2024-02-09 18:31:37 -07:00
parent 58838b9527
commit 17d764c2ec
4 changed files with 29 additions and 13 deletions

View File

@@ -1,11 +1,13 @@
module Control.Monad.Utils module Control.Monad.Utils
( mapAccumLM ( mapAccumLM
, Kendo(..) , Kendo(..)
, generalise
) )
where where
---------------------------------------------------------------------------------- ----------------------------------------------------------------------------------
import Data.Tuple (swap) import Data.Tuple (swap)
import Data.Coerce import Data.Coerce
import Data.Functor.Identity
import Control.Monad.State import Control.Monad.State
import Control.Monad import Control.Monad
---------------------------------------------------------------------------------- ----------------------------------------------------------------------------------
@@ -30,3 +32,6 @@ instance (Monad m) => Semigroup (Kendo m a) where
instance (Monad m) => Monoid (Kendo m a) where instance (Monad m) => Monoid (Kendo m a) where
mempty = Kendo pure mempty = Kendo pure
generalise :: (Monad m) => Identity a -> m a
generalise (Identity a) = pure a

View File

@@ -10,6 +10,7 @@ module Core.HindleyMilner
, check , check
, checkCoreProg , checkCoreProg
, checkCoreProgR , checkCoreProgR
, checkCoreExprR
, TypeError(..) , TypeError(..)
, HMError , HMError
) )
@@ -30,7 +31,7 @@ import Compiler.RlpcError
import Control.Monad (foldM, void, forM) import Control.Monad (foldM, void, forM)
import Control.Monad.Errorful import Control.Monad.Errorful
import Control.Monad.State import Control.Monad.State
import Control.Monad.Utils (mapAccumLM) import Control.Monad.Utils (mapAccumLM, generalise)
import Text.Printf import Text.Printf
import Core.Syntax import Core.Syntax
---------------------------------------------------------------------------------- ----------------------------------------------------------------------------------
@@ -113,8 +114,11 @@ checkCoreProgR p = (hoistRlpcT generalise . liftE . checkCoreProg $ p)
where where
liftE = liftErrorful . mapErrorful (errorMsg (SrcSpan 0 0 0 0)) liftE = liftErrorful . mapErrorful (errorMsg (SrcSpan 0 0 0 0))
generalise :: forall a. Identity a -> m a checkCoreExprR :: (Monad m) => Context' -> Expr' -> RLPCT m Expr'
generalise (Identity a) = pure a checkCoreExprR g e = (hoistRlpcT generalise . liftE . infer g $ e)
$> e
where
liftE = liftErrorful . mapErrorful (errorMsg (SrcSpan 0 0 0 0))
-- | Infer the type of an expression under some context. -- | Infer the type of an expression under some context.
-- --

View File

@@ -7,6 +7,7 @@ Description : Parser for the Core language
module Core.Parse module Core.Parse
( parseCore ( parseCore
, parseCoreExpr , parseCoreExpr
, parseCoreExprR
, parseCoreProg , parseCoreProg
, parseCoreProgR , parseCoreProgR
, module Core.Lex -- temp convenience , module Core.Lex -- temp convenience
@@ -16,6 +17,7 @@ module Core.Parse
where where
import Control.Monad ((>=>)) import Control.Monad ((>=>))
import Control.Monad.Utils (generalise)
import Data.Foldable (foldl') import Data.Foldable (foldl')
import Data.Functor.Identity import Data.Functor.Identity
import Core.Syntax import Core.Syntax
@@ -226,12 +228,12 @@ insScDef sc = programScDefs %~ (sc:)
singletonScDef :: (Hashable b) => ScDef b -> Program b singletonScDef :: (Hashable b) => ScDef b -> Program b
singletonScDef sc = insScDef sc mempty singletonScDef sc = insScDef sc mempty
parseCoreExprR :: (Monad m) => [Located CoreToken] -> RLPCT m Expr'
parseCoreExprR = hoistRlpcT generalise . parseCoreExpr
parseCoreProgR :: forall m. (Monad m) => [Located CoreToken] -> RLPCT m Program' parseCoreProgR :: forall m. (Monad m) => [Located CoreToken] -> RLPCT m Program'
parseCoreProgR = ddumpast <=< (hoistRlpcT generalise . parseCoreProg) parseCoreProgR = ddumpast <=< (hoistRlpcT generalise . parseCoreProg)
where where
generalise :: forall a. Identity a -> m a
generalise (Identity a) = pure a
ddumpast :: Program' -> RLPCT m Program' ddumpast :: Program' -> RLPCT m Program'
ddumpast p = do ddumpast p = do
addDebugMsg "dump-parsed-core" . show $ p addDebugMsg "dump-parsed-core" . show $ p

View File

@@ -5,6 +5,7 @@ Description : Core quasiquoters
module Core.TH module Core.TH
( coreExpr ( coreExpr
, coreProg , coreProg
, coreExprT
, coreProgT , coreProgT
) )
where where
@@ -22,20 +23,26 @@ import Data.Text qualified as T
import Core.Parse import Core.Parse
import Core.Lex import Core.Lex
import Core.Syntax import Core.Syntax
import Core.HindleyMilner (checkCoreProgR) import Core.HindleyMilner (checkCoreProgR, checkCoreExprR)
---------------------------------------------------------------------------------- ----------------------------------------------------------------------------------
coreProg :: QuasiQuoter coreProg :: QuasiQuoter
coreProg = mkqq $ lexCoreR >=> parseCoreProgR coreProg = mkqq $ lexCoreR >=> parseCoreProgR
coreExpr :: QuasiQuoter coreExpr :: QuasiQuoter
coreExpr = mkqq $ lexCoreR >=> parseCoreExpr coreExpr = mkqq $ lexCoreR >=> parseCoreExprR
-- | Type-checked @coreProg@ -- | Type-checked @coreProg@
coreProgT :: QuasiQuoter coreProgT :: QuasiQuoter
coreProgT = mkqq $ lexCoreR >=> parseCoreProgR >=> checkCoreProgR coreProgT = mkqq $ lexCoreR >=> parseCoreProgR >=> checkCoreProgR
mkqq :: (Lift a) => (Text -> RLPC a) -> QuasiQuoter coreExprT :: QuasiQuoter
coreExprT = mkqq $ lexCoreR >=> parseCoreExprR >=> checkCoreExprR g
where
g = [ ("+#", TyCon "Int#" :-> TyCon "Int#" :-> TyCon "Int#")
]
mkqq :: (Lift a) => (Text -> RLPCIO a) -> QuasiQuoter
mkqq p = QuasiQuoter mkqq p = QuasiQuoter
{ quoteExp = mkq p { quoteExp = mkq p
, quotePat = error "core quasiquotes may only be used in expressions" , quotePat = error "core quasiquotes may only be used in expressions"
@@ -43,8 +50,6 @@ mkqq p = QuasiQuoter
, quoteDec = error "core quasiquotes may only be used in expressions" , quoteDec = error "core quasiquotes may only be used in expressions"
} }
mkq :: (Lift a) => (Text -> RLPC a) -> String -> Q Exp mkq :: (Lift a) => (Text -> RLPCIO a) -> String -> Q Exp
mkq parse s = case evalRLPC def (parse $ T.pack s) of mkq parse s = liftIO $ evalRLPCIO def (parse $ T.pack s) >>= lift
(Just a, _) -> lift a
(Nothing, _) -> error "todo: aaahhbbhjhbdjhabsjh"