test: add Expr.find? test

This commit is contained in:
Leonardo de Moura 2020-02-17 19:49:13 -08:00
parent 09f613111b
commit 378dca293e
2 changed files with 19 additions and 9 deletions

View file

@ -44,23 +44,23 @@ else do
unsafe def initCache : State :=
{ keys := mkArray cacheSize.toNat (cast lcProof ()) }
@[inline] unsafe def findExprUnsafe? (p : Expr → Bool) (e : Expr) : Option Expr :=
@[inline] unsafe def findUnsafe? (p : Expr → Bool) (e : Expr) : Option Expr :=
(findM? p cacheSize e).run' initCache
end FindImpl
@[implementedBy FindImpl.findExprUnsafe?]
partial def findExpr? (p : Expr → Bool) : Expr → Option Expr
@[implementedBy FindImpl.findUnsafe?]
partial def find? (p : Expr → Bool) : Expr → Option Expr
| e =>
/- This is a reference implementation for the unsafe one above -/
if p e then some e
else match e with
| Expr.forallE _ d b _ => findExpr? d <|> findExpr? b
| Expr.lam _ d b _ => findExpr? d <|> findExpr? b
| Expr.mdata _ b _ => findExpr? b
| Expr.letE _ t v b _ => findExpr? t <|> findExpr? v <|> findExpr? b
| Expr.app f a _ => findExpr? f <|> findExpr? a
| Expr.proj _ _ b _ => findExpr? b
| Expr.forallE _ d b _ => find? d <|> find? b
| Expr.lam _ d b _ => find? d <|> find? b
| Expr.mdata _ b _ => find? b
| Expr.letE _ t v b _ => find? t <|> find? v <|> find? b
| Expr.app f a _ => find? f <|> find? a
| Expr.proj _ _ b _ => find? b
| e => none
end Expr

View file

@ -14,3 +14,13 @@ e.replace $ fun e => match e with
#eval replaceTest $ mkBig 4
#eval (replaceTest $ mkBig 128).getAppFn
def findTest (e : Expr) : Option Expr :=
e.find? $ fun e => match e with
| Expr.const c _ _ => c == `g
| _ => false
#eval findTest $ mkBig 4
#eval findTest $ replaceTest $ mkBig 4
#eval findTest $ mkBig 128
#eval findTest $ (replaceTest $ mkBig 128)