Skip to content
Snippets Groups Projects
Commit 1b00c98f authored by chrg's avatar chrg
Browse files

Add overloading example

parent eacab4ca
No related branches found
No related tags found
No related merge requests found
...@@ -40,6 +40,7 @@ module Control.Monad.Reduce ( ...@@ -40,6 +40,7 @@ module Control.Monad.Reduce (
import Control.Applicative import Control.Applicative
import Control.Monad import Control.Monad
import Control.Monad.State
import Control.Monad.Trans.Maybe import Control.Monad.Trans.Maybe
import Data.Bool import Data.Bool
import qualified Data.List.NonEmpty as NE import qualified Data.List.NonEmpty as NE
...@@ -133,7 +134,9 @@ onBoth mlhs mrhs fn = ...@@ -133,7 +134,9 @@ onBoth mlhs mrhs fn =
Nothing -> pure lhs Nothing -> pure lhs
Just rhs -> fn lhs rhs Just rhs -> fn lhs rhs
-- <|> fmap pure mrhs) <|> fmap pure mlhs instance (MonadReduce m) => MonadReduce (StateT s m) where
check = StateT (\s -> split (pure (False, s)) (pure (True, s)))
{-# INLINE check #-}
{- | A reduction path, can be used as an index into reduction tree. {- | A reduction path, can be used as an index into reduction tree.
Is isomorphic to a list of choices. Is isomorphic to a list of choices.
......
┳━┳━┳━┳━┳━┳━ x := 1; x := 2; x + x
┃ ┃ ┃ ┃ ┃ ┗━ x := 1; x := 2; x
┃ ┃ ┃ ┃ ┗━┳━ x := 1; x := 2; x
┃ ┃ ┃ ┃ ┗━ ⊥
┃ ┃ ┃ ┗━┳━┳━┳━ x := 1; 2 + 2
┃ ┃ ┃ ┃ ┃ ┗━ x := 1; 4
┃ ┃ ┃ ┃ ┗━ x := 1; 2
┃ ┃ ┃ ┗━┳━ x := 1; 2
┃ ┃ ┃ ┗━ ⊥
┃ ┃ ┗━ ⊥
┃ ┗━┳━┳━┳━┳━ x := 2; x + x
┃ ┃ ┃ ┃ ┗━ x := 2; x
┃ ┃ ┃ ┗━┳━ x := 2; x
┃ ┃ ┃ ┗━ ⊥
┃ ┃ ┗━┳━┳━┳━ 2 + 2
┃ ┃ ┃ ┃ ┗━ 4
┃ ┃ ┃ ┗━ 2
┃ ┃ ┗━┳━ 2
┃ ┃ ┗━ ⊥
┃ ┗━ ⊥
┗━ ⊥
...@@ -28,10 +28,10 @@ rList :: (MonadReduce m) => [a] -> m [a] ...@@ -28,10 +28,10 @@ rList :: (MonadReduce m) => [a] -> m [a]
rList = collect (given $>) rList = collect (given $>)
data Expr data Expr
= Var String = Var !String
| Cnt Int | Cnt !Int
| Opr Expr Expr | Opr !Expr !Expr
| Let String Expr Expr | Let !String !Expr !Expr
deriving (Show, Eq) deriving (Show, Eq)
rExpr rExpr
...@@ -49,7 +49,7 @@ rExpr e = case e of ...@@ -49,7 +49,7 @@ rExpr e = case e of
Opr e1 e2 -> onBoth (rExpr e1) (rExpr e2) \e1' e2' -> Opr e1 e2 -> onBoth (rExpr e1) (rExpr e2) \e1' e2' ->
case (e1', e2') of case (e1', e2') of
(Cnt a, Cnt b) -> pure (Cnt (a + b)) <| pure (Opr e1' e2') (Cnt a, Cnt b) -> pure (Cnt (a + b)) <| pure (Opr e1' e2')
_ -> pure $ Opr e1' e2' _ow -> pure $ Opr e1' e2'
Let k e1 e2 -> do Let k e1 e2 -> do
e1' <- rExpr e1 e1' <- rExpr e1
split split
...@@ -68,18 +68,18 @@ prettyExprS :: Int -> Expr -> String -> String ...@@ -68,18 +68,18 @@ prettyExprS :: Int -> Expr -> String -> String
prettyExprS d = \case prettyExprS d = \case
Var x -> showString x Var x -> showString x
Opr l r -> Opr l r ->
showParen (d > addPrec) showParen (d > addPrec) $
$ prettyExprS (addPrec + 1) l prettyExprS (addPrec + 1) l
. showString " + " . showString " + "
. prettyExprS (addPrec + 1) r . prettyExprS (addPrec + 1) r
Cnt i -> shows i Cnt i -> shows i
Let x e1 e2 -> Let x e1 e2 ->
showParen (d > letPrec) showParen (d > letPrec) $
$ showString x showString x
. showString " := " . showString " := "
. prettyExprS (letPrec + 1) e1 . prettyExprS (letPrec + 1) e1
. showString "; " . showString "; "
. prettyExprS letPrec e2 . prettyExprS letPrec e2
where where
addPrec = 2 addPrec = 2
letPrec = 1 letPrec = 1
...@@ -88,20 +88,6 @@ prettyExprWithConfig :: (Maybe Expr, Map.Map String (Either String Expr)) -> Str ...@@ -88,20 +88,6 @@ prettyExprWithConfig :: (Maybe Expr, Map.Map String (Either String Expr)) -> Str
prettyExprWithConfig (e, _) = prettyExprWithConfig (e, _) =
maybe "⊥" (flip (prettyExprS 0) "") e maybe "⊥" (flip (prettyExprS 0) "") e
-- <> "\n("
-- <> L.intercalate
-- ", "
-- ( map
-- ( \(k, v) ->
-- showString k
-- . showString " = "
-- . either showString (prettyExprS 0) v
-- $ ""
-- )
-- (Map.toList m)
-- )
-- <> ")"
spec :: Spec spec :: Spec
spec = do spec = do
rtreeSpec rtreeSpec
...@@ -120,21 +106,24 @@ rtreeTSpec = describe "RTreeT" do ...@@ -120,21 +106,24 @@ rtreeTSpec = describe "RTreeT" do
let handle str e = describe str do let handle str e = describe str do
let me = runMaybeT $ rExpr e let me = runMaybeT $ rExpr e
it "should extract" do it "should extract" do
evalState (extractT me) Map.empty extract (runStateT me Map.empty)
`shouldBe` Just e `shouldBe` (Just e, Map.empty)
it "should draw the same" do it "should draw the same" do
golden golden
("test/expected/" <> str) ("test/expected/" <> str)
(drawRTree prettyExprWithConfig (flattenT (unStateT me Map.empty))) (drawRTree prettyExprWithConfig (runStateT me Map.empty))
handle "small-opr-expr" $
Opr (Cnt 1) (Cnt 2)
handle "small-opr-expr" handle "small-let-expr" $
$ Opr (Cnt 1) (Cnt 2) Let "x" (Cnt 1) (Opr (Cnt 2) (Var "x"))
handle "small-let-expr" handle "double-let-expr" $
$ Let "x" (Cnt 1) (Opr (Cnt 2) (Var "x")) Let "x" (Cnt 1) (Let "y" (Cnt 2) (Opr (Var "x") (Var "y")))
handle "double-let-expr" handle "double-overloading-let-expr" $
$ Let "x" (Cnt 1) (Let "y" (Cnt 2) (Opr (Var "x") (Var "y"))) Let "x" (Cnt 1) (Let "x" (Cnt 2) (Opr (Var "x") (Var "x")))
equiv equiv
:: (Show b, MonadReduce x, MonadReduce y, Eq b) :: (Show b, MonadReduce x, MonadReduce y, Eq b)
......
...@@ -12,8 +12,8 @@ import System.Process.Typed ...@@ -12,8 +12,8 @@ import System.Process.Typed
import Test.Hspec.Core.Spec import Test.Hspec.Core.Spec
data GitGolden = GitGolden data GitGolden = GitGolden
{ filename :: FilePath { filename :: !FilePath
, content :: String , content :: !String
} }
instance Example GitGolden where instance Example GitGolden where
...@@ -29,8 +29,20 @@ instance Example GitGolden where ...@@ -29,8 +29,20 @@ instance Example GitGolden where
case ec of case ec of
ExitFailure _ -> do ExitFailure _ -> do
pure $ Result "" (Pending location (Just $ "file " <> show e.filename <> " not in index")) pure $
_ -> do Result
""
( Failure
location
( Reason $
"file "
<> show e.filename
<> " not in index"
<> "\n"
<> e.content
)
)
ExitSuccess -> do
(_, diff) <- readProcessStdout (proc "git" ["diff", e.filename]) (_, diff) <- readProcessStdout (proc "git" ["diff", e.filename])
pure $ case (ec, Text.unpack (Text.decodeUtf8 diff)) of pure $ case (ec, Text.unpack (Text.decodeUtf8 diff)) of
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment