Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model #33

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CreuSAT/src/formula.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl Formula {
#[cfg_attr(feature = "trust_formula", trusted)]
#[requires(self.invariant())]
#[requires(a.invariant(*self))]
#[ensures(result == self.sat(*a))]
#[ensures(result == a.real_model().satisfies(self.real_model()))]
pub fn is_sat(&self, a: &Assignments) -> bool {
let mut i: usize = 0;
#[invariant(prev, forall<k: Int> 0 <= k && k < @i ==> (@self.clauses)[k].sat(*a))]
Expand Down
55 changes: 54 additions & 1 deletion CreuSAT/src/logic/logic.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,64 @@
extern crate creusot_contracts;
use creusot_contracts::std::*;
use creusot_contracts::*;

use crate::{assignments::*, clause::*, formula::*, lit::*, trail::*};

use crate::logic::{logic_assignments::*, logic_clause::*, logic_formula::*, logic_trail::*};

#[cfg(feature = "contracts")]
mod inner {
use creusot_contracts::{*, Model};
use crate::lit::Lit;
use crate::formula::Formula;
use crate::assignments::Assignments;
pub struct M(Mapping<Int, bool>);

impl M {
#[predicate]
fn satisfies_clause(self, cl: Seq<Lit>) -> bool {
pearlite! {
exists<i : Int> 0 <= i && i < cl.len() && self.0.get(@cl[i].idx) == cl[i].polarity
}
}

#[predicate]
pub fn satisfies(self, fml: Seq<Seq<Lit>>) -> bool {
pearlite! {
forall<c : _> 0 <= c && c < fml.len() ==> self.satisfies_clause(fml[c])
}
}
}

impl Formula {
#[predicate]
pub fn unsat(self) -> bool {
pearlite! { forall<m : M> m.satisfies(self.real_model()) ==> false }
}

#[predicate]
pub fn sat(self) -> bool {
pearlite! { exists<m : M> m.satisfies(self.real_model()) }
}

#[predicate]
pub fn equisat(self, f: Self) -> bool {
pearlite! {
forall<m : M> m.satisfies(self.real_model()) ==> m.satisfies(f.real_model()) && m.satisfies(f.real_model()) ==> m.satisfies(self.real_model())
}
}
}

impl Assignments {
#[logic]
pub fn real_model(self) -> M {
M(Mapping::cst(false))
}
}
}

#[cfg(feature = "contracts")]
pub use inner::*;

#[logic]
fn pos() -> AssignedState {
1u8
Expand Down
31 changes: 19 additions & 12 deletions CreuSAT/src/logic/logic_formula.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ impl Model for Formula {
}
}

impl Formula {
#[logic]
pub fn real_model(self) -> Seq<Seq<Lit>> {
Seq::EMPTY
}
}

#[predicate]
pub fn formula_invariant(f: (Seq<Clause>, Int)) -> bool {
pearlite! {
Expand Down Expand Up @@ -75,10 +82,10 @@ impl Formula {
exists<a2 : Seq<AssignedState>> a2.len() == @self.num_vars && complete_inner(a2) && self.sat_inner(a2)
}
}
#[predicate]
pub fn equisat(self, o: Formula) -> bool {
self.eventually_sat_complete_no_ass() == o.eventually_sat_complete_no_ass()
}
// #[predicate]
// pub fn equisat(self, o: Formula) -> bool {
// self.eventually_sat_complete_no_ass() == o.eventually_sat_complete_no_ass()
// }

#[predicate]
pub fn compatible(self, o: Formula) -> bool {
Expand Down Expand Up @@ -146,10 +153,10 @@ impl Formula {
}
}

#[predicate]
pub fn sat(self, a: Assignments) -> bool {
pearlite! { formula_sat_inner(@self, @a) }
}
// #[predicate]
// pub fn sat(self, a: Assignments) -> bool {
// pearlite! { formula_sat_inner(@self, @a) }
// }

#[predicate]
fn unsat_inner(self, a: Seq<AssignedState>) -> bool {
Expand All @@ -159,10 +166,10 @@ impl Formula {
}
}

#[predicate]
pub fn unsat(self, a: Assignments) -> bool {
pearlite! { self.unsat_inner(@a) }
}
// #[predicate]
// pub fn unsat(self, a: Assignments) -> bool {
// pearlite! { self.unsat_inner(@a) }
// }

#[predicate]
pub fn not_satisfiable(self) -> bool {
Expand Down
3 changes: 2 additions & 1 deletion CreuSAT/src/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ impl Solver {
#[ensures(@f.num_vars == @(^f).num_vars)]
#[ensures(f.equisat(^f))]
#[ensures(match result {
SatResult::Sat(_) => { (^f).sat((^trail).assignments)
SatResult::Sat(_) => {
(^trail).assignments.real_model().satisfies((^f).real_model())
&& ((^trail).assignments).complete() }, // Do I really need this for anything?
SatResult::Unsat => { (^f).not_satisfiable() },
SatResult::Unknown => { true }
Expand Down
12 changes: 9 additions & 3 deletions CreuSAT/src/unit_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ fn exists_new_watchable_lit(
#[ensures(match result {
Ok(true) => true,
Ok(false) => (@(^trail).trail).len() == (@trail.trail).len(),
Err(n) => @n < (@(^f).clauses).len() && (^f).unsat((^trail).assignments) && (@(^f).clauses)[@n].unsat((^trail).assignments),
Err(n) => @n < (@(^f).clauses).len() &&
// (^f).unsat((^trail).assignments) &&
(@(^f).clauses)[@n].unsat((^trail).assignments),
})]
#[ensures(@f.num_vars == @(^f).num_vars)]
#[ensures(f.equisat(^f))]
Expand Down Expand Up @@ -234,7 +236,9 @@ fn propagate_lit_with_regard_to_clause(
#[requires(lit.index_logic() < @f.num_vars)]
#[ensures(match result {
Ok(()) => true,// !(^f).unsat(^a),
Err(n) => @n < (@(^f).clauses).len() && (^f).unsat((^trail).assignments) && (@(^f).clauses)[@n].unsat((^trail).assignments),
Err(n) => @n < (@(^f).clauses).len() &&
// (^f).unsat((^trail).assignments) &&
(@(^f).clauses)[@n].unsat((^trail).assignments),
})]
#[ensures(@f.num_vars == @(^f).num_vars)]
#[ensures(f.equisat(^f))]
Expand Down Expand Up @@ -283,7 +287,9 @@ fn propagate_literal(f: &mut Formula, trail: &mut Trail, watches: &mut Watches,
#[requires(@f.num_vars < @usize::MAX/2)]
#[ensures(match result {
Ok(()) => true, // !(^f).unsat(^a),
Err(n) => @n < (@(^f).clauses).len() && (^f).unsat((^trail).assignments) && (@(^f).clauses)[@n].unsat((^trail).assignments),
Err(n) => @n < (@(^f).clauses).len() &&
// (^f).unsat((^trail).assignments) &&
(@(^f).clauses)[@n].unsat((^trail).assignments),
})]
#[ensures(@f.num_vars == @(^f).num_vars)]
#[ensures(f.equisat(^f))]
Expand Down
Loading