shitty temp frontend

This commit is contained in:
crumbtoo
2023-11-22 21:53:36 -07:00
parent 8195895233
commit ac6c0b7457
10 changed files with 198 additions and 20 deletions

View File

@@ -1,5 +1,75 @@
{-# LANGUAGE BlockArguments #-}
module Main where module Main where
---------------------------------------------------------------------------------- ----------------------------------------------------------------------------------
import Compiler.RLPC
import Options.Applicative hiding (ParseError)
import Control.Monad
import Control.Monad.Reader
import System.IO
import Core
import TIM
import Lens.Micro
import Lens.Micro.Mtl
----------------------------------------------------------------------------------
optParser :: ParserInfo RLPCOptions
optParser = info (helper <*> options)
( fullDesc
<> progDesc "Compile rl' programs"
<> header "rlpc - The Inglorious rl' Compiler"
)
options :: Parser RLPCOptions
options = RLPCOptions
<$> optional # strOption
( long "log"
<> short 'l'
<> metavar "FILE"
<> help "output dumps to FILE. stderr is used by default"
)
-- temp. i want gcc/ghc style options
<*> switch
( long "dump-evals"
<> short 'd'
<> help "dump evaluation logs"
)
<*> some (argument str (metavar "FILES..."))
where
infixr 9 #
f # x = f x
main :: IO () main :: IO ()
main = putStrLn "god i love you haskell i love you ghc i love you functionalprogramming researchers" main = do
opts <- execParser optParser
evalRLPCIO opts driver
pure ()
driver :: RLPCIO () ()
driver = sequence_
[ dumpEval
]
whenView :: (MonadReader s m) => Getting Bool s Bool -> m () -> m ()
whenView l m = view l >>= \a -> when a m
dumpEval :: RLPCIO () ()
dumpEval = whenView rlpcDumpEval do
fs <- view rlpcInputFiles
forM_ fs $ \f -> liftIO (readFile f) >>= doProg
where
doProg :: String -> RLPCIO () ()
doProg s = ask >>= \o -> case parseProg o s of
-- TODO: error handling
Left e -> error $ show e
Right (a,_) -> do
log <- view rlpcLogFile
case log of
Just f -> void . liftIO $ withFile f WriteMode $ hdbgProg a
Nothing -> void . liftIO $ hdbgProg a stderr
parseProg :: RLPCOptions
-> String
-> Either (SrcError ParseError) (Program, [SrcError ParseError])
parseProg o = evalRLPC o . (lexCore >=> parseCoreProg)

View File

@@ -158,7 +158,7 @@ evaluated as True (:code:`NData 1 []`).
& h & h
\begin{bmatrix} \begin{bmatrix}
f : \mathtt{NPrim} \; \mathtt{IfP} \\ f : \mathtt{NPrim} \; \mathtt{IfP} \\
c : \mathtt{NPrim} \; (\mathtt{NData} \; 1 \; []) \\ c : \mathtt{NPrim} \; (\mathtt{NData} \; 1 \; \nillist) \\
a_1 : \mathtt{NAp} \; f \; c \\ a_1 : \mathtt{NAp} \; f \; c \\
a_2 : \mathtt{NAp} \; a_1 \; x \\ a_2 : \mathtt{NAp} \; a_1 \; x \\
a_3 : \mathtt{NAp} \; a_2 \; y a_3 : \mathtt{NAp} \; a_2 \; y
@@ -181,7 +181,7 @@ evaluated as False (:code:`NData 0 []`).
& h & h
\begin{bmatrix} \begin{bmatrix}
f : \mathtt{NPrim} \; \mathtt{IfP} \\ f : \mathtt{NPrim} \; \mathtt{IfP} \\
c : \mathtt{NPrim} \; (\mathtt{NData} \; 0 \; []) \\ c : \mathtt{NPrim} \; (\mathtt{NData} \; 0 \; \nillist) \\
a_1 : \mathtt{NAp} \; f \; c \\ a_1 : \mathtt{NAp} \; f \; c \\
a_2 : \mathtt{NAp} \; a_1 \; x \\ a_2 : \mathtt{NAp} \; a_1 \; x \\
a_3 : \mathtt{NAp} \; a_2 \; y a_3 : \mathtt{NAp} \; a_2 \; y
@@ -237,3 +237,53 @@ Construct :code:`NData` out of a constructor and its arguments
\end{bmatrix} \end{bmatrix}
& g & g
} }
Pairs
-----
Evaluate the first argument if necessary
.. math::
\transrule
{ c : a_1 : a_2 : \nillist
& d
& h
\begin{bmatrix}
c : \mathtt{NPrim} \; \mathtt{CasePairP} \\
p : \mathtt{NAp} \; \_ \: \_ \\
a_1 : \mathtt{NAp} \; c \; p \\
a_2 : \mathtt{NAp} \; a_2 \; f
\end{bmatrix}
& g
}
{ p : \nillist
& (c : a_1 : a_2 : \nillist) : d
& h
& g
}
Perform the reduction if the first argument is in normal form
.. math::
\transrule
{ c : a_1 : a_2 : s
& d
& h
\begin{bmatrix}
c : \mathtt{NPrim} \; \mathtt{CasePairP} \\
p : \mathtt{NData} \; 0 \; [x,y] \\
a_1 : \mathtt{NAp} \; c \; p \\
a_2 : \mathtt{NAp} \; a_1 \; f
\end{bmatrix}
& g
}
{ a_2 : s
& d
& h
\begin{bmatrix}
a_1 : \mathtt{NAp} \; f \; x \\
a_2 : \mathtt{NAp} \; a_1 \; y
\end{bmatrix}
& g
}

View File

@@ -20,6 +20,8 @@ library
import: warnings import: warnings
exposed-modules: Core exposed-modules: Core
, TIM , TIM
, Compiler.RLPC
other-modules: Data.Heap other-modules: Data.Heap
, Data.Pretty , Data.Pretty
, Core.Syntax , Core.Syntax
@@ -27,7 +29,6 @@ library
, Core.TH , Core.TH
, Core.Examples , Core.Examples
, Core.Lex , Core.Lex
, Compiler.RLPC
, Control.Monad.Errorful , Control.Monad.Errorful
build-tool-depends: happy:happy, alex:alex build-tool-depends: happy:happy, alex:alex
@@ -41,6 +42,7 @@ library
, template-haskell , template-haskell
-- required for happy -- required for happy
, array , array
, data-default-class
hs-source-dirs: src hs-source-dirs: src
default-language: GHC2021 default-language: GHC2021
@@ -49,9 +51,12 @@ executable rlpc
main-is: Main.hs main-is: Main.hs
-- other-modules: -- other-modules:
-- other-extensions: -- other-extensions:
build-depends: build-depends: base ^>=4.18.0.0
base ^>=4.18.0.0 , rlp
, rlp , optparse-applicative
, microlens
, microlens-mtl
, mtl
hs-source-dirs: app hs-source-dirs: app
default-language: GHC2021 default-language: GHC2021

View File

@@ -1,21 +1,28 @@
{-# LANGUAGE GeneralisedNewtypeDeriving #-} {-# LANGUAGE GeneralisedNewtypeDeriving, StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TemplateHaskell #-}
module Compiler.RLPC module Compiler.RLPC
( RLPC(..) ( RLPC(..)
, RLPCIO
, RLPCOptions(RLPCOptions) , RLPCOptions(RLPCOptions)
, addFatal , addFatal
, addWound , addWound
, Severity(..) , Severity(..)
, SrcError(..) , SrcError(..)
, evalRLPCT , evalRLPCT
, evalRLPCIO
, evalRLPC , evalRLPC
, rlpcLogFile
, rlpcDumpEval
, rlpcInputFiles
) )
where where
---------------------------------------------------------------------------------- ----------------------------------------------------------------------------------
import Control.Arrow ((>>>)) import Control.Arrow ((>>>))
import Control.Monad.Reader import Control.Monad.Reader
import Control.Monad.Errorful import Control.Monad.Errorful
import Data.Functor.Identity import Data.Functor.Identity
import Data.Default.Class
import Data.Coerce import Data.Coerce
import Lens.Micro import Lens.Micro
import Lens.Micro.TH import Lens.Micro.TH
@@ -27,8 +34,12 @@ newtype RLPCT e m a = RLPC {
} }
deriving (Functor, Applicative, Monad, MonadReader RLPCOptions) deriving (Functor, Applicative, Monad, MonadReader RLPCOptions)
deriving instance (MonadIO m) => MonadIO (RLPCT e m)
type RLPC e = RLPCT e Identity type RLPC e = RLPCT e Identity
type RLPCIO e = RLPCT e IO
evalRLPCT :: RLPCOptions evalRLPCT :: RLPCOptions
-> RLPCT e m a -> RLPCT e m a
-> m (Either (SrcError e) (a, [SrcError e])) -> m (Either (SrcError e) (a, [SrcError e]))
@@ -39,7 +50,29 @@ evalRLPC :: RLPCOptions
-> Either (SrcError e) (a, [SrcError e]) -> Either (SrcError e) (a, [SrcError e])
evalRLPC o m = coerce $ evalRLPCT o m evalRLPC o m = coerce $ evalRLPCT o m
evalRLPCIO :: RLPCOptions
-> RLPCIO e a
-> IO (a, [SrcError e])
evalRLPCIO o m = do
m' <- evalRLPCT o m
case m' of
Left e -> error "need to impl io errors llol" -- TODO
Right a -> pure a
data RLPCOptions = RLPCOptions data RLPCOptions = RLPCOptions
{ _rlpcLogFile :: Maybe FilePath
, _rlpcDumpEval :: Bool
, _rlpcInputFiles :: [FilePath]
}
deriving Show
instance Default RLPCOptions where
def = RLPCOptions
{ _rlpcLogFile = Nothing
, _rlpcDumpEval = False
, _rlpcInputFiles = []
}
data SrcError e = SrcError data SrcError e = SrcError
{ _errSpan :: (Int, Int, Int) { _errSpan :: (Int, Int, Int)
@@ -60,7 +93,7 @@ type ErrorDoc = String
class Diagnostic e where class Diagnostic e where
errorDoc :: e -> ErrorDoc errorDoc :: e -> ErrorDoc
-- makeLenses ''RLPCOptions makeLenses ''RLPCOptions
makeLenses ''SrcError makeLenses ''SrcError
pure [] pure []

View File

@@ -1,3 +1,4 @@
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE FunctionalDependencies #-} {-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE TupleSections, PatternSynonyms #-} {-# LANGUAGE TupleSections, PatternSynonyms #-}
module Control.Monad.Errorful module Control.Monad.Errorful
@@ -40,6 +41,9 @@ instance (Applicative m) => MonadErrorful e (ErrorfulT e m) where
instance MonadTrans (ErrorfulT e) where instance MonadTrans (ErrorfulT e) where
lift m = ErrorfulT (Right . (,[]) <$> m) lift m = ErrorfulT (Right . (,[]) <$> m)
instance (MonadIO m) => MonadIO (ErrorfulT e m) where
liftIO = lift . liftIO
instance (Functor m) => Functor (ErrorfulT e m) where instance (Functor m) => Functor (ErrorfulT e m) where
fmap f (ErrorfulT m) = ErrorfulT $ fmap (_1 %~ f) <$> m fmap f (ErrorfulT m) = ErrorfulT $ fmap (_1 %~ f) <$> m

View File

@@ -1,7 +1,14 @@
module Core module Core
( module Core.Syntax ( module Core.Syntax
, parseCore
, parseCoreProg
, parseCoreExpr
, lexCore
, ParseError
) )
where where
---------------------------------------------------------------------------------- ----------------------------------------------------------------------------------
import Core.Syntax import Core.Syntax
import Core.Parse
import Core.Lex

View File

@@ -88,11 +88,11 @@ corePrelude = Module (Just ("Prelude", [])) $ Program
, ScDef "k" ["x", "y"] $ "x" , ScDef "k" ["x", "y"] $ "x"
, ScDef "k1" ["x", "y"] $ "y" , ScDef "k1" ["x", "y"] $ "y"
, ScDef "succ" ["f", "g", "x"] $ "f" :$ "x" :$ ("g" :$ "x") , ScDef "succ" ["f", "g", "x"] $ "f" :$ "x" :$ ("g" :$ "x")
, ScDef "compose" ["f", "g", "x"] "f" :$ ("g" :$ "x") , ScDef "compose" ["f", "g", "x"] $ "f" :$ ("g" :$ "x")
, ScDef "twice" ["f", "x"] $ "f" :$ ("f" :$ "x") , ScDef "twice" ["f", "x"] $ "f" :$ ("f" :$ "x")
, ScDef "False" [] $ Con 0 0 , ScDef "False" [] $ Con 0 0
, ScDef "True" [] $ Con 1 0 , ScDef "True" [] $ Con 1 0
, ScDef "MkPair" [] $ Con 1 2 , ScDef "MkPair" [] $ Con 0 2
, ScDef "fst" ["p"] $ "casePair#" :$ "p" :$ "k" , ScDef "fst" ["p"] $ "casePair#" :$ "p" :$ "k"
, ScDef "snd" ["p"] $ "casePair#" :$ "p" :$ "k1" , ScDef "snd" ["p"] $ "casePair#" :$ "p" :$ "k1"
] ]

View File

@@ -16,6 +16,7 @@ import Data.Foldable (foldl')
import Core.Syntax import Core.Syntax
import Core.Lex import Core.Lex
import Compiler.RLPC import Compiler.RLPC
import Data.Default.Class (def)
} }
%name parseCore Module %name parseCore Module
@@ -152,7 +153,7 @@ parseTmp = do
Left e -> error (show e) Left e -> error (show e)
Right (ts,_) -> pure ts Right (ts,_) -> pure ts
where where
parse = evalRLPC RLPCOptions . (lexCore >=> parseCore) parse = evalRLPC def . (lexCore >=> parseCore)
} }

View File

@@ -10,6 +10,7 @@ import Language.Haskell.TH.Syntax hiding (Module)
import Language.Haskell.TH.Quote import Language.Haskell.TH.Quote
import Control.Monad ((>=>)) import Control.Monad ((>=>))
import Compiler.RLPC import Compiler.RLPC
import Data.Default.Class (def)
import Core.Parse import Core.Parse
import Core.Lex import Core.Lex
---------------------------------------------------------------------------------- ----------------------------------------------------------------------------------
@@ -43,19 +44,19 @@ qCore s = case parse s of
Left e -> error (show e) Left e -> error (show e)
Right (m,ts) -> lift m Right (m,ts) -> lift m
where where
parse = evalRLPC RLPCOptions . (lexCore >=> parseCore) parse = evalRLPC def . (lexCore >=> parseCore)
qCoreExpr :: String -> Q Exp qCoreExpr :: String -> Q Exp
qCoreExpr s = case parseExpr s of qCoreExpr s = case parseExpr s of
Left e -> error (show e) Left e -> error (show e)
Right (m,ts) -> lift m Right (m,ts) -> lift m
where where
parseExpr = evalRLPC RLPCOptions . (lexCore >=> parseCoreExpr) parseExpr = evalRLPC def . (lexCore >=> parseCoreExpr)
qCoreProg :: String -> Q Exp qCoreProg :: String -> Q Exp
qCoreProg s = case parseProg s of qCoreProg s = case parseProg s of
Left e -> error (show e) Left e -> error (show e)
Right (m,ts) -> lift m Right (m,ts) -> lift m
where where
parseProg = evalRLPC RLPCOptions . (lexCore >=> parseCoreProg) parseProg = evalRLPC def . (lexCore >=> parseCoreProg)

View File

@@ -4,12 +4,9 @@
{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TemplateHaskell #-}
module TIM module TIM
( module Core.Examples ( module Core.Examples
, hdbgProg
) where ) where
---------------------------------------------------------------------------------- ----------------------------------------------------------------------------------
import Data.Map (Map, (!?), (!))
import Data.Map qualified as M
import Data.Set (Set)
import Data.Set qualified as S
import Data.Maybe (fromJust, fromMaybe) import Data.Maybe (fromJust, fromMaybe)
import Data.List (mapAccumL, intersperse) import Data.List (mapAccumL, intersperse)
import Control.Monad (guard) import Control.Monad (guard)
@@ -287,6 +284,16 @@ step st =
NData 0 [] -> False NData 0 [] -> False
NData 1 [] -> True NData 1 [] -> True
primStep _ CasePairP (TiState s d h g sts) =
case needsEval pn of
True -> TiState s' d' h g sts
where s' = undefined; d' = undefined
False -> TiState s' d h' g sts
where s' = undefined; h' = undefined
where
[p,f] = getArgs h s
pn = undefined
primStep n (ConP t a) (TiState s d h g sts) = primStep n (ConP t a) (TiState s d h g sts) =
TiState s' d h' g sts TiState s' d h' g sts
where where
@@ -465,7 +472,7 @@ instance Pretty TiState where
pnode (NPrim n _) _ = IStr n pnode (NPrim n _) _ = IStr n
pnode (NData t cs) p = "NData{" <> IStr (show t) <> "}" <> m pnode (NData t cs) p = "NData{" <> IStr (show t) <> "} " <> m
where where
m = cs m = cs
& fmap (\a -> pnode (hLookupUnsafe a h) (succ p)) & fmap (\a -> pnode (hLookupUnsafe a h) (succ p))