You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
After discussion with @mbovel, consider adding this to stainless library and introduce a rewriting as sketched below to enable specification and verification of such functions in case of recursion.
importstainless.annotation.*importstainless.lang.*importstainless.lang.StaticChecks.*importstainless.proof.check@library
objectMemo:caseclassMemoFun[A,B](f: A=>B):@ignore @extern
valcache: scala.collection.mutable.Map[A, B] = scala.collection.mutable.Map.empty
@extern @pure
defapply(a: A):B= {
cache.getOrElseUpdate(a, {println(f"Cache miss: f(${a}) = ${f(a)}, cache.size = ${cache.size}"); f(a)})
}.ensuring(_ == f(a))
@extern @pure
defevict(a: A):Unit= cache.remove(a)
@extern @pure
defevictWhere(p: A=>Boolean):Unit=
cache.filterInPlace((k, _) =>!p(k))
@extern @pure
defevictAll():Unit= cache.clear()
valf:MemoFun[BigInt, BigInt] =MemoFun((n:BigInt) => {
// require(0 <= n) // should be allowed// decreases(n) // should be allowedif n <=1then n
else f(n -1) + f(n -2)
}) // .ensuring(_ >= 0) // should be allowed// the above should become, where fdef(x) should be used in the rest of code instead of `f(x)`:deffdef(n:BigInt):BigInt= {
require(0<= n)
decreases(n)
if n <=1then n
else fdef(n -1) + fdef(n -2)
}.ensuring(_ >=0)
// uses to f.evict, f.evictWhere, f.evicAll should be erased for verification@main @extern
defmain=valres1= f(40)
f.evict(6)
f.evictWhere(_ >4)
@samuelchassot@mbovel did we conclude that using inline def could introduce memoization while appearing to stainless like recursion through function values,
No description provided.
The text was updated successfully, but these errors were encountered: