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

Final code

parent 8fc081ff
No related branches found
No related tags found
No related merge requests found
......@@ -111,16 +111,16 @@ def reduce_expr(expr: Expr, check) -> Expr:
return Const(0)
return expr
elif isinstance(expr, Add):
if check("2) reduce to lhs"):
return reduce_expr(expr.lhs, check)
if check("3) reduce to rhs"):
return reduce_expr(expr.rhs, check)
lhs_ = reduce_expr(expr.lhs, check)
rhs_ = reduce_expr(expr.rhs, check)
if check("2) reduce to lhs"):
return lhs_
if check("3) reduce to rhs"):
return rhs_
return Add(lhs_, rhs_)
elif isinstance(expr, Let):
assignee_ = reduce_expr(expr.assignee, check)
if check("4) reduce to lhs"):
if check("4) reduce to assingee"):
return assignee_
if check(f"5) inline {expr.var!r}"):
return reduce_expr(expr.body.replace({expr.var: assignee_}), check)
......@@ -145,7 +145,7 @@ if __name__ == "__main__":
rt = partial(reduce_expr, expr)
p(rtree.Probe(rt, []))
rp = rtree.reduce(p, rt)
print(f"&& {input_format(rp.input)} & true \\\\")
print(f"& \\verb|{rtree.pretty(rp)}| & {input_format(rp.input)} & true \\\\")
print()
......@@ -158,4 +158,4 @@ if __name__ == "__main__":
rt = partial(reduce_expr, expr)
p(rtree.Probe(rt, []))
rp = rtree.reduce(p, rt)
print(f"& {rtree.pretty{rp}} & {input_format(rp.input)} & true \\\\")
print(f"& {rtree.pretty(rp)} & {input_format(rp.input)} & true \\\\")
......@@ -96,7 +96,8 @@ def pretty(rp):
return "1" if a else "0"
return "".join(
a for a, _ in zip_longest(map(binary, rp.path), rp.reasons, fillvalue="*")
a if b != "*" else "!"
for a, b in zip_longest(map(binary, rp.path), rp.reasons, fillvalue="*")
)
......
......@@ -13,6 +13,7 @@ library
Control.Monad.IRTree
Control.Monad.Reduce
Control.Monad.RTree
Control.Monad.RTree.Simple
Data.RPath
Data.Valuation
other-modules:
......
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DerivingVia #-}
module Control.Monad.RTree.Simple where
import Control.Monad.Identity
import Control.Monad.State
type RTree a = State ([Bool], Int) a
check :: RTree Bool
check = state \(c : cs, n) -> (c, (cs, n + 1))
probe :: RTree a -> [Bool] -> (a, Int)
probe root path = (extract, depth - length path)
where
(extract, (_, depth)) = runState root (path ++ repeat False, 0)
reduce :: (a -> IO Bool) -> RTree a -> [Bool] -> IO a
reduce predicate root path
| undecided > 0 = do
result <- predicate (fst (probe root (path ++ [True])))
reduce predicate root (path ++ [result])
| otherwise = pure extract
where
(extract, undecided) = probe root path
reduceAbc :: RTree [Char]
reduceAbc = filterM (\_ -> check) "abc"
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