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

Final code

parent 8fc081ff
Branches
No related tags found
No related merge requests found
...@@ -111,16 +111,16 @@ def reduce_expr(expr: Expr, check) -> Expr: ...@@ -111,16 +111,16 @@ def reduce_expr(expr: Expr, check) -> Expr:
return Const(0) return Const(0)
return expr return expr
elif isinstance(expr, Add): 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) lhs_ = reduce_expr(expr.lhs, check)
rhs_ = reduce_expr(expr.rhs, 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_) return Add(lhs_, rhs_)
elif isinstance(expr, Let): elif isinstance(expr, Let):
assignee_ = reduce_expr(expr.assignee, check) assignee_ = reduce_expr(expr.assignee, check)
if check("4) reduce to lhs"): if check("4) reduce to assingee"):
return assignee_ return assignee_
if check(f"5) inline {expr.var!r}"): if check(f"5) inline {expr.var!r}"):
return reduce_expr(expr.body.replace({expr.var: assignee_}), check) return reduce_expr(expr.body.replace({expr.var: assignee_}), check)
...@@ -145,7 +145,7 @@ if __name__ == "__main__": ...@@ -145,7 +145,7 @@ if __name__ == "__main__":
rt = partial(reduce_expr, expr) rt = partial(reduce_expr, expr)
p(rtree.Probe(rt, [])) p(rtree.Probe(rt, []))
rp = rtree.reduce(p, 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() print()
...@@ -158,4 +158,4 @@ if __name__ == "__main__": ...@@ -158,4 +158,4 @@ if __name__ == "__main__":
rt = partial(reduce_expr, expr) rt = partial(reduce_expr, expr)
p(rtree.Probe(rt, [])) p(rtree.Probe(rt, []))
rp = rtree.reduce(p, 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): ...@@ -96,7 +96,8 @@ def pretty(rp):
return "1" if a else "0" return "1" if a else "0"
return "".join( 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 ...@@ -13,6 +13,7 @@ library
Control.Monad.IRTree Control.Monad.IRTree
Control.Monad.Reduce Control.Monad.Reduce
Control.Monad.RTree Control.Monad.RTree
Control.Monad.RTree.Simple
Data.RPath Data.RPath
Data.Valuation Data.Valuation
other-modules: 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.
Please register or to comment