benchmark_name
stringclasses 3
values | index
stringlengths 1
3
| method_filepath
stringlengths 21
58
| method_without_assertion_filename
stringlengths 17
78
| method_name
stringlengths 6
66
| method_content
stringlengths 183
6.2k
| method_without_assertion_content
stringlengths 133
6.16k
|
---|---|---|---|---|---|---|
cedar | 809 | ./cedar-dafny/validation/thm/soundness.dfy | SoundInSetMemberFalse_fix_809.dfy | SoundInSetMemberFalse | lemma SoundInSetMemberFalse(e1: Expr, ei2s: seq<Expr>, i: nat, effs: Effects)
decreases BinaryApp(BinaryOp.In,e1,Expr.Set(ei2s)) , 0 , Expr.Set(ei2s) , 0
requires WellFormedRequestAndStore()
requires EffectsInvariant(effs)
requires WellTyped(BinaryApp(BinaryOp.In,e1,Expr.Set(ei2s)),effs)
requires getType(BinaryApp(BinaryOp.In,e1,Expr.Set(ei2s)),effs) == Type.Bool(False)
requires !reqty.isUnspecifiedVar(e1)
requires 0 <= i < |ei2s|
ensures IsFalse(r,s,BinaryApp(BinaryOp.In,e1,ei2s[i]))
{
// Reestablishing things we had at the call site in `SoundIn`.
var e2 := Expr.Set(ei2s);
assert TC.inferIn(BinaryApp(BinaryOp.In,e1,e2),e1,e2,effs) == types.Ok(Type.Bool(False));
assert TC.ensureEntityType(e1,effs).Ok?;
var t1 := getType(e1,effs);
var euids2 :- assert TC.tryGetEUIDs(e2);
var ets2 := set u <- euids2 :: u.ty;
// New proof.
var u2 :- assert TC.tryGetEUID(ei2s[i]);
assert u2 in euids2;
match e1 {
case Var(v1) =>
var et1 :- assert TC.getPrincipalOrResource(v1);
assert t1 == Type.Entity(EntityLUB({et1}));
assert IsSafe(r,s,Var(v1),t1) by { Sound(e1,t1,effs); }
assert !ets.possibleDescendantOf(et1,u2.ty);
reveal WellFormedRequestAndStore();
InSingleFalseEntityTypeAndLiteral(r,s,e1,et1,u2);
case PrimitiveLit(EntityUID(u1)) =>
if isAction(u1.ty) {
assert !acts.descendantOfSet(u1,euids2);
assert !acts.descendantOf(u1,u2);
} else {
assert !ets.possibleDescendantOfSet(u1.ty,ets2);
assert !ets.possibleDescendantOf(u1.ty,u2.ty);
}
reveal WellFormedRequestAndStore();
InSingleFalseLiterals(r,s,u1,u2);
}
}
| lemma SoundInSetMemberFalse(e1: Expr, ei2s: seq<Expr>, i: nat, effs: Effects)
decreases BinaryApp(BinaryOp.In,e1,Expr.Set(ei2s)) , 0 , Expr.Set(ei2s) , 0
requires WellFormedRequestAndStore()
requires EffectsInvariant(effs)
requires WellTyped(BinaryApp(BinaryOp.In,e1,Expr.Set(ei2s)),effs)
requires getType(BinaryApp(BinaryOp.In,e1,Expr.Set(ei2s)),effs) == Type.Bool(False)
requires !reqty.isUnspecifiedVar(e1)
requires 0 <= i < |ei2s|
ensures IsFalse(r,s,BinaryApp(BinaryOp.In,e1,ei2s[i]))
{
// Reestablishing things we had at the call site in `SoundIn`.
var e2 := Expr.Set(ei2s);
assert TC.ensureEntityType(e1,effs).Ok?;
var t1 := getType(e1,effs);
var euids2 :- assert TC.tryGetEUIDs(e2);
var ets2 := set u <- euids2 :: u.ty;
// New proof.
var u2 :- assert TC.tryGetEUID(ei2s[i]);
assert u2 in euids2;
match e1 {
case Var(v1) =>
var et1 :- assert TC.getPrincipalOrResource(v1);
assert t1 == Type.Entity(EntityLUB({et1}));
assert IsSafe(r,s,Var(v1),t1) by { Sound(e1,t1,effs); }
assert !ets.possibleDescendantOf(et1,u2.ty);
reveal WellFormedRequestAndStore();
InSingleFalseEntityTypeAndLiteral(r,s,e1,et1,u2);
case PrimitiveLit(EntityUID(u1)) =>
if isAction(u1.ty) {
assert !acts.descendantOfSet(u1,euids2);
assert !acts.descendantOf(u1,u2);
} else {
assert !ets.possibleDescendantOfSet(u1.ty,ets2);
assert !ets.possibleDescendantOf(u1.ty,u2.ty);
}
reveal WellFormedRequestAndStore();
InSingleFalseLiterals(r,s,u1,u2);
}
}
|
cedar | 102 | ./cedar-dafny/validation/thm/model.dfy | OrRShortSafe_fix_102.dfy | OrRShortSafe | lemma OrRShortSafe(r: Request, s: EntityStore, e: Expr, e': Expr)
requires IsSafe(r,s,e,Type.Bool(AnyBool))
requires IsTrue(r,s,e')
ensures IsTrue(r,s,Or(e,e'))
{
reveal IsSafe();
if Evaluate(e,r,s).Ok? && Evaluate(e',r,s).Ok? {
assert Evaluate(e',r,s) == base.Ok(Value.Primitive(Primitive.Bool(true)));
assert Evaluator(r,s).interpretShortcircuit(Or(e,e'),e,e',true) == base.Ok(Value.Primitive(Primitive.Bool(true)));
assert Evaluate(Or(e,e'),r,s) == base.Ok(Value.Primitive(Primitive.Bool(true)));
} else {
if Evaluate(e,r,s).Err? {
assert Evaluator(r,s).interpretShortcircuit(Or(e,e'),e,e',true) == Evaluate(e,r,s);
assert Evaluate(Or(e,e'),r,s) == Evaluate(e,r,s);
} else {
assert Evaluate(e',r,s).Err?;
var b :| Evaluate(e,r,s) == base.Ok(Value.Primitive(Primitive.Bool(b)));
if b {
assert Evaluator(r,s).interpretShortcircuit(Or(e,e'),e,e',true) == base.Ok(Value.Primitive(Primitive.Bool(true)));
assert Evaluate(Or(e,e'),r,s) == base.Ok(Value.Primitive(Primitive.Bool(true)));
} else {
assert Evaluator(r,s).interpretShortcircuit(Or(e,e'),e,e',true) == Evaluate(e',r,s);
assert Evaluate(Or(e,e'),r,s) == Evaluate(e',r,s);
}
}
}
}
| lemma OrRShortSafe(r: Request, s: EntityStore, e: Expr, e': Expr)
requires IsSafe(r,s,e,Type.Bool(AnyBool))
requires IsTrue(r,s,e')
ensures IsTrue(r,s,Or(e,e'))
{
reveal IsSafe();
if Evaluate(e,r,s).Ok? && Evaluate(e',r,s).Ok? {
assert Evaluate(e',r,s) == base.Ok(Value.Primitive(Primitive.Bool(true)));
assert Evaluate(Or(e,e'),r,s) == base.Ok(Value.Primitive(Primitive.Bool(true)));
} else {
if Evaluate(e,r,s).Err? {
assert Evaluator(r,s).interpretShortcircuit(Or(e,e'),e,e',true) == Evaluate(e,r,s);
assert Evaluate(Or(e,e'),r,s) == Evaluate(e,r,s);
} else {
assert Evaluate(e',r,s).Err?;
var b :| Evaluate(e,r,s) == base.Ok(Value.Primitive(Primitive.Bool(b)));
if b {
assert Evaluator(r,s).interpretShortcircuit(Or(e,e'),e,e',true) == base.Ok(Value.Primitive(Primitive.Bool(true)));
assert Evaluate(Or(e,e'),r,s) == base.Ok(Value.Primitive(Primitive.Bool(true)));
} else {
assert Evaluator(r,s).interpretShortcircuit(Or(e,e'),e,e',true) == Evaluate(e',r,s);
assert Evaluate(Or(e,e'),r,s) == Evaluate(e',r,s);
}
}
}
}
|
cedar | 54 | ./cedar-dafny/validation/thm/model.dfy | OrSafe_fix_54.dfy | OrSafe | lemma OrSafe(r: Request, s: EntityStore, e: Expr, e': Expr)
requires IsSafe(r,s,e,Type.Bool(AnyBool))
requires IsSafe(r,s,e',Type.Bool(AnyBool))
ensures IsSafe(r,s,Or(e,e'),Type.Bool(AnyBool))
{
reveal IsSafe();
if Evaluate(e,r,s).Ok? && Evaluate(e',r,s).Ok? {
assert Evaluator(r,s).interpretShortcircuit(Or(e,e'),e,e',true).Ok?;
assert Evaluate(Or(e,e'),r,s).Ok?;
} else {
if Evaluate(e,r,s).Err? {
assert Evaluator(r,s).interpretShortcircuit(Or(e,e'),e,e',true) == Evaluate(e,r,s);
assert Evaluate(Or(e,e'),r,s) == Evaluate(e,r,s);
} else {
assert Evaluate(e',r,s).Err?;
var b :| Evaluate(e,r,s) == base.Ok(Value.Primitive(Primitive.Bool(b)));
if b {
assert Evaluator(r,s).interpretShortcircuit(Or(e,e'),e,e',true) == base.Ok(Value.Primitive(Primitive.Bool(true)));
assert Evaluate(Or(e,e'),r,s) == base.Ok(Value.Primitive(Primitive.Bool(true)));
} else {
assert Evaluator(r,s).interpretShortcircuit(Or(e,e'),e,e',true) == Evaluate(e',r,s);
assert Evaluate(Or(e,e'),r,s) == Evaluate(e',r,s);
}
}
}
}
| lemma OrSafe(r: Request, s: EntityStore, e: Expr, e': Expr)
requires IsSafe(r,s,e,Type.Bool(AnyBool))
requires IsSafe(r,s,e',Type.Bool(AnyBool))
ensures IsSafe(r,s,Or(e,e'),Type.Bool(AnyBool))
{
reveal IsSafe();
if Evaluate(e,r,s).Ok? && Evaluate(e',r,s).Ok? {
assert Evaluator(r,s).interpretShortcircuit(Or(e,e'),e,e',true).Ok?;
assert Evaluate(Or(e,e'),r,s).Ok?;
} else {
if Evaluate(e,r,s).Err? {
assert Evaluator(r,s).interpretShortcircuit(Or(e,e'),e,e',true) == Evaluate(e,r,s);
assert Evaluate(Or(e,e'),r,s) == Evaluate(e,r,s);
} else {
assert Evaluate(e',r,s).Err?;
var b :| Evaluate(e,r,s) == base.Ok(Value.Primitive(Primitive.Bool(b)));
if b {
assert Evaluator(r,s).interpretShortcircuit(Or(e,e'),e,e',true) == base.Ok(Value.Primitive(Primitive.Bool(true)));
assert Evaluate(Or(e,e'),r,s) == base.Ok(Value.Primitive(Primitive.Bool(true)));
} else {
assert Evaluate(Or(e,e'),r,s) == Evaluate(e',r,s);
}
}
}
}
|
cedar | 33 | ./cedar-dafny/validation/thm/model.dfy | AndLShortSafe_fix_33.dfy | AndLShortSafe | lemma AndLShortSafe(r: Request, s: EntityStore, e: Expr, e': Expr)
requires IsFalse(r,s,e)
ensures IsFalse(r,s,And(e,e'))
{
reveal IsSafe();
if Evaluate(e,r,s).Ok? {
assert Evaluate(e,r,s) == base.Ok(Value.Primitive(Primitive.Bool(false)));
assert Evaluator(r,s).interpretShortcircuit(And(e,e'),e,e',false) == base.Ok(Value.Primitive(Primitive.Bool(false)));
assert Evaluate(And(e,e'),r,s) == base.Ok(Value.Primitive(Primitive.Bool(false)));
} else {
assert Evaluator(r,s).interpretShortcircuit(And(e,e'),e,e',false) == Evaluate(e,r,s);
assert Evaluate(And(e,e'),r,s) == Evaluate(e,r,s);
}
}
| lemma AndLShortSafe(r: Request, s: EntityStore, e: Expr, e': Expr)
requires IsFalse(r,s,e)
ensures IsFalse(r,s,And(e,e'))
{
reveal IsSafe();
if Evaluate(e,r,s).Ok? {
assert Evaluate(e,r,s) == base.Ok(Value.Primitive(Primitive.Bool(false)));
assert Evaluate(And(e,e'),r,s) == base.Ok(Value.Primitive(Primitive.Bool(false)));
} else {
assert Evaluator(r,s).interpretShortcircuit(And(e,e'),e,e',false) == Evaluate(e,r,s);
assert Evaluate(And(e,e'),r,s) == Evaluate(e,r,s);
}
}
|
cedar | 789 | ./cedar-dafny/validation/thm/soundness.dfy | SoundArith_fix_789.dfy | SoundArith | lemma SoundArith(op: BinaryOp, e1: Expr, e2: Expr, t: Type, effs: Effects)
decreases BinaryApp(op,e1,e2) , 0
requires op == Add || op == Sub
requires WellFormedRequestAndStore()
requires EffectsInvariant(effs)
requires Typesafe(BinaryApp(op,e1,e2),effs,t)
ensures IsSafe(r,s,BinaryApp(op,e1,e2),t)
ensures getEffects(BinaryApp(op,e1,e2),effs) == Effects.empty()
{
var t' :| getType(BinaryApp(op,e1,e2),effs) == t' && subty(t',t,ValidationMode.Permissive);
assert TC.inferArith2(op,e1,e2,effs) == types.Ok(Type.Int);
assert TC.ensureIntType(e1,effs).Ok?;
assert Typesafe(e1,effs,Type.Int);
assert TC.ensureIntType(e2,effs).Ok?;
assert Typesafe(e2,effs,Type.Int);
assert IsSafe(r,s,e1,Type.Int) by { Sound(e1,Type.Int,effs); }
assert IsSafe(r,s,e2,Type.Int) by { Sound(e2,Type.Int,effs); }
assert IsSafe(r,s,BinaryApp(op,e1,e2),t') by { ArithSafe(r,s,op,e1,e2); }
assert IsSafe(r,s,BinaryApp(op,e1,e2),t) by {
SubtyCompat(t',t);
SemSubtyTransport(r,s,BinaryApp(op,e1,e2),t',t);
}
}
| lemma SoundArith(op: BinaryOp, e1: Expr, e2: Expr, t: Type, effs: Effects)
decreases BinaryApp(op,e1,e2) , 0
requires op == Add || op == Sub
requires WellFormedRequestAndStore()
requires EffectsInvariant(effs)
requires Typesafe(BinaryApp(op,e1,e2),effs,t)
ensures IsSafe(r,s,BinaryApp(op,e1,e2),t)
ensures getEffects(BinaryApp(op,e1,e2),effs) == Effects.empty()
{
var t' :| getType(BinaryApp(op,e1,e2),effs) == t' && subty(t',t,ValidationMode.Permissive);
assert TC.inferArith2(op,e1,e2,effs) == types.Ok(Type.Int);
assert Typesafe(e1,effs,Type.Int);
assert TC.ensureIntType(e2,effs).Ok?;
assert Typesafe(e2,effs,Type.Int);
assert IsSafe(r,s,e1,Type.Int) by { Sound(e1,Type.Int,effs); }
assert IsSafe(r,s,e2,Type.Int) by { Sound(e2,Type.Int,effs); }
assert IsSafe(r,s,BinaryApp(op,e1,e2),t') by { ArithSafe(r,s,op,e1,e2); }
assert IsSafe(r,s,BinaryApp(op,e1,e2),t) by {
SubtyCompat(t',t);
SemSubtyTransport(r,s,BinaryApp(op,e1,e2),t',t);
}
}
|
cedar | 19 | ./cedar-dafny/validation/subtyping.dfy | StrictLubIsStrict_fix_19.dfy | StrictLubIsStrict | lemma StrictLubIsStrict(t1: Type, t2: Type)
requires LubDefined(t1, t2, ValidationMode.Strict)
ensures lubOpt(t1, t2, ValidationMode.Permissive) == lubOpt(t1, t2, ValidationMode.Strict)
{
match (t1,t2) {
case (Never,_) =>
case (_,Never) =>
case (Int,Int) =>
case (String,String) =>
case (Bool(b1),Bool(b2)) =>
case (Entity(e1),Entity(e2)) =>
case (Set(t1'),Set(t2')) =>
case (Record(rt1),Record(rt2)) => {
assert lubRecordType(rt1, rt2, ValidationMode.Strict).Ok?;
assert lubRecordType(rt1, rt2, ValidationMode.Permissive).Ok?;
var strict_attrs :=
map k | k in rt1.attrs.Keys && k in rt2.attrs.Keys && lubOpt(rt1.attrs[k].ty, rt2.attrs[k].ty, ValidationMode.Strict).Ok? ::
lubAttrType(rt1.attrs[k], rt2.attrs[k], ValidationMode.Strict);
assert strict_attrs == lubRecordType(rt1, rt2, ValidationMode.Strict).value.attrs;
var permissive_attrs :=
map k | k in rt1.attrs.Keys && k in rt2.attrs.Keys && lubOpt(rt1.attrs[k].ty, rt2.attrs[k].ty, ValidationMode.Permissive).Ok? ::
lubAttrType(rt1.attrs[k], rt2.attrs[k], ValidationMode.Permissive);
assert permissive_attrs == lubRecordType(rt1, rt2, ValidationMode.Permissive).value.attrs;
assert permissive_attrs == strict_attrs;
}
case (Extension(n1),Extension(n2)) =>
}
}
| lemma StrictLubIsStrict(t1: Type, t2: Type)
requires LubDefined(t1, t2, ValidationMode.Strict)
ensures lubOpt(t1, t2, ValidationMode.Permissive) == lubOpt(t1, t2, ValidationMode.Strict)
{
match (t1,t2) {
case (Never,_) =>
case (_,Never) =>
case (Int,Int) =>
case (String,String) =>
case (Bool(b1),Bool(b2)) =>
case (Entity(e1),Entity(e2)) =>
case (Set(t1'),Set(t2')) =>
case (Record(rt1),Record(rt2)) => {
assert lubRecordType(rt1, rt2, ValidationMode.Strict).Ok?;
assert lubRecordType(rt1, rt2, ValidationMode.Permissive).Ok?;
var strict_attrs :=
map k | k in rt1.attrs.Keys && k in rt2.attrs.Keys && lubOpt(rt1.attrs[k].ty, rt2.attrs[k].ty, ValidationMode.Strict).Ok? ::
lubAttrType(rt1.attrs[k], rt2.attrs[k], ValidationMode.Strict);
assert strict_attrs == lubRecordType(rt1, rt2, ValidationMode.Strict).value.attrs;
var permissive_attrs :=
map k | k in rt1.attrs.Keys && k in rt2.attrs.Keys && lubOpt(rt1.attrs[k].ty, rt2.attrs[k].ty, ValidationMode.Permissive).Ok? ::
lubAttrType(rt1.attrs[k], rt2.attrs[k], ValidationMode.Permissive);
assert permissive_attrs == lubRecordType(rt1, rt2, ValidationMode.Permissive).value.attrs;
}
case (Extension(n1),Extension(n2)) =>
}
}
|
cedar | 771 | ./cedar-dafny/validation/thm/soundness.dfy | SoundContains_fix_771.dfy | SoundContains | lemma SoundContainsAnyAll(op: BinaryOp, e1: Expr, e2: Expr, t: Type, effs: Effects)
decreases BinaryApp(op,e1,e2) , 0
requires op == ContainsAny || op == ContainsAll
requires WellFormedRequestAndStore()
requires EffectsInvariant(effs)
requires Typesafe(BinaryApp(op,e1,e2),effs,t)
ensures IsSafe(r,s,BinaryApp(op,e1,e2),t)
ensures getEffects(BinaryApp(op,e1,e2),effs) == Effects.empty()
{
var t' :| getType(BinaryApp(op,e1,e2),effs) == t' && subty(t',t,ValidationMode.Permissive);
assert TC.inferContainsAnyAll(op,e1,e2,effs) == types.Ok(t');
var t1 := TC.inferSetType(e1,effs).value;
var t2 := TC.inferSetType(e2,effs).value;
assert Typesafe(e1,effs,Type.Set(t1)) by { SubtyRefl(Type.Set(t1)); }
assert Typesafe(e2,effs,Type.Set(t2)) by { SubtyRefl(Type.Set(t2)); }
assert IsSafe(r,s,e1,Type.Set(t1)) by { Sound(e1,Type.Set(t1),effs); }
assert IsSafe(r,s,e2,Type.Set(t2)) by { Sound(e2,Type.Set(t2),effs); }
assert IsSafe(r,s,BinaryApp(op,e1,e2),t') by { ContainsAnyAllSafe(r,s,op,e1,e2,t1,t2); }
assert IsSafe(r,s,BinaryApp(op,e1,e2),t) by {
SubtyCompat(t',t);
SemSubtyTransport(r,s,BinaryApp(op,e1,e2),t',t);
}
}
| lemma SoundContainsAnyAll(op: BinaryOp, e1: Expr, e2: Expr, t: Type, effs: Effects)
decreases BinaryApp(op,e1,e2) , 0
requires op == ContainsAny || op == ContainsAll
requires WellFormedRequestAndStore()
requires EffectsInvariant(effs)
requires Typesafe(BinaryApp(op,e1,e2),effs,t)
ensures IsSafe(r,s,BinaryApp(op,e1,e2),t)
ensures getEffects(BinaryApp(op,e1,e2),effs) == Effects.empty()
{
var t' :| getType(BinaryApp(op,e1,e2),effs) == t' && subty(t',t,ValidationMode.Permissive);
var t1 := TC.inferSetType(e1,effs).value;
var t2 := TC.inferSetType(e2,effs).value;
assert Typesafe(e1,effs,Type.Set(t1)) by { SubtyRefl(Type.Set(t1)); }
assert Typesafe(e2,effs,Type.Set(t2)) by { SubtyRefl(Type.Set(t2)); }
assert IsSafe(r,s,e1,Type.Set(t1)) by { Sound(e1,Type.Set(t1),effs); }
assert IsSafe(r,s,e2,Type.Set(t2)) by { Sound(e2,Type.Set(t2),effs); }
assert IsSafe(r,s,BinaryApp(op,e1,e2),t') by { ContainsAnyAllSafe(r,s,op,e1,e2,t1,t2); }
assert IsSafe(r,s,BinaryApp(op,e1,e2),t) by {
SubtyCompat(t',t);
SemSubtyTransport(r,s,BinaryApp(op,e1,e2),t',t);
}
}
|
cedar | 838 | ./cedar-dafny/validation/thm/soundness.dfy | SoundHasAttr_fix_838.dfy | SoundHasAttr | lemma SoundHasAttr(e: Expr, k: Attr, t: Type, effs: Effects)
decreases HasAttr(e,k) , 0
requires WellFormedRequestAndStore()
requires EffectsInvariant(effs)
requires Typesafe(HasAttr(e,k),effs,t)
ensures IsSafe(r,s,HasAttr(e,k),t)
ensures GuardedEffectsInvariant(HasAttr(e,k),getEffects(HasAttr(e,k),effs))
{
var t' :| getType(HasAttr(e,k),effs) == t' && subty(t',t,ValidationMode.Permissive);
assert TC.inferHasAttr(e,k,effs).Ok?;
var ret := TC.inferRecordEntityType(e,effs).value;
assert GuardedEffectsInvariant(HasAttr(e,k),Effects.empty()) by {
EmptyEffectsInvariant();
}
match ret {
case Record(rt) => {
assert Typesafe(e,effs,Type.Record(rt)) by { SubtyRefl(Type.Record(rt)); }
assert IsSafe(r,s,e,Type.Record(rt)) by { Sound(e,Type.Record(rt),effs); }
if k in rt.attrs {
if rt.attrs[k].isRequired {
assert IsSafe(r,s,e,Type.Record(RecordType(map[k := rt.attrs[k]], OpenAttributes))) by {
SubtyRefl(rt.attrs[k].ty);
assert subtyRecordType(rt,RecordType(map[k := rt.attrs[k]], OpenAttributes),ValidationMode.Permissive);
assert subty(Type.Record(rt),Type.Record(RecordType(map[k := rt.attrs[k]], OpenAttributes)),ValidationMode.Permissive);
SubtyCompat(Type.Record(rt),Type.Record(RecordType(map[k := rt.attrs[k]], OpenAttributes)));
SemSubtyTransport(r,s,e,Type.Record(rt),Type.Record(RecordType(map[k := rt.attrs[k]], OpenAttributes)));
}
assert IsSafe(r,s,HasAttr(e,k),t') by { RecordHasRequiredTrueSafe(r,s,e,k,rt.attrs[k]); }
} else if effs.contains(e,k) {
assert IsSafe(r,s,HasAttr(e,k),t') by {
reveal EffectsInvariant();
}
} else {
assert IsSafe(r,s,e,Type.Record(RecordType(map[], OpenAttributes))) by {
assert subty(Type.Record(rt),Type.Record(RecordType(map[], OpenAttributes)),ValidationMode.Permissive);
SubtyCompat(Type.Record(rt),Type.Record(RecordType(map[], OpenAttributes)));
SemSubtyTransport(r,s,e,Type.Record(rt),Type.Record(RecordType(map[], OpenAttributes)));
}
assert IsSafe(r,s,HasAttr(e,k),t') by { RecordHasOpenRecSafe(r,s,e,k); }
assert GuardedEffectsInvariant(HasAttr(e,k),Effects.singleton(e,k)) by {
if IsTrueStrong(r,s,HasAttr(e,k)) {
IsTrueStrongImpliesIsTrue(r,s,HasAttr(e,k));
reveal EffectsInvariant();
}
}
}
} else if rt.isOpen() {
assert IsSafe(r,s,e,Type.Record(RecordType(map[], OpenAttributes))) by {
assert subty(Type.Record(rt),Type.Record(RecordType(map[], OpenAttributes)),ValidationMode.Permissive);
SubtyCompat(Type.Record(rt),Type.Record(RecordType(map[], OpenAttributes)));
SemSubtyTransport(r,s,e,Type.Record(rt),Type.Record(RecordType(map[], OpenAttributes)));
}
assert IsSafe(r,s,HasAttr(e,k),t') by { RecordHasOpenRecSafe(r,s,e,k); }
} else {
assert IsSafe(r,s,HasAttr(e,k),t') by { RecordHasClosedRecFalseSafe(r,s,e,k, rt); }
}
}
case Entity(et) => {
assert Typesafe(e,effs,Type.Entity(et)) by { SubtyRefl(Type.Entity(et)); }
assert IsSafe(r,s,e,Type.Entity(et)) by { Sound(e,Type.Entity(et),effs); }
if !ets.isAttrPossible(et,k) {
reveal WellFormedRequestAndStore();
EntityHasImpossibleFalseSafe(r,s,e,k,et);
} else {
var m := ets.getLubRecordType(et,ValidationMode.Permissive).value;
if k in m.attrs {
if effs.contains(e,k) {
assert IsSafe(r,s,HasAttr(e,k),t') by {
reveal EffectsInvariant();
}
} else {
assert IsSafe(r,s,e,Type.Entity(AnyEntity)) by {
SubtyCompat(Type.Entity(et),Type.Entity(AnyEntity));
SemSubtyTransport(r,s,e,Type.Entity(et),Type.Entity(AnyEntity));
}
assert IsSafe(r,s,HasAttr(e,k),t') by { EntityHasOpenSafe(r,s,e,k); }
assert GuardedEffectsInvariant(HasAttr(e,k),Effects.singleton(e,k)) by {
if IsTrueStrong(r,s,HasAttr(e,k)) {
IsTrueStrongImpliesIsTrue(r,s,HasAttr(e,k));
reveal EffectsInvariant();
}
}
}
} else {
PossibleAttrNotInLubAttrImpliesOpen(et, k, m);
assert IsSafe(r,s,e,Type.Entity(AnyEntity)) by {
SubtyCompat(Type.Entity(et),Type.Entity(AnyEntity));
SemSubtyTransport(r,s,e,Type.Entity(et),Type.Entity(AnyEntity));
}
assert IsSafe(r,s,HasAttr(e,k),t') by { EntityHasOpenSafe(r,s,e,k); }
}
}
}
}
assert IsSafe(r,s,HasAttr(e,k),t) by {
SubtyCompat(t',t);
SemSubtyTransport(r,s,HasAttr(e,k),t',t);
}
}
| lemma SoundHasAttr(e: Expr, k: Attr, t: Type, effs: Effects)
decreases HasAttr(e,k) , 0
requires WellFormedRequestAndStore()
requires EffectsInvariant(effs)
requires Typesafe(HasAttr(e,k),effs,t)
ensures IsSafe(r,s,HasAttr(e,k),t)
ensures GuardedEffectsInvariant(HasAttr(e,k),getEffects(HasAttr(e,k),effs))
{
var t' :| getType(HasAttr(e,k),effs) == t' && subty(t',t,ValidationMode.Permissive);
var ret := TC.inferRecordEntityType(e,effs).value;
assert GuardedEffectsInvariant(HasAttr(e,k),Effects.empty()) by {
EmptyEffectsInvariant();
}
match ret {
case Record(rt) => {
assert Typesafe(e,effs,Type.Record(rt)) by { SubtyRefl(Type.Record(rt)); }
assert IsSafe(r,s,e,Type.Record(rt)) by { Sound(e,Type.Record(rt),effs); }
if k in rt.attrs {
if rt.attrs[k].isRequired {
assert IsSafe(r,s,e,Type.Record(RecordType(map[k := rt.attrs[k]], OpenAttributes))) by {
SubtyRefl(rt.attrs[k].ty);
assert subtyRecordType(rt,RecordType(map[k := rt.attrs[k]], OpenAttributes),ValidationMode.Permissive);
assert subty(Type.Record(rt),Type.Record(RecordType(map[k := rt.attrs[k]], OpenAttributes)),ValidationMode.Permissive);
SubtyCompat(Type.Record(rt),Type.Record(RecordType(map[k := rt.attrs[k]], OpenAttributes)));
SemSubtyTransport(r,s,e,Type.Record(rt),Type.Record(RecordType(map[k := rt.attrs[k]], OpenAttributes)));
}
assert IsSafe(r,s,HasAttr(e,k),t') by { RecordHasRequiredTrueSafe(r,s,e,k,rt.attrs[k]); }
} else if effs.contains(e,k) {
assert IsSafe(r,s,HasAttr(e,k),t') by {
reveal EffectsInvariant();
}
} else {
assert IsSafe(r,s,e,Type.Record(RecordType(map[], OpenAttributes))) by {
assert subty(Type.Record(rt),Type.Record(RecordType(map[], OpenAttributes)),ValidationMode.Permissive);
SubtyCompat(Type.Record(rt),Type.Record(RecordType(map[], OpenAttributes)));
SemSubtyTransport(r,s,e,Type.Record(rt),Type.Record(RecordType(map[], OpenAttributes)));
}
assert IsSafe(r,s,HasAttr(e,k),t') by { RecordHasOpenRecSafe(r,s,e,k); }
assert GuardedEffectsInvariant(HasAttr(e,k),Effects.singleton(e,k)) by {
if IsTrueStrong(r,s,HasAttr(e,k)) {
IsTrueStrongImpliesIsTrue(r,s,HasAttr(e,k));
reveal EffectsInvariant();
}
}
}
} else if rt.isOpen() {
assert IsSafe(r,s,e,Type.Record(RecordType(map[], OpenAttributes))) by {
assert subty(Type.Record(rt),Type.Record(RecordType(map[], OpenAttributes)),ValidationMode.Permissive);
SubtyCompat(Type.Record(rt),Type.Record(RecordType(map[], OpenAttributes)));
SemSubtyTransport(r,s,e,Type.Record(rt),Type.Record(RecordType(map[], OpenAttributes)));
}
assert IsSafe(r,s,HasAttr(e,k),t') by { RecordHasOpenRecSafe(r,s,e,k); }
} else {
assert IsSafe(r,s,HasAttr(e,k),t') by { RecordHasClosedRecFalseSafe(r,s,e,k, rt); }
}
}
case Entity(et) => {
assert Typesafe(e,effs,Type.Entity(et)) by { SubtyRefl(Type.Entity(et)); }
assert IsSafe(r,s,e,Type.Entity(et)) by { Sound(e,Type.Entity(et),effs); }
if !ets.isAttrPossible(et,k) {
reveal WellFormedRequestAndStore();
EntityHasImpossibleFalseSafe(r,s,e,k,et);
} else {
var m := ets.getLubRecordType(et,ValidationMode.Permissive).value;
if k in m.attrs {
if effs.contains(e,k) {
assert IsSafe(r,s,HasAttr(e,k),t') by {
reveal EffectsInvariant();
}
} else {
assert IsSafe(r,s,e,Type.Entity(AnyEntity)) by {
SubtyCompat(Type.Entity(et),Type.Entity(AnyEntity));
SemSubtyTransport(r,s,e,Type.Entity(et),Type.Entity(AnyEntity));
}
assert IsSafe(r,s,HasAttr(e,k),t') by { EntityHasOpenSafe(r,s,e,k); }
assert GuardedEffectsInvariant(HasAttr(e,k),Effects.singleton(e,k)) by {
if IsTrueStrong(r,s,HasAttr(e,k)) {
IsTrueStrongImpliesIsTrue(r,s,HasAttr(e,k));
reveal EffectsInvariant();
}
}
}
} else {
PossibleAttrNotInLubAttrImpliesOpen(et, k, m);
assert IsSafe(r,s,e,Type.Entity(AnyEntity)) by {
SubtyCompat(Type.Entity(et),Type.Entity(AnyEntity));
SemSubtyTransport(r,s,e,Type.Entity(et),Type.Entity(AnyEntity));
}
assert IsSafe(r,s,HasAttr(e,k),t') by { EntityHasOpenSafe(r,s,e,k); }
}
}
}
}
assert IsSafe(r,s,HasAttr(e,k),t) by {
SubtyCompat(t',t);
SemSubtyTransport(r,s,HasAttr(e,k),t',t);
}
}
|
cedar | 72 | ./cedar-dafny/validation/thm/model.dfy | AndLRetSafe_fix_72.dfy | AndLRetSafe | lemma AndLRetSafe(r: Request, s: EntityStore, e: Expr, e': Expr, t: Type)
requires IsSafe(r,s,e,t)
requires IsTrue(r,s,e')
requires SemanticSubty(t,Type.Bool(AnyBool))
ensures IsSafe(r,s,And(e,e'),t)
{
reveal IsSafe();
if Evaluate(e,r,s).Ok? && Evaluate(e',r,s).Ok? {
assert Evaluate(e',r,s) == base.Ok(Value.Primitive(Primitive.Bool(true)));
var v :| Evaluate(e,r,s) == base.Ok(v) && InstanceOfType(v,t);
assert InstanceOfType(v,Type.Bool(AnyBool)) by {
SemSubtyTransportVal(t,Type.Bool(AnyBool),v);
}
var b :| v == Value.Primitive(Primitive.Bool(b));
assert Evaluator(r,s).interpretShortcircuit(And(e,e'),e,e',false) == base.Ok(Value.Primitive(Primitive.Bool(b)));
assert Evaluate(And(e,e'),r,s) == base.Ok(Value.Primitive(Primitive.Bool(b)));
} else {
if Evaluate(e,r,s).Err? {
assert Evaluator(r,s).interpretShortcircuit(And(e,e'),e,e',false) == Evaluate(e,r,s);
assert Evaluate(And(e,e'),r,s) == Evaluate(e,r,s);
} else {
assert Evaluate(e',r,s).Err?;
var b :| Evaluate(e,r,s) == base.Ok(Value.Primitive(Primitive.Bool(b)));
if b {
assert Evaluator(r,s).interpretShortcircuit(And(e,e'),e,e',false) == Evaluate(e',r,s);
assert Evaluate(And(e,e'),r,s) == Evaluate(e',r,s);
} else {
assert Evaluator(r,s).interpretShortcircuit(And(e,e'),e,e',false) == base.Ok(Value.Primitive(Primitive.Bool(false)));
assert Evaluate(And(e,e'),r,s) == base.Ok(Value.Primitive(Primitive.Bool(false)));
}
}
}
}
| lemma AndLRetSafe(r: Request, s: EntityStore, e: Expr, e': Expr, t: Type)
requires IsSafe(r,s,e,t)
requires IsTrue(r,s,e')
requires SemanticSubty(t,Type.Bool(AnyBool))
ensures IsSafe(r,s,And(e,e'),t)
{
reveal IsSafe();
if Evaluate(e,r,s).Ok? && Evaluate(e',r,s).Ok? {
assert Evaluate(e',r,s) == base.Ok(Value.Primitive(Primitive.Bool(true)));
var v :| Evaluate(e,r,s) == base.Ok(v) && InstanceOfType(v,t);
assert InstanceOfType(v,Type.Bool(AnyBool)) by {
SemSubtyTransportVal(t,Type.Bool(AnyBool),v);
}
var b :| v == Value.Primitive(Primitive.Bool(b));
assert Evaluator(r,s).interpretShortcircuit(And(e,e'),e,e',false) == base.Ok(Value.Primitive(Primitive.Bool(b)));
assert Evaluate(And(e,e'),r,s) == base.Ok(Value.Primitive(Primitive.Bool(b)));
} else {
if Evaluate(e,r,s).Err? {
assert Evaluate(And(e,e'),r,s) == Evaluate(e,r,s);
} else {
assert Evaluate(e',r,s).Err?;
var b :| Evaluate(e,r,s) == base.Ok(Value.Primitive(Primitive.Bool(b)));
if b {
assert Evaluator(r,s).interpretShortcircuit(And(e,e'),e,e',false) == Evaluate(e',r,s);
assert Evaluate(And(e,e'),r,s) == Evaluate(e',r,s);
} else {
assert Evaluator(r,s).interpretShortcircuit(And(e,e'),e,e',false) == base.Ok(Value.Primitive(Primitive.Bool(false)));
assert Evaluate(And(e,e'),r,s) == base.Ok(Value.Primitive(Primitive.Bool(false)));
}
}
}
}
|
cedar | 731 | ./cedar-dafny/validation/thm/soundness.dfy | TypesafeCallSemantics_fix_731.dfy | TypesafeCallSemantics | lemma TypesafeCallSemantics(name: base.Name, es: seq<Expr>, effs: Effects, t: Type)
requires Typesafe(Call(name,es),effs,t)
ensures name in extFunTypes
ensures |extFunTypes[name].args| == |es|
ensures forall i | 0 <= i < |es| :: Typesafe(es[i],effs,extFunTypes[name].args[i])
ensures extFunTypes[name].ret == t
{
assert TC.inferCall(Call(name,es),name,es,effs).Ok?;
InferCallArgsSound(Call(name,es),name,es,extFunTypes[name].args,effs);
assert extFunTypes[name].check.Some? ==> extFunTypes[name].check.value(es).Ok?;
assert forall i | 0 <= i < |es| :: Typesafe(es[i],effs,extFunTypes[name].args[i]);
assert extFunTypes[name].ret == t;
}
| lemma TypesafeCallSemantics(name: base.Name, es: seq<Expr>, effs: Effects, t: Type)
requires Typesafe(Call(name,es),effs,t)
ensures name in extFunTypes
ensures |extFunTypes[name].args| == |es|
ensures forall i | 0 <= i < |es| :: Typesafe(es[i],effs,extFunTypes[name].args[i])
ensures extFunTypes[name].ret == t
{
InferCallArgsSound(Call(name,es),name,es,extFunTypes[name].args,effs);
assert extFunTypes[name].check.Some? ==> extFunTypes[name].check.value(es).Ok?;
assert forall i | 0 <= i < |es| :: Typesafe(es[i],effs,extFunTypes[name].args[i]);
assert extFunTypes[name].ret == t;
}
|
cedar | 810 | ./cedar-dafny/validation/thm/soundness.dfy | SoundInSetMemberFalse_fix_810.dfy | SoundInSetMemberFalse | lemma SoundInSetMemberFalse(e1: Expr, ei2s: seq<Expr>, i: nat, effs: Effects)
decreases BinaryApp(BinaryOp.In,e1,Expr.Set(ei2s)) , 0 , Expr.Set(ei2s) , 0
requires WellFormedRequestAndStore()
requires EffectsInvariant(effs)
requires WellTyped(BinaryApp(BinaryOp.In,e1,Expr.Set(ei2s)),effs)
requires getType(BinaryApp(BinaryOp.In,e1,Expr.Set(ei2s)),effs) == Type.Bool(False)
requires !reqty.isUnspecifiedVar(e1)
requires 0 <= i < |ei2s|
ensures IsFalse(r,s,BinaryApp(BinaryOp.In,e1,ei2s[i]))
{
// Reestablishing things we had at the call site in `SoundIn`.
var e2 := Expr.Set(ei2s);
assert TC.inferIn(BinaryApp(BinaryOp.In,e1,e2),e1,e2,effs) == types.Ok(Type.Bool(False));
assert TC.ensureEntityType(e1,effs).Ok?;
var t1 := getType(e1,effs);
var euids2 :- assert TC.tryGetEUIDs(e2);
var ets2 := set u <- euids2 :: u.ty;
// New proof.
var u2 :- assert TC.tryGetEUID(ei2s[i]);
assert u2 in euids2;
match e1 {
case Var(v1) =>
var et1 :- assert TC.getPrincipalOrResource(v1);
assert t1 == Type.Entity(EntityLUB({et1}));
assert IsSafe(r,s,Var(v1),t1) by { Sound(e1,t1,effs); }
assert !ets.possibleDescendantOf(et1,u2.ty);
reveal WellFormedRequestAndStore();
InSingleFalseEntityTypeAndLiteral(r,s,e1,et1,u2);
case PrimitiveLit(EntityUID(u1)) =>
if isAction(u1.ty) {
assert !acts.descendantOfSet(u1,euids2);
assert !acts.descendantOf(u1,u2);
} else {
assert !ets.possibleDescendantOfSet(u1.ty,ets2);
assert !ets.possibleDescendantOf(u1.ty,u2.ty);
}
reveal WellFormedRequestAndStore();
InSingleFalseLiterals(r,s,u1,u2);
}
}
| lemma SoundInSetMemberFalse(e1: Expr, ei2s: seq<Expr>, i: nat, effs: Effects)
decreases BinaryApp(BinaryOp.In,e1,Expr.Set(ei2s)) , 0 , Expr.Set(ei2s) , 0
requires WellFormedRequestAndStore()
requires EffectsInvariant(effs)
requires WellTyped(BinaryApp(BinaryOp.In,e1,Expr.Set(ei2s)),effs)
requires getType(BinaryApp(BinaryOp.In,e1,Expr.Set(ei2s)),effs) == Type.Bool(False)
requires !reqty.isUnspecifiedVar(e1)
requires 0 <= i < |ei2s|
ensures IsFalse(r,s,BinaryApp(BinaryOp.In,e1,ei2s[i]))
{
// Reestablishing things we had at the call site in `SoundIn`.
var e2 := Expr.Set(ei2s);
assert TC.inferIn(BinaryApp(BinaryOp.In,e1,e2),e1,e2,effs) == types.Ok(Type.Bool(False));
var t1 := getType(e1,effs);
var euids2 :- assert TC.tryGetEUIDs(e2);
var ets2 := set u <- euids2 :: u.ty;
// New proof.
var u2 :- assert TC.tryGetEUID(ei2s[i]);
assert u2 in euids2;
match e1 {
case Var(v1) =>
var et1 :- assert TC.getPrincipalOrResource(v1);
assert t1 == Type.Entity(EntityLUB({et1}));
assert IsSafe(r,s,Var(v1),t1) by { Sound(e1,t1,effs); }
assert !ets.possibleDescendantOf(et1,u2.ty);
reveal WellFormedRequestAndStore();
InSingleFalseEntityTypeAndLiteral(r,s,e1,et1,u2);
case PrimitiveLit(EntityUID(u1)) =>
if isAction(u1.ty) {
assert !acts.descendantOfSet(u1,euids2);
assert !acts.descendantOf(u1,u2);
} else {
assert !ets.possibleDescendantOfSet(u1.ty,ets2);
assert !ets.possibleDescendantOf(u1.ty,u2.ty);
}
reveal WellFormedRequestAndStore();
InSingleFalseLiterals(r,s,u1,u2);
}
}
|
cedar | 7 | ./cedar-dafny/validation/subtyping.dfy | LubIsUB_fix_7.dfy | LubIsUB | lemma LubIsUB(t1: Type, t2: Type, t: Type, m: ValidationMode)
requires lubOpt(t1,t2,m) == Ok(t)
ensures subty(t1,t,m)
ensures subty(t2,t,m)
{
match (t1,t2,t) {
case (Never,_,_) => assert t2 == t; SubtyRefl(t);
case (_,Never,_) => assert t1 == t; SubtyRefl(t);
case (Int,Int,Int) =>
case (String,String,String) =>
case(Bool(b1),Bool(b2),Bool(bt)) =>
case (Entity(e1),Entity(e2),Entity(e)) =>
case (Set(t1'),Set(t2'),Set(t')) => LubIsUB(t1',t2',t',m);
case(Record(rt1'),Record(rt2'),Record(rt')) =>
assert rt'.attrs.Keys <= rt1'.attrs.Keys;
assert rt'.attrs.Keys <= rt2'.attrs.Keys;
assert subty(Type.Record(rt1'),Type.Record(rt'),m) by {
forall k | k in rt'.attrs.Keys
ensures subtyAttrType(rt1'.attrs[k],rt'.attrs[k],m)
{
assert rt'.attrs[k] == lubAttrType(rt1'.attrs[k],rt2'.attrs[k],m);
assert lubOpt(rt1'.attrs[k].ty,rt2'.attrs[k].ty,m) == Ok(rt'.attrs[k].ty);
LubIsUB(rt1'.attrs[k].ty,rt2'.attrs[k].ty,rt'.attrs[k].ty,m);
}
}
assert subty(Type.Record(rt2'),Type.Record(rt'),m) by {
forall k | k in rt'.attrs.Keys
ensures subtyAttrType(rt2'.attrs[k],rt'.attrs[k],m)
{
assert rt'.attrs[k] == lubAttrType(rt1'.attrs[k],rt2'.attrs[k],m);
assert lubOpt(rt1'.attrs[k].ty,rt2'.attrs[k].ty,m) == Ok(rt'.attrs[k].ty);
LubIsUB(rt1'.attrs[k].ty,rt2'.attrs[k].ty,rt'.attrs[k].ty,m);
}
}
case (Extension(n1),Extension(n2),Extension(n)) =>
}
}
| lemma LubIsUB(t1: Type, t2: Type, t: Type, m: ValidationMode)
requires lubOpt(t1,t2,m) == Ok(t)
ensures subty(t1,t,m)
ensures subty(t2,t,m)
{
match (t1,t2,t) {
case (Never,_,_) => case (_,Never,_) => assert t1 == t; SubtyRefl(t);
case (Int,Int,Int) =>
case (String,String,String) =>
case(Bool(b1),Bool(b2),Bool(bt)) =>
case (Entity(e1),Entity(e2),Entity(e)) =>
case (Set(t1'),Set(t2'),Set(t')) => LubIsUB(t1',t2',t',m);
case(Record(rt1'),Record(rt2'),Record(rt')) =>
assert rt'.attrs.Keys <= rt1'.attrs.Keys;
assert rt'.attrs.Keys <= rt2'.attrs.Keys;
assert subty(Type.Record(rt1'),Type.Record(rt'),m) by {
forall k | k in rt'.attrs.Keys
ensures subtyAttrType(rt1'.attrs[k],rt'.attrs[k],m)
{
assert rt'.attrs[k] == lubAttrType(rt1'.attrs[k],rt2'.attrs[k],m);
assert lubOpt(rt1'.attrs[k].ty,rt2'.attrs[k].ty,m) == Ok(rt'.attrs[k].ty);
LubIsUB(rt1'.attrs[k].ty,rt2'.attrs[k].ty,rt'.attrs[k].ty,m);
}
}
assert subty(Type.Record(rt2'),Type.Record(rt'),m) by {
forall k | k in rt'.attrs.Keys
ensures subtyAttrType(rt2'.attrs[k],rt'.attrs[k],m)
{
assert rt'.attrs[k] == lubAttrType(rt1'.attrs[k],rt2'.attrs[k],m);
assert lubOpt(rt1'.attrs[k].ty,rt2'.attrs[k].ty,m) == Ok(rt'.attrs[k].ty);
LubIsUB(rt1'.attrs[k].ty,rt2'.attrs[k].ty,rt'.attrs[k].ty,m);
}
}
case (Extension(n1),Extension(n2),Extension(n)) =>
}
}
|
vmc | 1 | ./src/Math/Helper.dfy | DivModAddDenominator_fix_1.dfy | DivModAddDenominator | lemma DivModAddDenominator(n: nat, m: nat)
requires m > 0
ensures (n + m) / m == n / m + 1
ensures (n + m) % m == n % m
{
var zp := (n + m) / m - n / m - 1;
assert 0 == m * zp + ((n + m) % m) - (n % m);
}
| lemma DivModAddDenominator(n: nat, m: nat)
requires m > 0
ensures (n + m) / m == n / m + 1
ensures (n + m) % m == n % m
{
var zp := (n + m) / m - n / m - 1;
}
|
vmc | 4 | ./src/Math/Rationals.dfy | FloorIsCorrect_fix_4.dfy | FloorIsCorrect | lemma FloorIsCorrect(r: Rational)
ensures Floor(r) == ToReal(r).Floor
{
var floor := r.numer / r.denom;
var multiple := floor * r.denom;
assert r.numer == multiple + r.numer % r.denom;
var nextMultiple := multiple + r.denom;
assert Floor(r) as real <= ToReal(r);
assert ToReal(r) < Floor(r) as real + 1.0 by {
assert r.numer < nextMultiple;
calc {
ToReal(r);
==
r.numer as real / r.denom as real;
< { DivStrictlyMonotonic(r.denom as real, r.numer as real, nextMultiple as real); }
nextMultiple as real / r.denom as real;
==
(floor + 1) as real;
==
Floor(r) as real + 1.0;
}
}
}
| lemma FloorIsCorrect(r: Rational)
ensures Floor(r) == ToReal(r).Floor
{
var floor := r.numer / r.denom;
var multiple := floor * r.denom;
assert r.numer == multiple + r.numer % r.denom;
var nextMultiple := multiple + r.denom;
assert ToReal(r) < Floor(r) as real + 1.0 by {
assert r.numer < nextMultiple;
calc {
ToReal(r);
==
r.numer as real / r.denom as real;
< { DivStrictlyMonotonic(r.denom as real, r.numer as real, nextMultiple as real); }
nextMultiple as real / r.denom as real;
==
(floor + 1) as real;
==
Floor(r) as real + 1.0;
}
}
}
|
vmc | 5 | ./src/Math/Rationals.dfy | FloorIsCorrect_fix_5.dfy | FloorIsCorrect | lemma FloorIsCorrect(r: Rational)
ensures Floor(r) == ToReal(r).Floor
{
var floor := r.numer / r.denom;
var multiple := floor * r.denom;
assert r.numer == multiple + r.numer % r.denom;
var nextMultiple := multiple + r.denom;
assert Floor(r) as real <= ToReal(r);
assert ToReal(r) < Floor(r) as real + 1.0 by {
assert r.numer < nextMultiple;
calc {
ToReal(r);
==
r.numer as real / r.denom as real;
< { DivStrictlyMonotonic(r.denom as real, r.numer as real, nextMultiple as real); }
nextMultiple as real / r.denom as real;
==
(floor + 1) as real;
==
Floor(r) as real + 1.0;
}
}
}
| lemma FloorIsCorrect(r: Rational)
ensures Floor(r) == ToReal(r).Floor
{
var floor := r.numer / r.denom;
var multiple := floor * r.denom;
assert r.numer == multiple + r.numer % r.denom;
var nextMultiple := multiple + r.denom;
assert Floor(r) as real <= ToReal(r);
assert ToReal(r) < Floor(r) as real + 1.0 by {
calc {
ToReal(r);
==
r.numer as real / r.denom as real;
< { DivStrictlyMonotonic(r.denom as real, r.numer as real, nextMultiple as real); }
nextMultiple as real / r.denom as real;
==
(floor + 1) as real;
==
Floor(r) as real + 1.0;
}
}
}
|
vmc | 6 | ./src/Math/Measures.dfy | PosCountAddImpliesAdd_fix_6.dfy | PosCountAddImpliesAdd | lemma PosCountAddImpliesAdd<T(!new)>(eventSpace: iset<iset<T>>, Prob: iset<T> -> real)
requires IsSigmaAlgebra(eventSpace)
requires IsPositive(eventSpace, Prob)
requires IsCountablyAdditive(eventSpace, Prob)
ensures IsAdditive(eventSpace, Prob)
{
forall e1, e2 | e1 in eventSpace && e2 in eventSpace && e1 * e2 == iset{} ensures Prob(e1) + Prob(e2) == Prob(e1 + e2) {
var f : nat -> iset<T> := (n: nat) => if n == 0 then e1 else if n == 1 then e2 else iset{};
assert CountableUnion(f) == e1 + e2;
assert CountableSum((n: nat) => Prob(f(n))) == Prob(e1) + Prob(e2) by {
assert CountableSum((n: nat) => Prob(f(n)), 2) == 0.0 by {
CountableSumOfZeroesIsZero((n: nat) => Prob(f(n)), 2);
}
calc {
CountableSum((n: nat) => Prob(f(n)))
==
Prob(f(0)) + CountableSum((n: nat) => Prob(f(n)), 1)
==
Prob(f(0)) + Prob(f(1)) + CountableSum((n: nat) => Prob(f(n)), 2)
==
Prob(e1) + Prob(e2) + CountableSum((n: nat) => Prob(f(n)), 2)
==
Prob(e1) + Prob(e2);
}
}
assert Prob(CountableUnion(f)) == CountableSum((n: nat) => Prob(f(n))) by {
assert IsCountablyAdditive(eventSpace, Prob);
}
assert Prob(e1 + e2) == Prob(e1) + Prob(e2);
}
}
| lemma PosCountAddImpliesAdd<T(!new)>(eventSpace: iset<iset<T>>, Prob: iset<T> -> real)
requires IsSigmaAlgebra(eventSpace)
requires IsPositive(eventSpace, Prob)
requires IsCountablyAdditive(eventSpace, Prob)
ensures IsAdditive(eventSpace, Prob)
{
forall e1, e2 | e1 in eventSpace && e2 in eventSpace && e1 * e2 == iset{} ensures Prob(e1) + Prob(e2) == Prob(e1 + e2) {
var f : nat -> iset<T> := (n: nat) => if n == 0 then e1 else if n == 1 then e2 else iset{};
assert CountableSum((n: nat) => Prob(f(n))) == Prob(e1) + Prob(e2) by {
assert CountableSum((n: nat) => Prob(f(n)), 2) == 0.0 by {
CountableSumOfZeroesIsZero((n: nat) => Prob(f(n)), 2);
}
calc {
CountableSum((n: nat) => Prob(f(n)))
==
Prob(f(0)) + CountableSum((n: nat) => Prob(f(n)), 1)
==
Prob(f(0)) + Prob(f(1)) + CountableSum((n: nat) => Prob(f(n)), 2)
==
Prob(e1) + Prob(e2) + CountableSum((n: nat) => Prob(f(n)), 2)
==
Prob(e1) + Prob(e2);
}
}
assert Prob(CountableUnion(f)) == CountableSum((n: nat) => Prob(f(n))) by {
assert IsCountablyAdditive(eventSpace, Prob);
}
assert Prob(e1 + e2) == Prob(e1) + Prob(e2);
}
}
|
vmc | 11 | ./src/Math/Analysis/Limits.dfy | LimitIsMultiplicative_fix_11.dfy | LimitIsMultiplicative | lemma LimitIsMultiplicative(sequence1: nat -> real, limit1: real, sequence2: nat -> real, limit2: real)
requires ConvergesTo(sequence1, limit1)
requires ConvergesTo(sequence2, limit2)
ensures ConvergesTo(Sequences.Mul(sequence1, sequence2), limit1 * limit2)
{
var bound1 := BoundOfConvergentSequence(sequence1, limit1);
var bound2 := BoundOfConvergentSequence(sequence2, limit2);
var productSequence := Sequences.Mul(sequence1, sequence2);
var productLimit := limit1 * limit2;
forall epsilon: real | epsilon > 0.0 ensures ExistsCloseSuffix(productSequence, productLimit, epsilon) {
var epsilon1 := epsilon / 2.0 / RealArith.Max(RealArith.Abs(limit2), 1.0);
var epsilon2 := epsilon / 2.0 / RealArith.Max(bound1, 1.0);
assert ExistsCloseSuffix(sequence1, limit1, epsilon1);
var N1: nat :| SuffixIsClose(sequence1, limit1, epsilon1, N1);
assert ExistsCloseSuffix(sequence2, limit2, epsilon2);
var N2: nat :| SuffixIsClose(sequence2, limit2, epsilon2, N2);
var N: nat :| N >= N1 && N >= N2;
assert SuffixIsClose(productSequence, productLimit, epsilon, N) by {
forall n: nat | n >= N ensures RealArith.Dist(productSequence(n), productLimit) < epsilon {
var s1 := sequence1(n);
var s2 := sequence2(n);
calc {
RealArith.Dist(productSequence(n), productLimit);
== { assert productSequence(n) == sequence1(n) * sequence2(n) == s1 * s2; }
RealArith.Dist(s1 * s2, limit1 * limit2);
< { LimitIsMultiplicativeSuffixHelper(s1, limit1, s2, limit2, bound1, epsilon); }
epsilon;
}
}
}
}
}
| lemma LimitIsMultiplicative(sequence1: nat -> real, limit1: real, sequence2: nat -> real, limit2: real)
requires ConvergesTo(sequence1, limit1)
requires ConvergesTo(sequence2, limit2)
ensures ConvergesTo(Sequences.Mul(sequence1, sequence2), limit1 * limit2)
{
var bound1 := BoundOfConvergentSequence(sequence1, limit1);
var bound2 := BoundOfConvergentSequence(sequence2, limit2);
var productSequence := Sequences.Mul(sequence1, sequence2);
var productLimit := limit1 * limit2;
forall epsilon: real | epsilon > 0.0 ensures ExistsCloseSuffix(productSequence, productLimit, epsilon) {
var epsilon1 := epsilon / 2.0 / RealArith.Max(RealArith.Abs(limit2), 1.0);
var epsilon2 := epsilon / 2.0 / RealArith.Max(bound1, 1.0);
var N1: nat :| SuffixIsClose(sequence1, limit1, epsilon1, N1);
assert ExistsCloseSuffix(sequence2, limit2, epsilon2);
var N2: nat :| SuffixIsClose(sequence2, limit2, epsilon2, N2);
var N: nat :| N >= N1 && N >= N2;
assert SuffixIsClose(productSequence, productLimit, epsilon, N) by {
forall n: nat | n >= N ensures RealArith.Dist(productSequence(n), productLimit) < epsilon {
var s1 := sequence1(n);
var s2 := sequence2(n);
calc {
RealArith.Dist(productSequence(n), productLimit);
== { assert productSequence(n) == sequence1(n) * sequence2(n) == s1 * s2; }
RealArith.Dist(s1 * s2, limit1 * limit2);
< { LimitIsMultiplicativeSuffixHelper(s1, limit1, s2, limit2, bound1, epsilon); }
epsilon;
}
}
}
}
}
|
vmc | 12 | ./src/Math/Analysis/Limits.dfy | LimitIsMultiplicative_fix_12.dfy | LimitIsMultiplicative | lemma LimitIsMultiplicative(sequence1: nat -> real, limit1: real, sequence2: nat -> real, limit2: real)
requires ConvergesTo(sequence1, limit1)
requires ConvergesTo(sequence2, limit2)
ensures ConvergesTo(Sequences.Mul(sequence1, sequence2), limit1 * limit2)
{
var bound1 := BoundOfConvergentSequence(sequence1, limit1);
var bound2 := BoundOfConvergentSequence(sequence2, limit2);
var productSequence := Sequences.Mul(sequence1, sequence2);
var productLimit := limit1 * limit2;
forall epsilon: real | epsilon > 0.0 ensures ExistsCloseSuffix(productSequence, productLimit, epsilon) {
var epsilon1 := epsilon / 2.0 / RealArith.Max(RealArith.Abs(limit2), 1.0);
var epsilon2 := epsilon / 2.0 / RealArith.Max(bound1, 1.0);
assert ExistsCloseSuffix(sequence1, limit1, epsilon1);
var N1: nat :| SuffixIsClose(sequence1, limit1, epsilon1, N1);
assert ExistsCloseSuffix(sequence2, limit2, epsilon2);
var N2: nat :| SuffixIsClose(sequence2, limit2, epsilon2, N2);
var N: nat :| N >= N1 && N >= N2;
assert SuffixIsClose(productSequence, productLimit, epsilon, N) by {
forall n: nat | n >= N ensures RealArith.Dist(productSequence(n), productLimit) < epsilon {
var s1 := sequence1(n);
var s2 := sequence2(n);
calc {
RealArith.Dist(productSequence(n), productLimit);
== { assert productSequence(n) == sequence1(n) * sequence2(n) == s1 * s2; }
RealArith.Dist(s1 * s2, limit1 * limit2);
< { LimitIsMultiplicativeSuffixHelper(s1, limit1, s2, limit2, bound1, epsilon); }
epsilon;
}
}
}
}
}
| lemma LimitIsMultiplicative(sequence1: nat -> real, limit1: real, sequence2: nat -> real, limit2: real)
requires ConvergesTo(sequence1, limit1)
requires ConvergesTo(sequence2, limit2)
ensures ConvergesTo(Sequences.Mul(sequence1, sequence2), limit1 * limit2)
{
var bound1 := BoundOfConvergentSequence(sequence1, limit1);
var bound2 := BoundOfConvergentSequence(sequence2, limit2);
var productSequence := Sequences.Mul(sequence1, sequence2);
var productLimit := limit1 * limit2;
forall epsilon: real | epsilon > 0.0 ensures ExistsCloseSuffix(productSequence, productLimit, epsilon) {
var epsilon1 := epsilon / 2.0 / RealArith.Max(RealArith.Abs(limit2), 1.0);
var epsilon2 := epsilon / 2.0 / RealArith.Max(bound1, 1.0);
assert ExistsCloseSuffix(sequence1, limit1, epsilon1);
var N1: nat :| SuffixIsClose(sequence1, limit1, epsilon1, N1);
var N2: nat :| SuffixIsClose(sequence2, limit2, epsilon2, N2);
var N: nat :| N >= N1 && N >= N2;
assert SuffixIsClose(productSequence, productLimit, epsilon, N) by {
forall n: nat | n >= N ensures RealArith.Dist(productSequence(n), productLimit) < epsilon {
var s1 := sequence1(n);
var s2 := sequence2(n);
calc {
RealArith.Dist(productSequence(n), productLimit);
== { assert productSequence(n) == sequence1(n) * sequence2(n) == s1 * s2; }
RealArith.Dist(s1 * s2, limit1 * limit2);
< { LimitIsMultiplicativeSuffixHelper(s1, limit1, s2, limit2, bound1, epsilon); }
epsilon;
}
}
}
}
}
|
vmc | 14 | ./src/Math/Analysis/Limits.dfy | LimitOfInverse_fix_14.dfy | LimitOfInverse | lemma LimitOfInverse(sequence: nat -> real, limit: real)
requires forall n: nat :: sequence(n) != 0.0
requires limit != 0.0
requires ConvergesTo(sequence, limit)
ensures ConvergesTo(Sequences.Inverse(sequence), 1.0 / limit)
{
var invSeq := Sequences.Inverse(sequence);
var invLimit := 1.0 / limit;
forall epsilon: real | epsilon > 0.0 ensures ExistsCloseSuffix(invSeq, invLimit, epsilon) {
var limitSquared := limit * limit;
var epsilon' := RealArith.Min(RealArith.Abs(limit) / 2.0, (epsilon * limitSquared) / 2.0);
assert ExistsCloseSuffix(sequence, limit, epsilon');
var N: nat :| SuffixIsClose(sequence, limit, epsilon', N);
assert SuffixIsClose(invSeq, invLimit, epsilon, N) by {
forall n: nat | n >= N ensures RealArith.Dist(invSeq(n), invLimit) < epsilon {
var s := sequence(n);
calc {
RealArith.Dist(invSeq(n), invLimit);
== { assert invSeq(n) == 1.0 / s; }
RealArith.Dist(1.0 / s, 1.0 / limit);
== { LimitOfInverseHelper1(s, limit); }
RealArith.Dist(s, limit) / RealArith.Abs(limit) / RealArith.Abs(s);
<= { LimitOfInverseHelper2(s, limit); }
2.0 * RealArith.Dist(s, limit) / limitSquared;
< { RealArith.DivisionIsMonotonicStrict(2.0 * RealArith.Dist(s, limit), 2.0 * epsilon', limitSquared); }
2.0 * epsilon' / limitSquared;
<= { RealArith.DivisionIsMonotonic(2.0 * epsilon', 2.0 * (epsilon * limitSquared) / 2.0, limitSquared); }
2.0 * (epsilon * limitSquared) / 2.0 / limitSquared;
== { assert 2.0 * (epsilon * limitSquared) / 2.0 == epsilon * limitSquared; }
(epsilon * limitSquared) / limitSquared;
==
epsilon;
}
}
}
}
}
| lemma LimitOfInverse(sequence: nat -> real, limit: real)
requires forall n: nat :: sequence(n) != 0.0
requires limit != 0.0
requires ConvergesTo(sequence, limit)
ensures ConvergesTo(Sequences.Inverse(sequence), 1.0 / limit)
{
var invSeq := Sequences.Inverse(sequence);
var invLimit := 1.0 / limit;
forall epsilon: real | epsilon > 0.0 ensures ExistsCloseSuffix(invSeq, invLimit, epsilon) {
var limitSquared := limit * limit;
var epsilon' := RealArith.Min(RealArith.Abs(limit) / 2.0, (epsilon * limitSquared) / 2.0);
var N: nat :| SuffixIsClose(sequence, limit, epsilon', N);
assert SuffixIsClose(invSeq, invLimit, epsilon, N) by {
forall n: nat | n >= N ensures RealArith.Dist(invSeq(n), invLimit) < epsilon {
var s := sequence(n);
calc {
RealArith.Dist(invSeq(n), invLimit);
== { assert invSeq(n) == 1.0 / s; }
RealArith.Dist(1.0 / s, 1.0 / limit);
== { LimitOfInverseHelper1(s, limit); }
RealArith.Dist(s, limit) / RealArith.Abs(limit) / RealArith.Abs(s);
<= { LimitOfInverseHelper2(s, limit); }
2.0 * RealArith.Dist(s, limit) / limitSquared;
< { RealArith.DivisionIsMonotonicStrict(2.0 * RealArith.Dist(s, limit), 2.0 * epsilon', limitSquared); }
2.0 * epsilon' / limitSquared;
<= { RealArith.DivisionIsMonotonic(2.0 * epsilon', 2.0 * (epsilon * limitSquared) / 2.0, limitSquared); }
2.0 * (epsilon * limitSquared) / 2.0 / limitSquared;
== { assert 2.0 * (epsilon * limitSquared) / 2.0 == epsilon * limitSquared; }
(epsilon * limitSquared) / limitSquared;
==
epsilon;
}
}
}
}
}
|
vmc | 15 | ./src/Math/Analysis/Limits.dfy | ConstantSequenceConverges_fix_15.dfy | ConstantSequenceConverges | lemma ConstantSequenceConverges(constant: real)
ensures ConvergesTo(Sequences.Constant(constant), constant)
{
forall epsilon: real | epsilon > 0.0 ensures ExistsCloseSuffix(Sequences.Constant(constant), constant, epsilon) {
assert SuffixIsClose(Sequences.Constant(constant), constant, epsilon, 0);
}
}
| lemma ConstantSequenceConverges(constant: real)
ensures ConvergesTo(Sequences.Constant(constant), constant)
{
forall epsilon: real | epsilon > 0.0 ensures ExistsCloseSuffix(Sequences.Constant(constant), constant, epsilon) {
}
}
|
vmc | 16 | ./src/Math/Analysis/Limits.dfy | LimitIsUnique_fix_16.dfy | LimitIsUnique | lemma LimitIsUnique(sequence: nat -> real, limit1: real, limit2: real)
requires ConvergesTo(sequence, limit1)
requires ConvergesTo(sequence, limit2)
ensures limit1 == limit2
{
if ConvergesTo(sequence, limit1) && ConvergesTo(sequence, limit2) && limit1 != limit2 {
var epsilon := RealArith.Dist(limit1, limit2) / 2.0;
assert ExistsCloseSuffix(sequence, limit1, epsilon);
var N1 : nat :| SuffixIsClose(sequence, limit1, epsilon, N1);
assert ExistsCloseSuffix(sequence, limit2, epsilon);
var N2 : nat :| SuffixIsClose(sequence, limit2, epsilon, N2);
var N : nat :| N >= N1 && N >= N2;
calc {
RealArith.Dist(limit1, limit2);
<= { RealArith.TriangleInequality(limit1, sequence(N), limit2); }
RealArith.Dist(limit1, sequence(N)) + RealArith.Dist(sequence(N), limit2);
<
epsilon + epsilon;
==
epsilon * 2.0;
==
RealArith.Dist(limit1, limit2);
}
}
}
| lemma LimitIsUnique(sequence: nat -> real, limit1: real, limit2: real)
requires ConvergesTo(sequence, limit1)
requires ConvergesTo(sequence, limit2)
ensures limit1 == limit2
{
if ConvergesTo(sequence, limit1) && ConvergesTo(sequence, limit2) && limit1 != limit2 {
var epsilon := RealArith.Dist(limit1, limit2) / 2.0;
var N1 : nat :| SuffixIsClose(sequence, limit1, epsilon, N1);
assert ExistsCloseSuffix(sequence, limit2, epsilon);
var N2 : nat :| SuffixIsClose(sequence, limit2, epsilon, N2);
var N : nat :| N >= N1 && N >= N2;
calc {
RealArith.Dist(limit1, limit2);
<= { RealArith.TriangleInequality(limit1, sequence(N), limit2); }
RealArith.Dist(limit1, sequence(N)) + RealArith.Dist(sequence(N), limit2);
<
epsilon + epsilon;
==
epsilon * 2.0;
==
RealArith.Dist(limit1, limit2);
}
}
}
|
vmc | 17 | ./src/Math/Analysis/Limits.dfy | LimitIsUnique_fix_17.dfy | LimitIsUnique | lemma LimitIsUnique(sequence: nat -> real, limit1: real, limit2: real)
requires ConvergesTo(sequence, limit1)
requires ConvergesTo(sequence, limit2)
ensures limit1 == limit2
{
if ConvergesTo(sequence, limit1) && ConvergesTo(sequence, limit2) && limit1 != limit2 {
var epsilon := RealArith.Dist(limit1, limit2) / 2.0;
assert ExistsCloseSuffix(sequence, limit1, epsilon);
var N1 : nat :| SuffixIsClose(sequence, limit1, epsilon, N1);
assert ExistsCloseSuffix(sequence, limit2, epsilon);
var N2 : nat :| SuffixIsClose(sequence, limit2, epsilon, N2);
var N : nat :| N >= N1 && N >= N2;
calc {
RealArith.Dist(limit1, limit2);
<= { RealArith.TriangleInequality(limit1, sequence(N), limit2); }
RealArith.Dist(limit1, sequence(N)) + RealArith.Dist(sequence(N), limit2);
<
epsilon + epsilon;
==
epsilon * 2.0;
==
RealArith.Dist(limit1, limit2);
}
}
}
| lemma LimitIsUnique(sequence: nat -> real, limit1: real, limit2: real)
requires ConvergesTo(sequence, limit1)
requires ConvergesTo(sequence, limit2)
ensures limit1 == limit2
{
if ConvergesTo(sequence, limit1) && ConvergesTo(sequence, limit2) && limit1 != limit2 {
var epsilon := RealArith.Dist(limit1, limit2) / 2.0;
assert ExistsCloseSuffix(sequence, limit1, epsilon);
var N1 : nat :| SuffixIsClose(sequence, limit1, epsilon, N1);
var N2 : nat :| SuffixIsClose(sequence, limit2, epsilon, N2);
var N : nat :| N >= N1 && N >= N2;
calc {
RealArith.Dist(limit1, limit2);
<= { RealArith.TriangleInequality(limit1, sequence(N), limit2); }
RealArith.Dist(limit1, sequence(N)) + RealArith.Dist(sequence(N), limit2);
<
epsilon + epsilon;
==
epsilon * 2.0;
==
RealArith.Dist(limit1, limit2);
}
}
}
|
vmc | 18 | ./src/Math/Analysis/Limits.dfy | BoundOfConvergentSequence_fix_18.dfy | BoundOfConvergentSequence | lemma BoundOfConvergentSequence(sequence: nat -> real, limit: real) returns (bound: real)
requires ConvergesTo(sequence, limit)
ensures Sequences.IsBounded(sequence, bound)
ensures bound > 0.0
{
assert ExistsCloseSuffix(sequence, limit, 1.0);
var N: nat :| SuffixIsClose(sequence, limit, 1.0, N);
bound := RealArith.Abs(limit) + 1.0;
for n := 0 to N
invariant bound >= RealArith.Abs(limit) + 1.0
invariant forall i: nat | i < n :: RealArith.Abs(sequence(i)) < bound
{
if RealArith.Abs(sequence(n)) >= bound {
bound := RealArith.Abs(sequence(n)) + 1.0;
}
}
assert forall n: nat | n < N :: RealArith.Abs(sequence(n)) < bound;
forall n: nat ensures RealArith.Abs(sequence(n)) < bound {
if n < N {
assert RealArith.Abs(sequence(n)) < bound;
} else {
assert RealArith.Abs(sequence(n)) < RealArith.Abs(limit) + 1.0 <= bound;
}
}
}
| lemma BoundOfConvergentSequence(sequence: nat -> real, limit: real) returns (bound: real)
requires ConvergesTo(sequence, limit)
ensures Sequences.IsBounded(sequence, bound)
ensures bound > 0.0
{
var N: nat :| SuffixIsClose(sequence, limit, 1.0, N);
bound := RealArith.Abs(limit) + 1.0;
for n := 0 to N
invariant bound >= RealArith.Abs(limit) + 1.0
invariant forall i: nat | i < n :: RealArith.Abs(sequence(i)) < bound
{
if RealArith.Abs(sequence(n)) >= bound {
bound := RealArith.Abs(sequence(n)) + 1.0;
}
}
assert forall n: nat | n < N :: RealArith.Abs(sequence(n)) < bound;
forall n: nat ensures RealArith.Abs(sequence(n)) < bound {
if n < N {
assert RealArith.Abs(sequence(n)) < bound;
} else {
assert RealArith.Abs(sequence(n)) < RealArith.Abs(limit) + 1.0 <= bound;
}
}
}
|
vmc | 22 | ./src/Math/Analysis/Limits.dfy | LimitIsAdditive_fix_22.dfy | LimitIsAdditive | lemma LimitIsAdditive(sequence1: nat -> real, limit1: real, sequence2: nat -> real, limit2: real)
requires ConvergesTo(sequence1, limit1)
requires ConvergesTo(sequence2, limit2)
ensures ConvergesTo(Sequences.Add(sequence1, sequence2), limit1 + limit2)
{
var sumSequence := Sequences.Add(sequence1, sequence2);
var sumLimit := limit1 + limit2;
forall epsilon: real | epsilon > 0.0 ensures ExistsCloseSuffix(sumSequence, sumLimit, epsilon) {
assert ExistsCloseSuffix(sequence1, limit1, epsilon / 2.0);
var N1: nat :| SuffixIsClose(sequence1, limit1, epsilon / 2.0, N1);
assert ExistsCloseSuffix(sequence2, limit2, epsilon / 2.0);
var N2: nat :| SuffixIsClose(sequence2, limit2, epsilon / 2.0, N2);
var N: nat :| N >= N1 && N >= N2;
assert SuffixIsClose(sumSequence, sumLimit, epsilon, N) by {
forall n: nat | n >= N ensures RealArith.Dist(sumSequence(n), sumLimit) < epsilon {
calc {
RealArith.Dist(sumSequence(n), sumLimit);
==
RealArith.Dist(sequence1(n) + sequence2(n), limit1 + limit2);
<=
RealArith.Dist(sequence1(n), limit1) + RealArith.Dist(sequence2(n), limit2);
<
epsilon / 2.0 + epsilon / 2.0;
==
epsilon;
}
}
}
}
}
| lemma LimitIsAdditive(sequence1: nat -> real, limit1: real, sequence2: nat -> real, limit2: real)
requires ConvergesTo(sequence1, limit1)
requires ConvergesTo(sequence2, limit2)
ensures ConvergesTo(Sequences.Add(sequence1, sequence2), limit1 + limit2)
{
var sumSequence := Sequences.Add(sequence1, sequence2);
var sumLimit := limit1 + limit2;
forall epsilon: real | epsilon > 0.0 ensures ExistsCloseSuffix(sumSequence, sumLimit, epsilon) {
var N1: nat :| SuffixIsClose(sequence1, limit1, epsilon / 2.0, N1);
assert ExistsCloseSuffix(sequence2, limit2, epsilon / 2.0);
var N2: nat :| SuffixIsClose(sequence2, limit2, epsilon / 2.0, N2);
var N: nat :| N >= N1 && N >= N2;
assert SuffixIsClose(sumSequence, sumLimit, epsilon, N) by {
forall n: nat | n >= N ensures RealArith.Dist(sumSequence(n), sumLimit) < epsilon {
calc {
RealArith.Dist(sumSequence(n), sumLimit);
==
RealArith.Dist(sequence1(n) + sequence2(n), limit1 + limit2);
<=
RealArith.Dist(sequence1(n), limit1) + RealArith.Dist(sequence2(n), limit2);
<
epsilon / 2.0 + epsilon / 2.0;
==
epsilon;
}
}
}
}
}
|
vmc | 23 | ./src/Math/Analysis/Limits.dfy | LimitIsAdditive_fix_23.dfy | LimitIsAdditive | lemma LimitIsAdditive(sequence1: nat -> real, limit1: real, sequence2: nat -> real, limit2: real)
requires ConvergesTo(sequence1, limit1)
requires ConvergesTo(sequence2, limit2)
ensures ConvergesTo(Sequences.Add(sequence1, sequence2), limit1 + limit2)
{
var sumSequence := Sequences.Add(sequence1, sequence2);
var sumLimit := limit1 + limit2;
forall epsilon: real | epsilon > 0.0 ensures ExistsCloseSuffix(sumSequence, sumLimit, epsilon) {
assert ExistsCloseSuffix(sequence1, limit1, epsilon / 2.0);
var N1: nat :| SuffixIsClose(sequence1, limit1, epsilon / 2.0, N1);
assert ExistsCloseSuffix(sequence2, limit2, epsilon / 2.0);
var N2: nat :| SuffixIsClose(sequence2, limit2, epsilon / 2.0, N2);
var N: nat :| N >= N1 && N >= N2;
assert SuffixIsClose(sumSequence, sumLimit, epsilon, N) by {
forall n: nat | n >= N ensures RealArith.Dist(sumSequence(n), sumLimit) < epsilon {
calc {
RealArith.Dist(sumSequence(n), sumLimit);
==
RealArith.Dist(sequence1(n) + sequence2(n), limit1 + limit2);
<=
RealArith.Dist(sequence1(n), limit1) + RealArith.Dist(sequence2(n), limit2);
<
epsilon / 2.0 + epsilon / 2.0;
==
epsilon;
}
}
}
}
}
| lemma LimitIsAdditive(sequence1: nat -> real, limit1: real, sequence2: nat -> real, limit2: real)
requires ConvergesTo(sequence1, limit1)
requires ConvergesTo(sequence2, limit2)
ensures ConvergesTo(Sequences.Add(sequence1, sequence2), limit1 + limit2)
{
var sumSequence := Sequences.Add(sequence1, sequence2);
var sumLimit := limit1 + limit2;
forall epsilon: real | epsilon > 0.0 ensures ExistsCloseSuffix(sumSequence, sumLimit, epsilon) {
assert ExistsCloseSuffix(sequence1, limit1, epsilon / 2.0);
var N1: nat :| SuffixIsClose(sequence1, limit1, epsilon / 2.0, N1);
var N2: nat :| SuffixIsClose(sequence2, limit2, epsilon / 2.0, N2);
var N: nat :| N >= N1 && N >= N2;
assert SuffixIsClose(sumSequence, sumLimit, epsilon, N) by {
forall n: nat | n >= N ensures RealArith.Dist(sumSequence(n), sumLimit) < epsilon {
calc {
RealArith.Dist(sumSequence(n), sumLimit);
==
RealArith.Dist(sequence1(n) + sequence2(n), limit1 + limit2);
<=
RealArith.Dist(sequence1(n), limit1) + RealArith.Dist(sequence2(n), limit2);
<
epsilon / 2.0 + epsilon / 2.0;
==
epsilon;
}
}
}
}
}
|
vmc | 24 | ./src/Math/Analysis/Limits.dfy | LimitOfMultiplicationWithZeroSequence_fix_24.dfy | LimitOfMultiplicationWithZeroSequence | lemma LimitOfMultiplicationWithZeroSequence(sequence: nat -> real, bound: real, zeroSeq: nat -> real)
requires Sequences.IsBounded(sequence, bound)
requires ConvergesTo(zeroSeq, 0.0)
ensures ConvergesTo(Sequences.Mul(sequence, zeroSeq), 0.0)
{
var productSequence := Sequences.Mul(sequence, zeroSeq);
forall epsilon: real | epsilon > 0.0 ensures ExistsCloseSuffix(productSequence, 0.0, epsilon) {
var epsilon' := epsilon / RealArith.Max(bound, 1.0);
assert ExistsCloseSuffix(zeroSeq, 0.0, epsilon');
var N :| SuffixIsClose(zeroSeq, 0.0, epsilon', N);
assert SuffixIsClose(productSequence, 0.0, epsilon, N) by {
forall n: nat | n >= N ensures RealArith.Dist(productSequence(n), 0.0) < epsilon {
var s := sequence(n);
var z := zeroSeq(n);
calc {
RealArith.Dist(productSequence(n), 0.0);
==
RealArith.Abs(s * z);
== { RealArith.AbsMul(s, z); }
RealArith.Abs(s) * RealArith.Abs(z);
<= { RealArith.MulMonotonic(RealArith.Abs(s), RealArith.Abs(z), epsilon'); }
RealArith.Abs(s) * epsilon';
< { RealArith.MulMonotonicStrict(epsilon', RealArith.Abs(s), bound); }
bound * epsilon';
<= { RealArith.MulMonotonic(epsilon', bound, RealArith.Max(bound, 1.0)); }
RealArith.Max(bound, 1.0) * epsilon';
==
epsilon;
}
}
}
}
}
| lemma LimitOfMultiplicationWithZeroSequence(sequence: nat -> real, bound: real, zeroSeq: nat -> real)
requires Sequences.IsBounded(sequence, bound)
requires ConvergesTo(zeroSeq, 0.0)
ensures ConvergesTo(Sequences.Mul(sequence, zeroSeq), 0.0)
{
var productSequence := Sequences.Mul(sequence, zeroSeq);
forall epsilon: real | epsilon > 0.0 ensures ExistsCloseSuffix(productSequence, 0.0, epsilon) {
var epsilon' := epsilon / RealArith.Max(bound, 1.0);
var N :| SuffixIsClose(zeroSeq, 0.0, epsilon', N);
assert SuffixIsClose(productSequence, 0.0, epsilon, N) by {
forall n: nat | n >= N ensures RealArith.Dist(productSequence(n), 0.0) < epsilon {
var s := sequence(n);
var z := zeroSeq(n);
calc {
RealArith.Dist(productSequence(n), 0.0);
==
RealArith.Abs(s * z);
== { RealArith.AbsMul(s, z); }
RealArith.Abs(s) * RealArith.Abs(z);
<= { RealArith.MulMonotonic(RealArith.Abs(s), RealArith.Abs(z), epsilon'); }
RealArith.Abs(s) * epsilon';
< { RealArith.MulMonotonicStrict(epsilon', RealArith.Abs(s), bound); }
bound * epsilon';
<= { RealArith.MulMonotonic(epsilon', bound, RealArith.Max(bound, 1.0)); }
RealArith.Max(bound, 1.0) * epsilon';
==
epsilon;
}
}
}
}
}
|
vmc | 26 | ./src/Math/Analysis/Sequences.dfy | OneOverNPlus1IsAntimonotonic_fix_26.dfy | OneOverNPlus1IsAntimonotonic | lemma OneOverNPlus1IsAntimonotonic(m: nat, n: nat)
requires m <= n
ensures OneOverNPlus1(m) >= OneOverNPlus1(n)
{
var mp1 := (m + 1) as real;
var np1 := (n + 1) as real;
assert mp1 > 0.0;
assert np1 > 0.0;
calc {
mp1 <= np1;
==
mp1 / np1 <= 1.0;
==
(1.0 / np1) * mp1 <= (1.0 / mp1) * mp1;
== { RealArith.MultiplicationCancelMonotonic(mp1, 1.0 / np1, 1.0 / mp1); }
1.0 / np1 <= 1.0 / mp1;
==
OneOverNPlus1(m) >= OneOverNPlus1(n);
}
}
| lemma OneOverNPlus1IsAntimonotonic(m: nat, n: nat)
requires m <= n
ensures OneOverNPlus1(m) >= OneOverNPlus1(n)
{
var mp1 := (m + 1) as real;
var np1 := (n + 1) as real;
assert mp1 > 0.0;
calc {
mp1 <= np1;
==
mp1 / np1 <= 1.0;
==
(1.0 / np1) * mp1 <= (1.0 / mp1) * mp1;
== { RealArith.MultiplicationCancelMonotonic(mp1, 1.0 / np1, 1.0 / mp1); }
1.0 / np1 <= 1.0 / mp1;
==
OneOverNPlus1(m) >= OneOverNPlus1(n);
}
}
|
vmc | 27 | ./src/Math/Analysis/Reals.dfy | Sqrt2Exists_fix_27.dfy | Sqrt2Exists | lemma Sqrt2Exists() returns (sqrt2: real)
ensures sqrt2 * sqrt2 == 2.0
{
var lower := iset x: real | x >= 0.0 && Square(x) < 2.0;
var upper := iset x: real | x >= 0.0 && Square(x) > 2.0;
forall x <- lower, y <- upper ensures x < y {
if x >= y {
calc {
2.0;
<
Square(y);
<= { RealArith.MulMonotonic(y, y, x); RealArith.MulMonotonic(x, y, x); }
Square(x);
<
2.0;
}
}
}
assert 1.0 in lower;
assert 2.0 in upper;
sqrt2 := Reals.DedekindCompleteness(lower, upper);
assert 1.0 <= sqrt2 <= 2.0;
assert Square(sqrt2) == 2.0 by {
if sqrt2 * sqrt2 > 2.0 {
var eps := (sqrt2 * sqrt2 - 2.0) / (2.0 * sqrt2);
assert 0.0 < eps < 1.0;
var y := sqrt2 - eps;
assert y * y > 2.0 by {
calc {
y * y;
sqrt2 * sqrt2 - 2.0 * sqrt2 * eps + eps * eps;
sqrt2 * sqrt2 - (sqrt2 * sqrt2 - 2.0) + eps * eps;
2.0 + eps * eps;
>
2.0;
}
}
assert y in upper by {
assert y >= 0.0;
assert y * y > 2.0;
}
assert false;
}
if sqrt2 * sqrt2 < 2.0 {
var eps := (2.0 - sqrt2 * sqrt2) / (2.0 * sqrt2 + 1.0);
assert 0.0 < eps < 1.0;
var y := sqrt2 + eps;
assert y * y < 2.0 by {
calc {
y * y;
sqrt2 * sqrt2 + 2.0 * sqrt2 * eps + eps * eps;
sqrt2 * sqrt2 + (2.0 * sqrt2 + eps) * eps;
< { RealArith.MulMonotonicStrict(eps, 2.0 * sqrt2 + eps, 2.0 * sqrt2 + 1.0); }
sqrt2 * sqrt2 + (2.0 * sqrt2 + 1.0) * eps;
sqrt2 * sqrt2 + (2.0 - sqrt2 * sqrt2);
2.0;
}
}
assert y in lower by {
assert y >= 0.0;
assert y * y < 2.0;
}
assert false;
}
}
}
| lemma Sqrt2Exists() returns (sqrt2: real)
ensures sqrt2 * sqrt2 == 2.0
{
var lower := iset x: real | x >= 0.0 && Square(x) < 2.0;
var upper := iset x: real | x >= 0.0 && Square(x) > 2.0;
forall x <- lower, y <- upper ensures x < y {
if x >= y {
calc {
2.0;
<
Square(y);
<= { RealArith.MulMonotonic(y, y, x); RealArith.MulMonotonic(x, y, x); }
Square(x);
<
2.0;
}
}
}
assert 2.0 in upper;
sqrt2 := Reals.DedekindCompleteness(lower, upper);
assert 1.0 <= sqrt2 <= 2.0;
assert Square(sqrt2) == 2.0 by {
if sqrt2 * sqrt2 > 2.0 {
var eps := (sqrt2 * sqrt2 - 2.0) / (2.0 * sqrt2);
assert 0.0 < eps < 1.0;
var y := sqrt2 - eps;
assert y * y > 2.0 by {
calc {
y * y;
sqrt2 * sqrt2 - 2.0 * sqrt2 * eps + eps * eps;
sqrt2 * sqrt2 - (sqrt2 * sqrt2 - 2.0) + eps * eps;
2.0 + eps * eps;
>
2.0;
}
}
assert y in upper by {
assert y >= 0.0;
assert y * y > 2.0;
}
assert false;
}
if sqrt2 * sqrt2 < 2.0 {
var eps := (2.0 - sqrt2 * sqrt2) / (2.0 * sqrt2 + 1.0);
assert 0.0 < eps < 1.0;
var y := sqrt2 + eps;
assert y * y < 2.0 by {
calc {
y * y;
sqrt2 * sqrt2 + 2.0 * sqrt2 * eps + eps * eps;
sqrt2 * sqrt2 + (2.0 * sqrt2 + eps) * eps;
< { RealArith.MulMonotonicStrict(eps, 2.0 * sqrt2 + eps, 2.0 * sqrt2 + 1.0); }
sqrt2 * sqrt2 + (2.0 * sqrt2 + 1.0) * eps;
sqrt2 * sqrt2 + (2.0 - sqrt2 * sqrt2);
2.0;
}
}
assert y in lower by {
assert y >= 0.0;
assert y * y < 2.0;
}
assert false;
}
}
}
|
vmc | 28 | ./src/Math/Analysis/Reals.dfy | Sqrt2Exists_fix_28.dfy | Sqrt2Exists | lemma Sqrt2Exists() returns (sqrt2: real)
ensures sqrt2 * sqrt2 == 2.0
{
var lower := iset x: real | x >= 0.0 && Square(x) < 2.0;
var upper := iset x: real | x >= 0.0 && Square(x) > 2.0;
forall x <- lower, y <- upper ensures x < y {
if x >= y {
calc {
2.0;
<
Square(y);
<= { RealArith.MulMonotonic(y, y, x); RealArith.MulMonotonic(x, y, x); }
Square(x);
<
2.0;
}
}
}
assert 1.0 in lower;
assert 2.0 in upper;
sqrt2 := Reals.DedekindCompleteness(lower, upper);
assert 1.0 <= sqrt2 <= 2.0;
assert Square(sqrt2) == 2.0 by {
if sqrt2 * sqrt2 > 2.0 {
var eps := (sqrt2 * sqrt2 - 2.0) / (2.0 * sqrt2);
assert 0.0 < eps < 1.0;
var y := sqrt2 - eps;
assert y * y > 2.0 by {
calc {
y * y;
sqrt2 * sqrt2 - 2.0 * sqrt2 * eps + eps * eps;
sqrt2 * sqrt2 - (sqrt2 * sqrt2 - 2.0) + eps * eps;
2.0 + eps * eps;
>
2.0;
}
}
assert y in upper by {
assert y >= 0.0;
assert y * y > 2.0;
}
assert false;
}
if sqrt2 * sqrt2 < 2.0 {
var eps := (2.0 - sqrt2 * sqrt2) / (2.0 * sqrt2 + 1.0);
assert 0.0 < eps < 1.0;
var y := sqrt2 + eps;
assert y * y < 2.0 by {
calc {
y * y;
sqrt2 * sqrt2 + 2.0 * sqrt2 * eps + eps * eps;
sqrt2 * sqrt2 + (2.0 * sqrt2 + eps) * eps;
< { RealArith.MulMonotonicStrict(eps, 2.0 * sqrt2 + eps, 2.0 * sqrt2 + 1.0); }
sqrt2 * sqrt2 + (2.0 * sqrt2 + 1.0) * eps;
sqrt2 * sqrt2 + (2.0 - sqrt2 * sqrt2);
2.0;
}
}
assert y in lower by {
assert y >= 0.0;
assert y * y < 2.0;
}
assert false;
}
}
}
| lemma Sqrt2Exists() returns (sqrt2: real)
ensures sqrt2 * sqrt2 == 2.0
{
var lower := iset x: real | x >= 0.0 && Square(x) < 2.0;
var upper := iset x: real | x >= 0.0 && Square(x) > 2.0;
forall x <- lower, y <- upper ensures x < y {
if x >= y {
calc {
2.0;
<
Square(y);
<= { RealArith.MulMonotonic(y, y, x); RealArith.MulMonotonic(x, y, x); }
Square(x);
<
2.0;
}
}
}
assert 1.0 in lower;
sqrt2 := Reals.DedekindCompleteness(lower, upper);
assert 1.0 <= sqrt2 <= 2.0;
assert Square(sqrt2) == 2.0 by {
if sqrt2 * sqrt2 > 2.0 {
var eps := (sqrt2 * sqrt2 - 2.0) / (2.0 * sqrt2);
assert 0.0 < eps < 1.0;
var y := sqrt2 - eps;
assert y * y > 2.0 by {
calc {
y * y;
sqrt2 * sqrt2 - 2.0 * sqrt2 * eps + eps * eps;
sqrt2 * sqrt2 - (sqrt2 * sqrt2 - 2.0) + eps * eps;
2.0 + eps * eps;
>
2.0;
}
}
assert y in upper by {
assert y >= 0.0;
assert y * y > 2.0;
}
assert false;
}
if sqrt2 * sqrt2 < 2.0 {
var eps := (2.0 - sqrt2 * sqrt2) / (2.0 * sqrt2 + 1.0);
assert 0.0 < eps < 1.0;
var y := sqrt2 + eps;
assert y * y < 2.0 by {
calc {
y * y;
sqrt2 * sqrt2 + 2.0 * sqrt2 * eps + eps * eps;
sqrt2 * sqrt2 + (2.0 * sqrt2 + eps) * eps;
< { RealArith.MulMonotonicStrict(eps, 2.0 * sqrt2 + eps, 2.0 * sqrt2 + 1.0); }
sqrt2 * sqrt2 + (2.0 * sqrt2 + 1.0) * eps;
sqrt2 * sqrt2 + (2.0 - sqrt2 * sqrt2);
2.0;
}
}
assert y in lower by {
assert y >= 0.0;
assert y * y < 2.0;
}
assert false;
}
}
}
|
vmc | 56 | ./src/Distributions/UniformPowerOfTwo/Correctness.dfy | UnifCorrectness2Inequality_fix_56.dfy | UnifCorrectness2Inequality | lemma UnifCorrectness2Inequality(n: nat, m: nat)
requires n >= 1
requires m <= Helper.Power(2, Helper.Log2Floor(n))
ensures
var e := iset s | Model.Sample(n)(s).value < m;
&& e in Rand.eventSpace
&& Rand.prob(e) == (m as real) / (Helper.Power(2, Helper.Log2Floor(n)) as real)
{
var e := iset s | Model.Sample(n)(s).value < m;
if m == 0 {
assert e == iset{};
Rand.ProbIsProbabilityMeasure();
} else {
var e1 := iset s | Model.Sample(n)(s).value < m-1;
var e2 := iset s | Model.Sample(n)(s).value == m-1;
assert e1 in Rand.eventSpace by {
UnifCorrectness2Inequality(n, m-1);
}
assert e2 in Rand.eventSpace by {
UnifCorrectness2(n, m-1);
}
assert e in Rand.eventSpace by {
assert e == e1 + e2;
Rand.ProbIsProbabilityMeasure();
Measures.BinaryUnion(Rand.eventSpace, e1, e2);
}
calc {
Rand.prob(e);
{ assert e == e1 + e2; }
Rand.prob(e1 + e2);
{ assert e1 * e2 == iset{}; Rand.ProbIsProbabilityMeasure(); Measures.PosCountAddImpliesAdd(Rand.eventSpace, Rand.prob); assert Measures.IsAdditive(Rand.eventSpace, Rand.prob); }
Rand.prob(e1) + Rand.prob(e2);
{ UnifCorrectness2(n, m-1); UnifCorrectness2Inequality(n, m-1); }
(1.0 / (Helper.Power(2, Helper.Log2Floor(n)) as real)) + (((m-1) as real) / (Helper.Power(2, Helper.Log2Floor(n)) as real));
{ Helper.AdditionOfFractions(1.0, (m-1) as real, Helper.Power(2, Helper.Log2Floor(n)) as real); }
(1.0 + (m-1) as real) / (Helper.Power(2, Helper.Log2Floor(n)) as real);
{ assert 1.0 + (m-1) as real == (m as real); }
(m as real) / (Helper.Power(2, Helper.Log2Floor(n)) as real);
}
}
}
| lemma UnifCorrectness2Inequality(n: nat, m: nat)
requires n >= 1
requires m <= Helper.Power(2, Helper.Log2Floor(n))
ensures
var e := iset s | Model.Sample(n)(s).value < m;
&& e in Rand.eventSpace
&& Rand.prob(e) == (m as real) / (Helper.Power(2, Helper.Log2Floor(n)) as real)
{
var e := iset s | Model.Sample(n)(s).value < m;
if m == 0 {
Rand.ProbIsProbabilityMeasure();
} else {
var e1 := iset s | Model.Sample(n)(s).value < m-1;
var e2 := iset s | Model.Sample(n)(s).value == m-1;
assert e1 in Rand.eventSpace by {
UnifCorrectness2Inequality(n, m-1);
}
assert e2 in Rand.eventSpace by {
UnifCorrectness2(n, m-1);
}
assert e in Rand.eventSpace by {
assert e == e1 + e2;
Rand.ProbIsProbabilityMeasure();
Measures.BinaryUnion(Rand.eventSpace, e1, e2);
}
calc {
Rand.prob(e);
{ assert e == e1 + e2; }
Rand.prob(e1 + e2);
{ assert e1 * e2 == iset{}; Rand.ProbIsProbabilityMeasure(); Measures.PosCountAddImpliesAdd(Rand.eventSpace, Rand.prob); assert Measures.IsAdditive(Rand.eventSpace, Rand.prob); }
Rand.prob(e1) + Rand.prob(e2);
{ UnifCorrectness2(n, m-1); UnifCorrectness2Inequality(n, m-1); }
(1.0 / (Helper.Power(2, Helper.Log2Floor(n)) as real)) + (((m-1) as real) / (Helper.Power(2, Helper.Log2Floor(n)) as real));
{ Helper.AdditionOfFractions(1.0, (m-1) as real, Helper.Power(2, Helper.Log2Floor(n)) as real); }
(1.0 + (m-1) as real) / (Helper.Power(2, Helper.Log2Floor(n)) as real);
{ assert 1.0 + (m-1) as real == (m as real); }
(m as real) / (Helper.Power(2, Helper.Log2Floor(n)) as real);
}
}
}
|
vmc | 57 | ./src/Distributions/UniformPowerOfTwo/Correctness.dfy | UnifCorrectness2Inequality_fix_57.dfy | UnifCorrectness2Inequality | lemma UnifCorrectness2Inequality(n: nat, m: nat)
requires n >= 1
requires m <= Helper.Power(2, Helper.Log2Floor(n))
ensures
var e := iset s | Model.Sample(n)(s).value < m;
&& e in Rand.eventSpace
&& Rand.prob(e) == (m as real) / (Helper.Power(2, Helper.Log2Floor(n)) as real)
{
var e := iset s | Model.Sample(n)(s).value < m;
if m == 0 {
assert e == iset{};
Rand.ProbIsProbabilityMeasure();
} else {
var e1 := iset s | Model.Sample(n)(s).value < m-1;
var e2 := iset s | Model.Sample(n)(s).value == m-1;
assert e1 in Rand.eventSpace by {
UnifCorrectness2Inequality(n, m-1);
}
assert e2 in Rand.eventSpace by {
UnifCorrectness2(n, m-1);
}
assert e in Rand.eventSpace by {
assert e == e1 + e2;
Rand.ProbIsProbabilityMeasure();
Measures.BinaryUnion(Rand.eventSpace, e1, e2);
}
calc {
Rand.prob(e);
{ assert e == e1 + e2; }
Rand.prob(e1 + e2);
{ assert e1 * e2 == iset{}; Rand.ProbIsProbabilityMeasure(); Measures.PosCountAddImpliesAdd(Rand.eventSpace, Rand.prob); assert Measures.IsAdditive(Rand.eventSpace, Rand.prob); }
Rand.prob(e1) + Rand.prob(e2);
{ UnifCorrectness2(n, m-1); UnifCorrectness2Inequality(n, m-1); }
(1.0 / (Helper.Power(2, Helper.Log2Floor(n)) as real)) + (((m-1) as real) / (Helper.Power(2, Helper.Log2Floor(n)) as real));
{ Helper.AdditionOfFractions(1.0, (m-1) as real, Helper.Power(2, Helper.Log2Floor(n)) as real); }
(1.0 + (m-1) as real) / (Helper.Power(2, Helper.Log2Floor(n)) as real);
{ assert 1.0 + (m-1) as real == (m as real); }
(m as real) / (Helper.Power(2, Helper.Log2Floor(n)) as real);
}
}
}
| lemma UnifCorrectness2Inequality(n: nat, m: nat)
requires n >= 1
requires m <= Helper.Power(2, Helper.Log2Floor(n))
ensures
var e := iset s | Model.Sample(n)(s).value < m;
&& e in Rand.eventSpace
&& Rand.prob(e) == (m as real) / (Helper.Power(2, Helper.Log2Floor(n)) as real)
{
var e := iset s | Model.Sample(n)(s).value < m;
if m == 0 {
assert e == iset{};
Rand.ProbIsProbabilityMeasure();
} else {
var e1 := iset s | Model.Sample(n)(s).value < m-1;
var e2 := iset s | Model.Sample(n)(s).value == m-1;
assert e1 in Rand.eventSpace by {
UnifCorrectness2Inequality(n, m-1);
}
assert e2 in Rand.eventSpace by {
UnifCorrectness2(n, m-1);
}
assert e in Rand.eventSpace by {
Rand.ProbIsProbabilityMeasure();
Measures.BinaryUnion(Rand.eventSpace, e1, e2);
}
calc {
Rand.prob(e);
{ assert e == e1 + e2; }
Rand.prob(e1 + e2);
{ assert e1 * e2 == iset{}; Rand.ProbIsProbabilityMeasure(); Measures.PosCountAddImpliesAdd(Rand.eventSpace, Rand.prob); assert Measures.IsAdditive(Rand.eventSpace, Rand.prob); }
Rand.prob(e1) + Rand.prob(e2);
{ UnifCorrectness2(n, m-1); UnifCorrectness2Inequality(n, m-1); }
(1.0 / (Helper.Power(2, Helper.Log2Floor(n)) as real)) + (((m-1) as real) / (Helper.Power(2, Helper.Log2Floor(n)) as real));
{ Helper.AdditionOfFractions(1.0, (m-1) as real, Helper.Power(2, Helper.Log2Floor(n)) as real); }
(1.0 + (m-1) as real) / (Helper.Power(2, Helper.Log2Floor(n)) as real);
{ assert 1.0 + (m-1) as real == (m as real); }
(m as real) / (Helper.Power(2, Helper.Log2Floor(n)) as real);
}
}
}
|
vmc | 58 | ./src/Distributions/UniformPowerOfTwo/Correctness.dfy | UnifCorrectness_fix_58.dfy | UnifCorrectness | lemma UnifCorrectness2(n: nat, m: nat)
requires n >= 1
ensures
var e := iset s | Model.Sample(n)(s).value == m;
&& e in Rand.eventSpace
&& Rand.prob(e) == if m < Helper.Power(2, Helper.Log2Floor(n)) then 1.0 / (Helper.Power(2, Helper.Log2Floor(n)) as real) else 0.0
{
var e := iset s | Model.Sample(n)(s).value == m;
var k := Helper.Log2Floor(n);
assert e in Rand.eventSpace by {
var resultsWithValueM := Monad.ResultsWithValueIn(iset{m});
assert resultsWithValueM in Monad.natResultEventSpace by {
Monad.LiftInEventSpaceToResultEventSpace(iset{m}, Measures.natEventSpace);
}
var preimage := Measures.PreImage(Model.Sample(n), resultsWithValueM);
assert Measures.IsMeasurable(Rand.eventSpace, Monad.natResultEventSpace, Model.Sample(n)) by {
SampleIsIndep(n);
Independence.IsIndepImpliesMeasurableNat(Model.Sample(n));
}
assert e == preimage;
}
Helper.Power2OfLog2Floor(n);
UnifCorrectness(n, k);
assert UnifIsCorrect(n, k, m);
}
| lemma UnifCorrectness2(n: nat, m: nat)
requires n >= 1
ensures
var e := iset s | Model.Sample(n)(s).value == m;
&& e in Rand.eventSpace
&& Rand.prob(e) == if m < Helper.Power(2, Helper.Log2Floor(n)) then 1.0 / (Helper.Power(2, Helper.Log2Floor(n)) as real) else 0.0
{
var e := iset s | Model.Sample(n)(s).value == m;
var k := Helper.Log2Floor(n);
assert e in Rand.eventSpace by {
var resultsWithValueM := Monad.ResultsWithValueIn(iset{m});
assert resultsWithValueM in Monad.natResultEventSpace by {
Monad.LiftInEventSpaceToResultEventSpace(iset{m}, Measures.natEventSpace);
}
var preimage := Measures.PreImage(Model.Sample(n), resultsWithValueM);
assert Measures.IsMeasurable(Rand.eventSpace, Monad.natResultEventSpace, Model.Sample(n)) by {
SampleIsIndep(n);
Independence.IsIndepImpliesMeasurableNat(Model.Sample(n));
}
}
Helper.Power2OfLog2Floor(n);
UnifCorrectness(n, k);
assert UnifIsCorrect(n, k, m);
}
|
vmc | 60 | ./src/Distributions/UniformPowerOfTwo/Correctness.dfy | UnifCorrectness2_fix_60.dfy | UnifCorrectness2 | lemma UnifCorrectness2(n: nat, m: nat)
requires n >= 1
ensures
var e := iset s | Model.Sample(n)(s).value == m;
&& e in Rand.eventSpace
&& Rand.prob(e) == if m < Helper.Power(2, Helper.Log2Floor(n)) then 1.0 / (Helper.Power(2, Helper.Log2Floor(n)) as real) else 0.0
{
var e := iset s | Model.Sample(n)(s).value == m;
var k := Helper.Log2Floor(n);
assert e in Rand.eventSpace by {
var resultsWithValueM := Monad.ResultsWithValueIn(iset{m});
assert resultsWithValueM in Monad.natResultEventSpace by {
Monad.LiftInEventSpaceToResultEventSpace(iset{m}, Measures.natEventSpace);
}
var preimage := Measures.PreImage(Model.Sample(n), resultsWithValueM);
assert Measures.IsMeasurable(Rand.eventSpace, Monad.natResultEventSpace, Model.Sample(n)) by {
SampleIsIndep(n);
Independence.IsIndepImpliesMeasurableNat(Model.Sample(n));
}
assert e == preimage;
}
Helper.Power2OfLog2Floor(n);
UnifCorrectness(n, k);
assert UnifIsCorrect(n, k, m);
}
| lemma UnifCorrectness2(n: nat, m: nat)
requires n >= 1
ensures
var e := iset s | Model.Sample(n)(s).value == m;
&& e in Rand.eventSpace
&& Rand.prob(e) == if m < Helper.Power(2, Helper.Log2Floor(n)) then 1.0 / (Helper.Power(2, Helper.Log2Floor(n)) as real) else 0.0
{
var e := iset s | Model.Sample(n)(s).value == m;
var k := Helper.Log2Floor(n);
assert e in Rand.eventSpace by {
var resultsWithValueM := Monad.ResultsWithValueIn(iset{m});
assert resultsWithValueM in Monad.natResultEventSpace by {
Monad.LiftInEventSpaceToResultEventSpace(iset{m}, Measures.natEventSpace);
}
var preimage := Measures.PreImage(Model.Sample(n), resultsWithValueM);
assert Measures.IsMeasurable(Rand.eventSpace, Monad.natResultEventSpace, Model.Sample(n)) by {
SampleIsIndep(n);
Independence.IsIndepImpliesMeasurableNat(Model.Sample(n));
}
}
Helper.Power2OfLog2Floor(n);
UnifCorrectness(n, k);
assert UnifIsCorrect(n, k, m);
}
|
vmc | 61 | ./src/Distributions/UniformPowerOfTwo/Correctness.dfy | UnifCorrectness2_fix_61.dfy | UnifCorrectness2 | lemma UnifCorrectness2(n: nat, m: nat)
requires n >= 1
ensures
var e := iset s | Model.Sample(n)(s).value == m;
&& e in Rand.eventSpace
&& Rand.prob(e) == if m < Helper.Power(2, Helper.Log2Floor(n)) then 1.0 / (Helper.Power(2, Helper.Log2Floor(n)) as real) else 0.0
{
var e := iset s | Model.Sample(n)(s).value == m;
var k := Helper.Log2Floor(n);
assert e in Rand.eventSpace by {
var resultsWithValueM := Monad.ResultsWithValueIn(iset{m});
assert resultsWithValueM in Monad.natResultEventSpace by {
Monad.LiftInEventSpaceToResultEventSpace(iset{m}, Measures.natEventSpace);
}
var preimage := Measures.PreImage(Model.Sample(n), resultsWithValueM);
assert Measures.IsMeasurable(Rand.eventSpace, Monad.natResultEventSpace, Model.Sample(n)) by {
SampleIsIndep(n);
Independence.IsIndepImpliesMeasurableNat(Model.Sample(n));
}
assert e == preimage;
}
Helper.Power2OfLog2Floor(n);
UnifCorrectness(n, k);
assert UnifIsCorrect(n, k, m);
}
| lemma UnifCorrectness2(n: nat, m: nat)
requires n >= 1
ensures
var e := iset s | Model.Sample(n)(s).value == m;
&& e in Rand.eventSpace
&& Rand.prob(e) == if m < Helper.Power(2, Helper.Log2Floor(n)) then 1.0 / (Helper.Power(2, Helper.Log2Floor(n)) as real) else 0.0
{
var e := iset s | Model.Sample(n)(s).value == m;
var k := Helper.Log2Floor(n);
assert e in Rand.eventSpace by {
var resultsWithValueM := Monad.ResultsWithValueIn(iset{m});
assert resultsWithValueM in Monad.natResultEventSpace by {
Monad.LiftInEventSpaceToResultEventSpace(iset{m}, Measures.natEventSpace);
}
var preimage := Measures.PreImage(Model.Sample(n), resultsWithValueM);
assert Measures.IsMeasurable(Rand.eventSpace, Monad.natResultEventSpace, Model.Sample(n)) by {
SampleIsIndep(n);
Independence.IsIndepImpliesMeasurableNat(Model.Sample(n));
}
assert e == preimage;
}
Helper.Power2OfLog2Floor(n);
UnifCorrectness(n, k);
}
|
vmc | 62 | ./src/Distributions/UniformPowerOfTwo/Correctness.dfy | SampleIsMeasurePreserving_fix_62.dfy | SampleIsMeasurePreserving | lemma SampleIsMeasurePreserving(n: nat)
requires n >= 1
ensures Measures.IsMeasurePreserving(Rand.eventSpace, Rand.prob, Rand.eventSpace, Rand.prob, SampleRest(n))
{
var f := SampleRest(n);
assert Measures.IsMeasurable(Rand.eventSpace, Rand.eventSpace, f) by {
forall e | e in Rand.eventSpace ensures Measures.PreImage(f, e) in Rand.eventSpace {
var resultsWithRestInE := Monad.ResultsWithRestIn(e);
assert resultsWithRestInE in Monad.natResultEventSpace by {
Monad.LiftRestInEventSpaceToResultEventSpace(e, Measures.natEventSpace);
}
var preimage' := Measures.PreImage(Model.Sample(n), resultsWithRestInE);
assert preimage' in Rand.eventSpace by {
SampleIsIndep(n);
Independence.IsIndepImpliesMeasurableNat(Model.Sample(n));
}
assert Measures.PreImage(f, e) == preimage';
}
}
if n == 1 {
forall e | e in Rand.eventSpace ensures Rand.prob(Measures.PreImage(f, e)) == Rand.prob(e) {
forall s: Rand.Bitstream ensures f(s) == s {
reveal Model.Sample();
assert f(s) == s;
}
Measures.PreImageIdentity(f, e);
}
assert Measures.IsMeasurePreserving(Rand.eventSpace, Rand.prob, Rand.eventSpace, Rand.prob, f);
} else {
var g := SampleRest(n / 2);
forall e | e in Rand.eventSpace ensures Rand.prob(Measures.PreImage(f, e)) == Rand.prob(e) {
var e' := (iset s | Rand.Tail(s) in e);
assert e' in Rand.eventSpace by {
assert e' == Measures.PreImage(Rand.Tail, e);
Rand.TailIsMeasurePreserving();
assert Measures.IsMeasurable(Rand.eventSpace, Rand.eventSpace, Rand.Tail);
}
assert Measures.PreImage(f, e) == Measures.PreImage(g, e') by {
assert forall s :: f(s) in e <==> g(s) in e' by {
forall s ensures f(s) in e <==> g(s) in e' {
calc {
f(s) in e;
<==> { assert f(s) == Model.Sample(n)(s).rest; }
Model.Sample(n)(s).rest in e;
<==> { SampleTailDecompose(n, s); }
Rand.Tail(Model.Sample(n / 2)(s).rest) in e;
<==>
Model.Sample(n / 2)(s).rest in e';
<==> { assert Model.Sample(n / 2)(s).rest == g(s); }
g(s) in e';
}
}
}
Measures.PreImagesEqual(f, e, g, e');
}
assert Rand.prob(Measures.PreImage(f, e)) == Rand.prob(e) by {
calc {
Rand.prob(Measures.PreImage(f, e));
==
Rand.prob(Measures.PreImage(g, e'));
== { SampleIsMeasurePreserving(n / 2); assert Measures.IsMeasurePreserving(Rand.eventSpace, Rand.prob, Rand.eventSpace, Rand.prob, g); assert e' in Rand.eventSpace; }
Rand.prob(e');
== { assert e' == Measures.PreImage(Rand.Tail, e); }
Rand.prob(Measures.PreImage(Rand.Tail, e));
== { Rand.TailIsMeasurePreserving(); }
Rand.prob(e);
}
}
}
assert Measures.IsMeasurePreserving(Rand.eventSpace, Rand.prob, Rand.eventSpace, Rand.prob, f);
}
}
| lemma SampleIsMeasurePreserving(n: nat)
requires n >= 1
ensures Measures.IsMeasurePreserving(Rand.eventSpace, Rand.prob, Rand.eventSpace, Rand.prob, SampleRest(n))
{
var f := SampleRest(n);
assert Measures.IsMeasurable(Rand.eventSpace, Rand.eventSpace, f) by {
forall e | e in Rand.eventSpace ensures Measures.PreImage(f, e) in Rand.eventSpace {
var resultsWithRestInE := Monad.ResultsWithRestIn(e);
assert resultsWithRestInE in Monad.natResultEventSpace by {
Monad.LiftRestInEventSpaceToResultEventSpace(e, Measures.natEventSpace);
}
var preimage' := Measures.PreImage(Model.Sample(n), resultsWithRestInE);
assert preimage' in Rand.eventSpace by {
SampleIsIndep(n);
Independence.IsIndepImpliesMeasurableNat(Model.Sample(n));
}
}
}
if n == 1 {
forall e | e in Rand.eventSpace ensures Rand.prob(Measures.PreImage(f, e)) == Rand.prob(e) {
forall s: Rand.Bitstream ensures f(s) == s {
reveal Model.Sample();
assert f(s) == s;
}
Measures.PreImageIdentity(f, e);
}
assert Measures.IsMeasurePreserving(Rand.eventSpace, Rand.prob, Rand.eventSpace, Rand.prob, f);
} else {
var g := SampleRest(n / 2);
forall e | e in Rand.eventSpace ensures Rand.prob(Measures.PreImage(f, e)) == Rand.prob(e) {
var e' := (iset s | Rand.Tail(s) in e);
assert e' in Rand.eventSpace by {
assert e' == Measures.PreImage(Rand.Tail, e);
Rand.TailIsMeasurePreserving();
assert Measures.IsMeasurable(Rand.eventSpace, Rand.eventSpace, Rand.Tail);
}
assert Measures.PreImage(f, e) == Measures.PreImage(g, e') by {
assert forall s :: f(s) in e <==> g(s) in e' by {
forall s ensures f(s) in e <==> g(s) in e' {
calc {
f(s) in e;
<==> { assert f(s) == Model.Sample(n)(s).rest; }
Model.Sample(n)(s).rest in e;
<==> { SampleTailDecompose(n, s); }
Rand.Tail(Model.Sample(n / 2)(s).rest) in e;
<==>
Model.Sample(n / 2)(s).rest in e';
<==> { assert Model.Sample(n / 2)(s).rest == g(s); }
g(s) in e';
}
}
}
Measures.PreImagesEqual(f, e, g, e');
}
assert Rand.prob(Measures.PreImage(f, e)) == Rand.prob(e) by {
calc {
Rand.prob(Measures.PreImage(f, e));
==
Rand.prob(Measures.PreImage(g, e'));
== { SampleIsMeasurePreserving(n / 2); assert Measures.IsMeasurePreserving(Rand.eventSpace, Rand.prob, Rand.eventSpace, Rand.prob, g); assert e' in Rand.eventSpace; }
Rand.prob(e');
== { assert e' == Measures.PreImage(Rand.Tail, e); }
Rand.prob(Measures.PreImage(Rand.Tail, e));
== { Rand.TailIsMeasurePreserving(); }
Rand.prob(e);
}
}
}
assert Measures.IsMeasurePreserving(Rand.eventSpace, Rand.prob, Rand.eventSpace, Rand.prob, f);
}
}
|
vmc | 65 | ./src/Distributions/UniformPowerOfTwo/Correctness.dfy | SampleIsMeasurePreserving_fix_65.dfy | SampleIsMeasurePreserving | lemma SampleIsMeasurePreserving(n: nat)
requires n >= 1
ensures Measures.IsMeasurePreserving(Rand.eventSpace, Rand.prob, Rand.eventSpace, Rand.prob, SampleRest(n))
{
var f := SampleRest(n);
assert Measures.IsMeasurable(Rand.eventSpace, Rand.eventSpace, f) by {
forall e | e in Rand.eventSpace ensures Measures.PreImage(f, e) in Rand.eventSpace {
var resultsWithRestInE := Monad.ResultsWithRestIn(e);
assert resultsWithRestInE in Monad.natResultEventSpace by {
Monad.LiftRestInEventSpaceToResultEventSpace(e, Measures.natEventSpace);
}
var preimage' := Measures.PreImage(Model.Sample(n), resultsWithRestInE);
assert preimage' in Rand.eventSpace by {
SampleIsIndep(n);
Independence.IsIndepImpliesMeasurableNat(Model.Sample(n));
}
assert Measures.PreImage(f, e) == preimage';
}
}
if n == 1 {
forall e | e in Rand.eventSpace ensures Rand.prob(Measures.PreImage(f, e)) == Rand.prob(e) {
forall s: Rand.Bitstream ensures f(s) == s {
reveal Model.Sample();
assert f(s) == s;
}
Measures.PreImageIdentity(f, e);
}
assert Measures.IsMeasurePreserving(Rand.eventSpace, Rand.prob, Rand.eventSpace, Rand.prob, f);
} else {
var g := SampleRest(n / 2);
forall e | e in Rand.eventSpace ensures Rand.prob(Measures.PreImage(f, e)) == Rand.prob(e) {
var e' := (iset s | Rand.Tail(s) in e);
assert e' in Rand.eventSpace by {
assert e' == Measures.PreImage(Rand.Tail, e);
Rand.TailIsMeasurePreserving();
assert Measures.IsMeasurable(Rand.eventSpace, Rand.eventSpace, Rand.Tail);
}
assert Measures.PreImage(f, e) == Measures.PreImage(g, e') by {
assert forall s :: f(s) in e <==> g(s) in e' by {
forall s ensures f(s) in e <==> g(s) in e' {
calc {
f(s) in e;
<==> { assert f(s) == Model.Sample(n)(s).rest; }
Model.Sample(n)(s).rest in e;
<==> { SampleTailDecompose(n, s); }
Rand.Tail(Model.Sample(n / 2)(s).rest) in e;
<==>
Model.Sample(n / 2)(s).rest in e';
<==> { assert Model.Sample(n / 2)(s).rest == g(s); }
g(s) in e';
}
}
}
Measures.PreImagesEqual(f, e, g, e');
}
assert Rand.prob(Measures.PreImage(f, e)) == Rand.prob(e) by {
calc {
Rand.prob(Measures.PreImage(f, e));
==
Rand.prob(Measures.PreImage(g, e'));
== { SampleIsMeasurePreserving(n / 2); assert Measures.IsMeasurePreserving(Rand.eventSpace, Rand.prob, Rand.eventSpace, Rand.prob, g); assert e' in Rand.eventSpace; }
Rand.prob(e');
== { assert e' == Measures.PreImage(Rand.Tail, e); }
Rand.prob(Measures.PreImage(Rand.Tail, e));
== { Rand.TailIsMeasurePreserving(); }
Rand.prob(e);
}
}
}
assert Measures.IsMeasurePreserving(Rand.eventSpace, Rand.prob, Rand.eventSpace, Rand.prob, f);
}
}
| lemma SampleIsMeasurePreserving(n: nat)
requires n >= 1
ensures Measures.IsMeasurePreserving(Rand.eventSpace, Rand.prob, Rand.eventSpace, Rand.prob, SampleRest(n))
{
var f := SampleRest(n);
assert Measures.IsMeasurable(Rand.eventSpace, Rand.eventSpace, f) by {
forall e | e in Rand.eventSpace ensures Measures.PreImage(f, e) in Rand.eventSpace {
var resultsWithRestInE := Monad.ResultsWithRestIn(e);
assert resultsWithRestInE in Monad.natResultEventSpace by {
Monad.LiftRestInEventSpaceToResultEventSpace(e, Measures.natEventSpace);
}
var preimage' := Measures.PreImage(Model.Sample(n), resultsWithRestInE);
assert preimage' in Rand.eventSpace by {
SampleIsIndep(n);
Independence.IsIndepImpliesMeasurableNat(Model.Sample(n));
}
assert Measures.PreImage(f, e) == preimage';
}
}
if n == 1 {
forall e | e in Rand.eventSpace ensures Rand.prob(Measures.PreImage(f, e)) == Rand.prob(e) {
forall s: Rand.Bitstream ensures f(s) == s {
reveal Model.Sample();
assert f(s) == s;
}
Measures.PreImageIdentity(f, e);
}
assert Measures.IsMeasurePreserving(Rand.eventSpace, Rand.prob, Rand.eventSpace, Rand.prob, f);
} else {
var g := SampleRest(n / 2);
forall e | e in Rand.eventSpace ensures Rand.prob(Measures.PreImage(f, e)) == Rand.prob(e) {
var e' := (iset s | Rand.Tail(s) in e);
assert e' in Rand.eventSpace by {
Rand.TailIsMeasurePreserving();
assert Measures.IsMeasurable(Rand.eventSpace, Rand.eventSpace, Rand.Tail);
}
assert Measures.PreImage(f, e) == Measures.PreImage(g, e') by {
assert forall s :: f(s) in e <==> g(s) in e' by {
forall s ensures f(s) in e <==> g(s) in e' {
calc {
f(s) in e;
<==> { assert f(s) == Model.Sample(n)(s).rest; }
Model.Sample(n)(s).rest in e;
<==> { SampleTailDecompose(n, s); }
Rand.Tail(Model.Sample(n / 2)(s).rest) in e;
<==>
Model.Sample(n / 2)(s).rest in e';
<==> { assert Model.Sample(n / 2)(s).rest == g(s); }
g(s) in e';
}
}
}
Measures.PreImagesEqual(f, e, g, e');
}
assert Rand.prob(Measures.PreImage(f, e)) == Rand.prob(e) by {
calc {
Rand.prob(Measures.PreImage(f, e));
==
Rand.prob(Measures.PreImage(g, e'));
== { SampleIsMeasurePreserving(n / 2); assert Measures.IsMeasurePreserving(Rand.eventSpace, Rand.prob, Rand.eventSpace, Rand.prob, g); assert e' in Rand.eventSpace; }
Rand.prob(e');
== { assert e' == Measures.PreImage(Rand.Tail, e); }
Rand.prob(Measures.PreImage(Rand.Tail, e));
== { Rand.TailIsMeasurePreserving(); }
Rand.prob(e);
}
}
}
assert Measures.IsMeasurePreserving(Rand.eventSpace, Rand.prob, Rand.eventSpace, Rand.prob, f);
}
}
|
vmc | 70 | ./src/Distributions/UniformPowerOfTwo/Correctness.dfy | SampleRecursiveHalf_fix_70.dfy | SampleRecursiveHalf | lemma SampleRecursiveHalf(n: nat, m: nat)
requires n >= 2
ensures Rand.prob(iset s | Model.Sample(n)(s).value == m) == Rand.prob(iset s | Model.Sample(n / 2)(s).value == m / 2) / 2.0
{
var aOf: Rand.Bitstream -> nat := (s: Rand.Bitstream) => Model.Sample(n / 2)(s).value;
var bOf: Rand.Bitstream -> bool := (s: Rand.Bitstream) => Monad.Coin(Model.Sample(n / 2)(s).rest).value;
var A: iset<nat> := (iset x: nat | x == m / 2);
var E: iset<Rand.Bitstream> := (iset s | m % 2 as nat == Helper.boolToNat(Monad.Coin(s).value));
var f := (s: Rand.Bitstream) => Model.Sample(n / 2)(s).rest;
var e1 := (iset s | Model.Sample(n / 2)(s).RestIn(E));
var e2 := (iset s | Model.Sample(n / 2)(s).In(A));
var e3 := (iset s | 2*aOf(s) + Helper.boolToNat(bOf(s)) == m);
assert SplitEvent: e3 == e1 * e2 by {
forall s ensures s in e3 <==> s in e1 && s in e2 {
var a: nat := aOf(s);
var b: nat := Helper.boolToNat(bOf(s));
assert b < 2;
calc {
s in e3;
2 * a + b == m;
m == a * 2 + b;
(a == m / 2) && (b == m % 2);
s in e1 && s in e2;
}
}
}
assert Eq2: (iset s | aOf(s) == m / 2) == e2 by {
forall s ensures aOf(s) == m / 2 <==> Model.Sample(n / 2)(s).value in A {
}
}
assert Eq3: (iset s | aOf(s) == m / 2) == (iset s | Model.Sample(n / 2)(s).value == m / 2) by {
forall s ensures aOf(s) == m / 2 <==> Model.Sample(n / 2)(s).value == m / 2 {
assert aOf(s) == Model.Sample(n / 2)(s).value;
}
}
assert Eq4: e1 == Measures.PreImage(f, E) by {
forall s ensures Model.Sample(n / 2)(s).rest in E <==> f(s) in E {
}
}
assert E in Rand.eventSpace && Rand.prob(E) == 0.5 by {
assert E == (iset s | Rand.Head(s) == (m % 2 == 1));
Rand.CoinHasProbOneHalf(m % 2 == 1);
}
assert Indep: Rand.prob(e1 * e2) == Rand.prob(e1) * Rand.prob(e2) by {
assert Measures.AreIndepEvents(Rand.eventSpace, Rand.prob, e1, e2) by {
assert Independence.IsIndepFunction(Model.Sample(n / 2)) by {
assert Independence.IsIndep(Model.Sample(n / 2)) by {
SampleIsIndep(n / 2);
}
Independence.IsIndepImpliesIsIndepFunction(Model.Sample(n / 2));
}
assert E in Rand.eventSpace;
assert Independence.IsIndepFunctionCondition(Model.Sample(n / 2), A, E);
}
Independence.AreIndepEventsConjunctElimination(e1, e2);
}
assert ProbE1: Rand.prob(e1) == 0.5 by {
calc {
0.5;
==
Rand.prob(E);
== { SampleIsMeasurePreserving(n / 2); }
Rand.prob(Measures.PreImage(f, E));
== { reveal Eq4; }
Rand.prob(e1);
}
}
calc {
Rand.prob(iset s | Model.Sample(n)(s).value == m);
== { SampleSetEquality(n, m); }
Rand.prob(e3);
== { reveal SplitEvent; }
Rand.prob(e1 * e2);
== { reveal Indep; }
Rand.prob(e1) * Rand.prob(e2);
== { reveal ProbE1; Helper.Congruence(Rand.prob(e1), 0.5, x => x * Rand.prob(e2)); }
0.5 * Rand.prob(e2);
==
Rand.prob(e2) / 2.0;
== { reveal Eq2; }
Rand.prob(iset s | aOf(s) == m / 2) / 2.0;
== { reveal Eq3; }
Rand.prob(iset s | Model.Sample(n / 2)(s).value == m / 2) / 2.0;
}
}
| lemma SampleRecursiveHalf(n: nat, m: nat)
requires n >= 2
ensures Rand.prob(iset s | Model.Sample(n)(s).value == m) == Rand.prob(iset s | Model.Sample(n / 2)(s).value == m / 2) / 2.0
{
var aOf: Rand.Bitstream -> nat := (s: Rand.Bitstream) => Model.Sample(n / 2)(s).value;
var bOf: Rand.Bitstream -> bool := (s: Rand.Bitstream) => Monad.Coin(Model.Sample(n / 2)(s).rest).value;
var A: iset<nat> := (iset x: nat | x == m / 2);
var E: iset<Rand.Bitstream> := (iset s | m % 2 as nat == Helper.boolToNat(Monad.Coin(s).value));
var f := (s: Rand.Bitstream) => Model.Sample(n / 2)(s).rest;
var e1 := (iset s | Model.Sample(n / 2)(s).RestIn(E));
var e2 := (iset s | Model.Sample(n / 2)(s).In(A));
var e3 := (iset s | 2*aOf(s) + Helper.boolToNat(bOf(s)) == m);
assert SplitEvent: e3 == e1 * e2 by {
forall s ensures s in e3 <==> s in e1 && s in e2 {
var a: nat := aOf(s);
var b: nat := Helper.boolToNat(bOf(s));
assert b < 2;
calc {
s in e3;
2 * a + b == m;
m == a * 2 + b;
(a == m / 2) && (b == m % 2);
s in e1 && s in e2;
}
}
}
assert Eq2: (iset s | aOf(s) == m / 2) == e2 by {
forall s ensures aOf(s) == m / 2 <==> Model.Sample(n / 2)(s).value in A {
}
}
assert Eq3: (iset s | aOf(s) == m / 2) == (iset s | Model.Sample(n / 2)(s).value == m / 2) by {
forall s ensures aOf(s) == m / 2 <==> Model.Sample(n / 2)(s).value == m / 2 {
assert aOf(s) == Model.Sample(n / 2)(s).value;
}
}
assert Eq4: e1 == Measures.PreImage(f, E) by {
forall s ensures Model.Sample(n / 2)(s).rest in E <==> f(s) in E {
}
}
assert E in Rand.eventSpace && Rand.prob(E) == 0.5 by {
Rand.CoinHasProbOneHalf(m % 2 == 1);
}
assert Indep: Rand.prob(e1 * e2) == Rand.prob(e1) * Rand.prob(e2) by {
assert Measures.AreIndepEvents(Rand.eventSpace, Rand.prob, e1, e2) by {
assert Independence.IsIndepFunction(Model.Sample(n / 2)) by {
assert Independence.IsIndep(Model.Sample(n / 2)) by {
SampleIsIndep(n / 2);
}
Independence.IsIndepImpliesIsIndepFunction(Model.Sample(n / 2));
}
assert E in Rand.eventSpace;
assert Independence.IsIndepFunctionCondition(Model.Sample(n / 2), A, E);
}
Independence.AreIndepEventsConjunctElimination(e1, e2);
}
assert ProbE1: Rand.prob(e1) == 0.5 by {
calc {
0.5;
==
Rand.prob(E);
== { SampleIsMeasurePreserving(n / 2); }
Rand.prob(Measures.PreImage(f, E));
== { reveal Eq4; }
Rand.prob(e1);
}
}
calc {
Rand.prob(iset s | Model.Sample(n)(s).value == m);
== { SampleSetEquality(n, m); }
Rand.prob(e3);
== { reveal SplitEvent; }
Rand.prob(e1 * e2);
== { reveal Indep; }
Rand.prob(e1) * Rand.prob(e2);
== { reveal ProbE1; Helper.Congruence(Rand.prob(e1), 0.5, x => x * Rand.prob(e2)); }
0.5 * Rand.prob(e2);
==
Rand.prob(e2) / 2.0;
== { reveal Eq2; }
Rand.prob(iset s | aOf(s) == m / 2) / 2.0;
== { reveal Eq3; }
Rand.prob(iset s | Model.Sample(n / 2)(s).value == m / 2) / 2.0;
}
}
|
vmc | 72 | ./src/Distributions/UniformPowerOfTwo/Correctness.dfy | SampleRecursiveHalf_fix_72.dfy | SampleRecursiveHalf | lemma SampleRecursiveHalf(n: nat, m: nat)
requires n >= 2
ensures Rand.prob(iset s | Model.Sample(n)(s).value == m) == Rand.prob(iset s | Model.Sample(n / 2)(s).value == m / 2) / 2.0
{
var aOf: Rand.Bitstream -> nat := (s: Rand.Bitstream) => Model.Sample(n / 2)(s).value;
var bOf: Rand.Bitstream -> bool := (s: Rand.Bitstream) => Monad.Coin(Model.Sample(n / 2)(s).rest).value;
var A: iset<nat> := (iset x: nat | x == m / 2);
var E: iset<Rand.Bitstream> := (iset s | m % 2 as nat == Helper.boolToNat(Monad.Coin(s).value));
var f := (s: Rand.Bitstream) => Model.Sample(n / 2)(s).rest;
var e1 := (iset s | Model.Sample(n / 2)(s).RestIn(E));
var e2 := (iset s | Model.Sample(n / 2)(s).In(A));
var e3 := (iset s | 2*aOf(s) + Helper.boolToNat(bOf(s)) == m);
assert SplitEvent: e3 == e1 * e2 by {
forall s ensures s in e3 <==> s in e1 && s in e2 {
var a: nat := aOf(s);
var b: nat := Helper.boolToNat(bOf(s));
assert b < 2;
calc {
s in e3;
2 * a + b == m;
m == a * 2 + b;
(a == m / 2) && (b == m % 2);
s in e1 && s in e2;
}
}
}
assert Eq2: (iset s | aOf(s) == m / 2) == e2 by {
forall s ensures aOf(s) == m / 2 <==> Model.Sample(n / 2)(s).value in A {
}
}
assert Eq3: (iset s | aOf(s) == m / 2) == (iset s | Model.Sample(n / 2)(s).value == m / 2) by {
forall s ensures aOf(s) == m / 2 <==> Model.Sample(n / 2)(s).value == m / 2 {
assert aOf(s) == Model.Sample(n / 2)(s).value;
}
}
assert Eq4: e1 == Measures.PreImage(f, E) by {
forall s ensures Model.Sample(n / 2)(s).rest in E <==> f(s) in E {
}
}
assert E in Rand.eventSpace && Rand.prob(E) == 0.5 by {
assert E == (iset s | Rand.Head(s) == (m % 2 == 1));
Rand.CoinHasProbOneHalf(m % 2 == 1);
}
assert Indep: Rand.prob(e1 * e2) == Rand.prob(e1) * Rand.prob(e2) by {
assert Measures.AreIndepEvents(Rand.eventSpace, Rand.prob, e1, e2) by {
assert Independence.IsIndepFunction(Model.Sample(n / 2)) by {
assert Independence.IsIndep(Model.Sample(n / 2)) by {
SampleIsIndep(n / 2);
}
Independence.IsIndepImpliesIsIndepFunction(Model.Sample(n / 2));
}
assert E in Rand.eventSpace;
assert Independence.IsIndepFunctionCondition(Model.Sample(n / 2), A, E);
}
Independence.AreIndepEventsConjunctElimination(e1, e2);
}
assert ProbE1: Rand.prob(e1) == 0.5 by {
calc {
0.5;
==
Rand.prob(E);
== { SampleIsMeasurePreserving(n / 2); }
Rand.prob(Measures.PreImage(f, E));
== { reveal Eq4; }
Rand.prob(e1);
}
}
calc {
Rand.prob(iset s | Model.Sample(n)(s).value == m);
== { SampleSetEquality(n, m); }
Rand.prob(e3);
== { reveal SplitEvent; }
Rand.prob(e1 * e2);
== { reveal Indep; }
Rand.prob(e1) * Rand.prob(e2);
== { reveal ProbE1; Helper.Congruence(Rand.prob(e1), 0.5, x => x * Rand.prob(e2)); }
0.5 * Rand.prob(e2);
==
Rand.prob(e2) / 2.0;
== { reveal Eq2; }
Rand.prob(iset s | aOf(s) == m / 2) / 2.0;
== { reveal Eq3; }
Rand.prob(iset s | Model.Sample(n / 2)(s).value == m / 2) / 2.0;
}
}
| lemma SampleRecursiveHalf(n: nat, m: nat)
requires n >= 2
ensures Rand.prob(iset s | Model.Sample(n)(s).value == m) == Rand.prob(iset s | Model.Sample(n / 2)(s).value == m / 2) / 2.0
{
var aOf: Rand.Bitstream -> nat := (s: Rand.Bitstream) => Model.Sample(n / 2)(s).value;
var bOf: Rand.Bitstream -> bool := (s: Rand.Bitstream) => Monad.Coin(Model.Sample(n / 2)(s).rest).value;
var A: iset<nat> := (iset x: nat | x == m / 2);
var E: iset<Rand.Bitstream> := (iset s | m % 2 as nat == Helper.boolToNat(Monad.Coin(s).value));
var f := (s: Rand.Bitstream) => Model.Sample(n / 2)(s).rest;
var e1 := (iset s | Model.Sample(n / 2)(s).RestIn(E));
var e2 := (iset s | Model.Sample(n / 2)(s).In(A));
var e3 := (iset s | 2*aOf(s) + Helper.boolToNat(bOf(s)) == m);
assert SplitEvent: e3 == e1 * e2 by {
forall s ensures s in e3 <==> s in e1 && s in e2 {
var a: nat := aOf(s);
var b: nat := Helper.boolToNat(bOf(s));
assert b < 2;
calc {
s in e3;
2 * a + b == m;
m == a * 2 + b;
(a == m / 2) && (b == m % 2);
s in e1 && s in e2;
}
}
}
assert Eq2: (iset s | aOf(s) == m / 2) == e2 by {
forall s ensures aOf(s) == m / 2 <==> Model.Sample(n / 2)(s).value in A {
}
}
assert Eq3: (iset s | aOf(s) == m / 2) == (iset s | Model.Sample(n / 2)(s).value == m / 2) by {
forall s ensures aOf(s) == m / 2 <==> Model.Sample(n / 2)(s).value == m / 2 {
assert aOf(s) == Model.Sample(n / 2)(s).value;
}
}
assert Eq4: e1 == Measures.PreImage(f, E) by {
forall s ensures Model.Sample(n / 2)(s).rest in E <==> f(s) in E {
}
}
assert E in Rand.eventSpace && Rand.prob(E) == 0.5 by {
assert E == (iset s | Rand.Head(s) == (m % 2 == 1));
Rand.CoinHasProbOneHalf(m % 2 == 1);
}
assert Indep: Rand.prob(e1 * e2) == Rand.prob(e1) * Rand.prob(e2) by {
assert Measures.AreIndepEvents(Rand.eventSpace, Rand.prob, e1, e2) by {
assert Independence.IsIndepFunction(Model.Sample(n / 2)) by {
assert Independence.IsIndep(Model.Sample(n / 2)) by {
SampleIsIndep(n / 2);
}
Independence.IsIndepImpliesIsIndepFunction(Model.Sample(n / 2));
}
assert E in Rand.eventSpace;
}
Independence.AreIndepEventsConjunctElimination(e1, e2);
}
assert ProbE1: Rand.prob(e1) == 0.5 by {
calc {
0.5;
==
Rand.prob(E);
== { SampleIsMeasurePreserving(n / 2); }
Rand.prob(Measures.PreImage(f, E));
== { reveal Eq4; }
Rand.prob(e1);
}
}
calc {
Rand.prob(iset s | Model.Sample(n)(s).value == m);
== { SampleSetEquality(n, m); }
Rand.prob(e3);
== { reveal SplitEvent; }
Rand.prob(e1 * e2);
== { reveal Indep; }
Rand.prob(e1) * Rand.prob(e2);
== { reveal ProbE1; Helper.Congruence(Rand.prob(e1), 0.5, x => x * Rand.prob(e2)); }
0.5 * Rand.prob(e2);
==
Rand.prob(e2) / 2.0;
== { reveal Eq2; }
Rand.prob(iset s | aOf(s) == m / 2) / 2.0;
== { reveal Eq3; }
Rand.prob(iset s | Model.Sample(n / 2)(s).value == m / 2) / 2.0;
}
}
|
vmc | 77 | ./src/Distributions/Uniform/Model.dfy | SampleTerminates_fix_77.dfy | SampleTerminates | lemma SampleTerminates(n: nat)
requires n > 0
ensures
&& Independence.IsIndep(Proposal(n))
&& Quantifier.WithPosProb(Loops.ProposalIsAccepted(Proposal(n), Accept(n)))
&& Loops.UntilTerminatesAlmostSurely(Proposal(n), Accept(n))
{
assert Independence.IsIndep(Proposal(n)) by {
UniformPowerOfTwo.Correctness.SampleIsIndep(2 * n);
}
var e := iset s | Loops.ProposalIsAccepted(Proposal(n), Accept(n))(s);
assert e in Rand.eventSpace by {
var ltN := iset m: nat | m < n;
var resultsLtN := Monad.ResultsWithValueIn(ltN);
assert e == Measures.PreImage(UniformPowerOfTwo.Model.Sample(2 * n), resultsLtN);
assert Measures.PreImage(UniformPowerOfTwo.Model.Sample(2 * n), resultsLtN) in Rand.eventSpace by {
assert Independence.IsIndep(UniformPowerOfTwo.Model.Sample(2 * n)) by {
UniformPowerOfTwo.Correctness.SampleIsIndep(2 * n);
}
assert Measures.IsMeasurable(Rand.eventSpace, Monad.natResultEventSpace, UniformPowerOfTwo.Model.Sample(2 * n)) by {
Independence.IsIndepImpliesMeasurableNat(UniformPowerOfTwo.Model.Sample(2 * n));
}
assert resultsLtN in Monad.natResultEventSpace by {
Monad.LiftInEventSpaceToResultEventSpace(ltN, Measures.natEventSpace);
}
}
}
assert Quantifier.WithPosProb(Loops.ProposalIsAccepted(Proposal(n), Accept(n))) by {
assert Rand.prob(e) > 0.0 by {
assert e == (iset s | UniformPowerOfTwo.Model.Sample(2 * n)(s).value < n);
assert n <= Helper.Power(2, Helper.Log2Floor(2 * n)) by {
Helper.NLtPower2Log2FloorOf2N(n);
}
calc {
Rand.prob(e);
== { UniformPowerOfTwo.Correctness.UnifCorrectness2Inequality(2 * n, n); }
n as real / (Helper.Power(2, Helper.Log2Floor(2 * n)) as real);
>
0.0;
}
}
}
assert Loops.UntilTerminatesAlmostSurely(Proposal(n), Accept(n)) by {
Loops.EnsureUntilTerminates(Proposal(n), Accept(n));
}
}
| lemma SampleTerminates(n: nat)
requires n > 0
ensures
&& Independence.IsIndep(Proposal(n))
&& Quantifier.WithPosProb(Loops.ProposalIsAccepted(Proposal(n), Accept(n)))
&& Loops.UntilTerminatesAlmostSurely(Proposal(n), Accept(n))
{
assert Independence.IsIndep(Proposal(n)) by {
UniformPowerOfTwo.Correctness.SampleIsIndep(2 * n);
}
var e := iset s | Loops.ProposalIsAccepted(Proposal(n), Accept(n))(s);
assert e in Rand.eventSpace by {
var ltN := iset m: nat | m < n;
var resultsLtN := Monad.ResultsWithValueIn(ltN);
assert Measures.PreImage(UniformPowerOfTwo.Model.Sample(2 * n), resultsLtN) in Rand.eventSpace by {
assert Independence.IsIndep(UniformPowerOfTwo.Model.Sample(2 * n)) by {
UniformPowerOfTwo.Correctness.SampleIsIndep(2 * n);
}
assert Measures.IsMeasurable(Rand.eventSpace, Monad.natResultEventSpace, UniformPowerOfTwo.Model.Sample(2 * n)) by {
Independence.IsIndepImpliesMeasurableNat(UniformPowerOfTwo.Model.Sample(2 * n));
}
assert resultsLtN in Monad.natResultEventSpace by {
Monad.LiftInEventSpaceToResultEventSpace(ltN, Measures.natEventSpace);
}
}
}
assert Quantifier.WithPosProb(Loops.ProposalIsAccepted(Proposal(n), Accept(n))) by {
assert Rand.prob(e) > 0.0 by {
assert e == (iset s | UniformPowerOfTwo.Model.Sample(2 * n)(s).value < n);
assert n <= Helper.Power(2, Helper.Log2Floor(2 * n)) by {
Helper.NLtPower2Log2FloorOf2N(n);
}
calc {
Rand.prob(e);
== { UniformPowerOfTwo.Correctness.UnifCorrectness2Inequality(2 * n, n); }
n as real / (Helper.Power(2, Helper.Log2Floor(2 * n)) as real);
>
0.0;
}
}
}
assert Loops.UntilTerminatesAlmostSurely(Proposal(n), Accept(n)) by {
Loops.EnsureUntilTerminates(Proposal(n), Accept(n));
}
}
|
vmc | 78 | ./src/Distributions/Uniform/Model.dfy | SampleTerminates_fix_78.dfy | SampleTerminates | lemma SampleTerminates(n: nat)
requires n > 0
ensures
&& Independence.IsIndep(Proposal(n))
&& Quantifier.WithPosProb(Loops.ProposalIsAccepted(Proposal(n), Accept(n)))
&& Loops.UntilTerminatesAlmostSurely(Proposal(n), Accept(n))
{
assert Independence.IsIndep(Proposal(n)) by {
UniformPowerOfTwo.Correctness.SampleIsIndep(2 * n);
}
var e := iset s | Loops.ProposalIsAccepted(Proposal(n), Accept(n))(s);
assert e in Rand.eventSpace by {
var ltN := iset m: nat | m < n;
var resultsLtN := Monad.ResultsWithValueIn(ltN);
assert e == Measures.PreImage(UniformPowerOfTwo.Model.Sample(2 * n), resultsLtN);
assert Measures.PreImage(UniformPowerOfTwo.Model.Sample(2 * n), resultsLtN) in Rand.eventSpace by {
assert Independence.IsIndep(UniformPowerOfTwo.Model.Sample(2 * n)) by {
UniformPowerOfTwo.Correctness.SampleIsIndep(2 * n);
}
assert Measures.IsMeasurable(Rand.eventSpace, Monad.natResultEventSpace, UniformPowerOfTwo.Model.Sample(2 * n)) by {
Independence.IsIndepImpliesMeasurableNat(UniformPowerOfTwo.Model.Sample(2 * n));
}
assert resultsLtN in Monad.natResultEventSpace by {
Monad.LiftInEventSpaceToResultEventSpace(ltN, Measures.natEventSpace);
}
}
}
assert Quantifier.WithPosProb(Loops.ProposalIsAccepted(Proposal(n), Accept(n))) by {
assert Rand.prob(e) > 0.0 by {
assert e == (iset s | UniformPowerOfTwo.Model.Sample(2 * n)(s).value < n);
assert n <= Helper.Power(2, Helper.Log2Floor(2 * n)) by {
Helper.NLtPower2Log2FloorOf2N(n);
}
calc {
Rand.prob(e);
== { UniformPowerOfTwo.Correctness.UnifCorrectness2Inequality(2 * n, n); }
n as real / (Helper.Power(2, Helper.Log2Floor(2 * n)) as real);
>
0.0;
}
}
}
assert Loops.UntilTerminatesAlmostSurely(Proposal(n), Accept(n)) by {
Loops.EnsureUntilTerminates(Proposal(n), Accept(n));
}
}
| lemma SampleTerminates(n: nat)
requires n > 0
ensures
&& Independence.IsIndep(Proposal(n))
&& Quantifier.WithPosProb(Loops.ProposalIsAccepted(Proposal(n), Accept(n)))
&& Loops.UntilTerminatesAlmostSurely(Proposal(n), Accept(n))
{
assert Independence.IsIndep(Proposal(n)) by {
UniformPowerOfTwo.Correctness.SampleIsIndep(2 * n);
}
var e := iset s | Loops.ProposalIsAccepted(Proposal(n), Accept(n))(s);
assert e in Rand.eventSpace by {
var ltN := iset m: nat | m < n;
var resultsLtN := Monad.ResultsWithValueIn(ltN);
assert e == Measures.PreImage(UniformPowerOfTwo.Model.Sample(2 * n), resultsLtN);
assert Measures.PreImage(UniformPowerOfTwo.Model.Sample(2 * n), resultsLtN) in Rand.eventSpace by {
assert Independence.IsIndep(UniformPowerOfTwo.Model.Sample(2 * n)) by {
UniformPowerOfTwo.Correctness.SampleIsIndep(2 * n);
}
assert Measures.IsMeasurable(Rand.eventSpace, Monad.natResultEventSpace, UniformPowerOfTwo.Model.Sample(2 * n)) by {
Independence.IsIndepImpliesMeasurableNat(UniformPowerOfTwo.Model.Sample(2 * n));
}
assert resultsLtN in Monad.natResultEventSpace by {
Monad.LiftInEventSpaceToResultEventSpace(ltN, Measures.natEventSpace);
}
}
}
assert Quantifier.WithPosProb(Loops.ProposalIsAccepted(Proposal(n), Accept(n))) by {
assert Rand.prob(e) > 0.0 by {
assert n <= Helper.Power(2, Helper.Log2Floor(2 * n)) by {
Helper.NLtPower2Log2FloorOf2N(n);
}
calc {
Rand.prob(e);
== { UniformPowerOfTwo.Correctness.UnifCorrectness2Inequality(2 * n, n); }
n as real / (Helper.Power(2, Helper.Log2Floor(2 * n)) as real);
>
0.0;
}
}
}
assert Loops.UntilTerminatesAlmostSurely(Proposal(n), Accept(n)) by {
Loops.EnsureUntilTerminates(Proposal(n), Accept(n));
}
}
|
vmc | 86 | ./src/ProbabilisticProgramming/Monad.dfy | LiftInEventSpaceToResultEventSpace_fix_86.dfy | LiftInEventSpaceToResultEventSpace | lemma LiftInEventSpaceToResultEventSpace<A(!new)>(event: iset<A>, eventSpace: iset<iset<A>>)
requires event in eventSpace
ensures ResultsWithValueIn(event) in ResultEventSpace(eventSpace)
{
var results := ResultsWithValueIn(event);
assert Measures.IsSigmaAlgebra(Rand.eventSpace) by {
Rand.ProbIsProbabilityMeasure();
}
assert Values(results) == event by {
forall v: A ensures v in event <==> v in Values(results) {
var s: Rand.Bitstream :| true;
assert v in event <==> Result(v, s) in results;
}
}
assert Rests(results) in Rand.eventSpace by {
if v :| v in event {
assert Rests(results) == Measures.SampleSpace() by {
forall s: Rand.Bitstream ensures s in Rests(results) <==> s in Measures.SampleSpace() {
calc {
s in Rests(results);
Result(v, s) in results;
true;
}
}
}
} else {
assert Rests(results) == iset{};
}
}
}
| lemma LiftInEventSpaceToResultEventSpace<A(!new)>(event: iset<A>, eventSpace: iset<iset<A>>)
requires event in eventSpace
ensures ResultsWithValueIn(event) in ResultEventSpace(eventSpace)
{
var results := ResultsWithValueIn(event);
assert Measures.IsSigmaAlgebra(Rand.eventSpace) by {
Rand.ProbIsProbabilityMeasure();
}
assert Values(results) == event by {
forall v: A ensures v in event <==> v in Values(results) {
var s: Rand.Bitstream :| true;
}
}
assert Rests(results) in Rand.eventSpace by {
if v :| v in event {
assert Rests(results) == Measures.SampleSpace() by {
forall s: Rand.Bitstream ensures s in Rests(results) <==> s in Measures.SampleSpace() {
calc {
s in Rests(results);
Result(v, s) in results;
true;
}
}
}
} else {
assert Rests(results) == iset{};
}
}
}
|
vmc | 87 | ./src/ProbabilisticProgramming/Monad.dfy | LiftInEventSpaceToResultEventSpace_fix_87.dfy | LiftInEventSpaceToResultEventSpace | lemma LiftInEventSpaceToResultEventSpace<A(!new)>(event: iset<A>, eventSpace: iset<iset<A>>)
requires event in eventSpace
ensures ResultsWithValueIn(event) in ResultEventSpace(eventSpace)
{
var results := ResultsWithValueIn(event);
assert Measures.IsSigmaAlgebra(Rand.eventSpace) by {
Rand.ProbIsProbabilityMeasure();
}
assert Values(results) == event by {
forall v: A ensures v in event <==> v in Values(results) {
var s: Rand.Bitstream :| true;
assert v in event <==> Result(v, s) in results;
}
}
assert Rests(results) in Rand.eventSpace by {
if v :| v in event {
assert Rests(results) == Measures.SampleSpace() by {
forall s: Rand.Bitstream ensures s in Rests(results) <==> s in Measures.SampleSpace() {
calc {
s in Rests(results);
Result(v, s) in results;
true;
}
}
}
} else {
assert Rests(results) == iset{};
}
}
}
| lemma LiftInEventSpaceToResultEventSpace<A(!new)>(event: iset<A>, eventSpace: iset<iset<A>>)
requires event in eventSpace
ensures ResultsWithValueIn(event) in ResultEventSpace(eventSpace)
{
var results := ResultsWithValueIn(event);
assert Measures.IsSigmaAlgebra(Rand.eventSpace) by {
Rand.ProbIsProbabilityMeasure();
}
assert Values(results) == event by {
forall v: A ensures v in event <==> v in Values(results) {
var s: Rand.Bitstream :| true;
assert v in event <==> Result(v, s) in results;
}
}
assert Rests(results) in Rand.eventSpace by {
if v :| v in event {
assert Rests(results) == Measures.SampleSpace() by {
forall s: Rand.Bitstream ensures s in Rests(results) <==> s in Measures.SampleSpace() {
calc {
s in Rests(results);
Result(v, s) in results;
true;
}
}
}
} else {
}
}
}
|
vmc | 89 | ./src/ProbabilisticProgramming/Loops.dfy | WhileUnroll_fix_89.dfy | WhileUnroll | lemma WhileUnrollIfConditionSatisfied<A>(condition: A -> bool, body: A -> Monad.Hurd<A>, init: A, s: Rand.Bitstream, init': A, s': Rand.Bitstream, loop: Monad.Result<A>, unrolled: Monad.Result<A>)
requires WhileCutTerminates(condition, body, init, s)
requires condition(init)
requires body(init)(s) == Monad.Result(init', s')
requires loop == While(condition, body)(init)(s)
requires unrolled == While(condition, body)(init')(s')
ensures loop == unrolled
{
var fuel: nat := LeastFuel(condition, body, init, s);
assert fuel >= 1;
var fuel': nat := fuel - 1;
assert WhileCutTerminatesWithFuel(condition, body, init', s')(fuel');
assert WhileCutTerminates(condition, body, init', s');
var minFuel: nat := LeastFuel(condition, body, init', s');
assert minFuel == fuel' by {
LeastFuelUnroll(condition, body, init, s, init', s', minFuel);
}
assert loop == unrolled by {
calc {
loop;
{ reveal While(); }
WhileCut(condition, body, init, fuel)(s);
{ WhileCutUnroll(condition, body, init, s, init', s', fuel'); }
WhileCut(condition, body, init', fuel')(s');
{ reveal While(); }
unrolled;
}
}
}
| lemma WhileUnrollIfConditionSatisfied<A>(condition: A -> bool, body: A -> Monad.Hurd<A>, init: A, s: Rand.Bitstream, init': A, s': Rand.Bitstream, loop: Monad.Result<A>, unrolled: Monad.Result<A>)
requires WhileCutTerminates(condition, body, init, s)
requires condition(init)
requires body(init)(s) == Monad.Result(init', s')
requires loop == While(condition, body)(init)(s)
requires unrolled == While(condition, body)(init')(s')
ensures loop == unrolled
{
var fuel: nat := LeastFuel(condition, body, init, s);
assert fuel >= 1;
var fuel': nat := fuel - 1;
assert WhileCutTerminates(condition, body, init', s');
var minFuel: nat := LeastFuel(condition, body, init', s');
assert minFuel == fuel' by {
LeastFuelUnroll(condition, body, init, s, init', s', minFuel);
}
assert loop == unrolled by {
calc {
loop;
{ reveal While(); }
WhileCut(condition, body, init, fuel)(s);
{ WhileCutUnroll(condition, body, init, s, init', s', fuel'); }
WhileCut(condition, body, init', fuel')(s');
{ reveal While(); }
unrolled;
}
}
}
|
vmc | 94 | ./src/ProbabilisticProgramming/Loops.dfy | WhileUnrollIfConditionSatisfied_fix_94.dfy | WhileUnrollIfConditionSatisfied | lemma WhileUnrollIfConditionSatisfied<A>(condition: A -> bool, body: A -> Monad.Hurd<A>, init: A, s: Rand.Bitstream, init': A, s': Rand.Bitstream, loop: Monad.Result<A>, unrolled: Monad.Result<A>)
requires WhileCutTerminates(condition, body, init, s)
requires condition(init)
requires body(init)(s) == Monad.Result(init', s')
requires loop == While(condition, body)(init)(s)
requires unrolled == While(condition, body)(init')(s')
ensures loop == unrolled
{
var fuel: nat := LeastFuel(condition, body, init, s);
assert fuel >= 1;
var fuel': nat := fuel - 1;
assert WhileCutTerminatesWithFuel(condition, body, init', s')(fuel');
assert WhileCutTerminates(condition, body, init', s');
var minFuel: nat := LeastFuel(condition, body, init', s');
assert minFuel == fuel' by {
LeastFuelUnroll(condition, body, init, s, init', s', minFuel);
}
assert loop == unrolled by {
calc {
loop;
{ reveal While(); }
WhileCut(condition, body, init, fuel)(s);
{ WhileCutUnroll(condition, body, init, s, init', s', fuel'); }
WhileCut(condition, body, init', fuel')(s');
{ reveal While(); }
unrolled;
}
}
}
| lemma WhileUnrollIfConditionSatisfied<A>(condition: A -> bool, body: A -> Monad.Hurd<A>, init: A, s: Rand.Bitstream, init': A, s': Rand.Bitstream, loop: Monad.Result<A>, unrolled: Monad.Result<A>)
requires WhileCutTerminates(condition, body, init, s)
requires condition(init)
requires body(init)(s) == Monad.Result(init', s')
requires loop == While(condition, body)(init)(s)
requires unrolled == While(condition, body)(init')(s')
ensures loop == unrolled
{
var fuel: nat := LeastFuel(condition, body, init, s);
assert fuel >= 1;
var fuel': nat := fuel - 1;
assert WhileCutTerminates(condition, body, init', s');
var minFuel: nat := LeastFuel(condition, body, init', s');
assert minFuel == fuel' by {
LeastFuelUnroll(condition, body, init, s, init', s', minFuel);
}
assert loop == unrolled by {
calc {
loop;
{ reveal While(); }
WhileCut(condition, body, init, fuel)(s);
{ WhileCutUnroll(condition, body, init, s, init', s', fuel'); }
WhileCut(condition, body, init', fuel')(s');
{ reveal While(); }
unrolled;
}
}
}
|
vmc | 98 | ./src/ProbabilisticProgramming/Loops.dfy | EnsureUntilTerminates_fix_98.dfy | EnsureUntilTerminates | lemma EnsureUntilTerminates<A(!new)>(proposal: Monad.Hurd<A>, accept: A -> bool)
requires Independence.IsIndep(proposal)
requires Quantifier.WithPosProb((s: Rand.Bitstream) => proposal(s).Satisfies(accept))
ensures UntilTerminatesAlmostSurely(proposal, accept)
{
var reject := (a: A) => !accept(a);
var body := (a: A) => proposal;
var proposalIsAccepted := (s: Rand.Bitstream) => proposal(s).Satisfies(accept);
assert UntilTerminatesAlmostSurely(proposal, accept) by {
forall a: A ensures Independence.IsIndep(body(a)) {
assert body(a) == proposal;
}
forall a: A ensures Quantifier.WithPosProb(WhileLoopExitsAfterOneIteration(body, reject, a)) {
assert Quantifier.WithPosProb(proposalIsAccepted);
assert (iset s | proposalIsAccepted(s)) == (iset s | WhileLoopExitsAfterOneIteration(body, reject, a)(s));
}
assert WhileTerminatesAlmostSurely(reject, body) by {
EnsureWhileTerminates(reject, body);
}
}
}
| lemma EnsureUntilTerminates<A(!new)>(proposal: Monad.Hurd<A>, accept: A -> bool)
requires Independence.IsIndep(proposal)
requires Quantifier.WithPosProb((s: Rand.Bitstream) => proposal(s).Satisfies(accept))
ensures UntilTerminatesAlmostSurely(proposal, accept)
{
var reject := (a: A) => !accept(a);
var body := (a: A) => proposal;
var proposalIsAccepted := (s: Rand.Bitstream) => proposal(s).Satisfies(accept);
assert UntilTerminatesAlmostSurely(proposal, accept) by {
forall a: A ensures Independence.IsIndep(body(a)) {
assert body(a) == proposal;
}
forall a: A ensures Quantifier.WithPosProb(WhileLoopExitsAfterOneIteration(body, reject, a)) {
assert Quantifier.WithPosProb(proposalIsAccepted);
}
assert WhileTerminatesAlmostSurely(reject, body) by {
EnsureWhileTerminates(reject, body);
}
}
}
|