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

Counter Example generation #652

Draft
wants to merge 35 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
8f574a1
WIP counter example generation
RobinWebbers Apr 20, 2023
61fa746
BFS Counter example generator
RobinWebbers May 3, 2023
9914197
Fixed typing issues of counter examples. Fixed issue of running multi…
RobinWebbers May 11, 2023
0cb062b
Put counter example in output result. Only runs tests for failing cla…
RobinWebbers May 12, 2023
37d9405
Now tracks counter examples per constraint id. Added a max depth to a…
RobinWebbers May 25, 2023
57e55b8
Removed dead code previously introduced
RobinWebbers May 25, 2023
4188204
Added some comments and reduced exports for CounterExample.hs
RobinWebbers May 25, 2023
8548f4c
Solved hlint warnings
RobinWebbers May 25, 2023
9d79fda
WIP Counterexample depth first run with location info
RobinWebbers Jun 13, 2023
5fdd8d8
Merge branch 'ucsd-progsys:develop' into develop
RobinWebbers Jul 5, 2023
a2d8fee
Modified to depth first counter example search. Added stack traces to…
RobinWebbers Jul 7, 2023
13dabbc
Split counterexample build and check
RobinWebbers Jul 7, 2023
705a2ce
Improved readback from SMT. Added back recursion limit
RobinWebbers Nov 2, 2023
8702a1c
Fixed issues with duplicate naming and SMT model parsing
RobinWebbers Nov 14, 2023
82ca427
Counterexamples now include Result generic, symbol and type. Moved co…
RobinWebbers Feb 15, 2024
9f91f76
Changed name scheme in counterexample to use BindId as a path instead…
RobinWebbers Feb 19, 2024
876804d
Changed the format of counterexamles. The extension to full counterex…
RobinWebbers Feb 19, 2024
3b84915
Removed obsolete where binding from `toFullCex`
RobinWebbers Feb 19, 2024
558306f
Obsolete environments are now deleted from the counterexample
RobinWebbers Feb 28, 2024
3a656bc
Split SMT interface of counterexamples from checker
RobinWebbers Feb 28, 2024
f0f0967
Squashed calls to not incorrectly unfold multiple kvars if they were …
RobinWebbers Feb 28, 2024
bca8208
Changed counterexample SMT scope to just include top level subcid. Ad…
RobinWebbers Mar 7, 2024
2852c27
Solved issue where counterexamples were not correctly inserted. Solve…
RobinWebbers Mar 8, 2024
3b43016
Used internal location information for producing spans in a counterex…
RobinWebbers Mar 14, 2024
63211db
Merged with latest branch of main repo
RobinWebbers Mar 14, 2024
bb27151
Removed spurous counterexamples from declarations after assume/call/a…
RobinWebbers Mar 19, 2024
f50e50c
Fixed wrong naming on 'fix$' names. Fixed wrong recursion limit, that…
RobinWebbers Mar 20, 2024
72ac0fa
Added define axioms to counterexample constraints, such that we gener…
RobinWebbers Mar 24, 2024
d308c7e
Removed debug print
RobinWebbers Mar 24, 2024
d8049fb
Added support for SMT Sequence theory
RobinWebbers Mar 25, 2024
d1cec55
Definitions as quantified axioms proved too slow, so for now I remove…
RobinWebbers Mar 25, 2024
b504b47
Resolved issue where the counterexample build would ignore other refi…
RobinWebbers Apr 1, 2024
b5bbb84
Added early pruning of paths and removal of duplicate kvars (i.e. ide…
RobinWebbers Apr 2, 2024
2e88e2c
Changed search strategy from DFS to iterative deepening DFS
RobinWebbers Apr 2, 2024
677d322
Removed obsolete import
RobinWebbers Apr 2, 2024
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
7 changes: 7 additions & 0 deletions liquid-fixpoint.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ library
import: warnings
exposed-modules: Data.ShareMap
Language.Fixpoint.Conditional.Z3
Language.Fixpoint.Counterexample
Language.Fixpoint.Counterexample.Build
Language.Fixpoint.Counterexample.Check
Language.Fixpoint.Counterexample.JSON
Language.Fixpoint.Counterexample.SMT
Language.Fixpoint.Counterexample.Types
Language.Fixpoint.Defunctionalize
Language.Fixpoint.Graph
Language.Fixpoint.Graph.Deps
Expand Down Expand Up @@ -158,6 +164,7 @@ library
, smtlib-backends-process >= 0.3
, stm
, store
, scientific >= 0.3.7
, vector < 0.14
, syb
, text
Expand Down
104 changes: 104 additions & 0 deletions src/Language/Fixpoint/Counterexample.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}

Check warning on line 3 in src/Language/Fixpoint/Counterexample.hs

View workflow job for this annotation

GitHub Actions / hlint-3.4

Warning in module Language.Fixpoint.Counterexample: Unused LANGUAGE pragma ▫︎ Found: "{-# LANGUAGE OverloadedStrings #-}"

module Language.Fixpoint.Counterexample
( tryCounterExample
) where

import Language.Fixpoint.Types
import Language.Fixpoint.Counterexample.JSON (jsonCex)
import Language.Fixpoint.Types.Config (Config, counterexample)
import Language.Fixpoint.Solver.EnvironmentReduction (dropLikelyIrrelevantBindings)

import Language.Fixpoint.Counterexample.Types
import Language.Fixpoint.Counterexample.Build
import Language.Fixpoint.Counterexample.Check

import qualified Data.HashMap.Strict as Map

import Control.Monad.IO.Class
import Control.Monad (forM_)

-- TODO: Remove variables from the counter example that got mapped to
-- the "wrong" type in smt format (e.g. to an int while not being one).

-- TODO: Ideally `Result` would not have `SubcId` in its generic. Instead, this
-- should just always be contained in a `Result`. Right now, our counterexample
-- will contain a bunch of meaningless `SubcId` as we need to read it from
-- the result.

-- | Try to get a counter example for the given unsafe clauses (if any).
tryCounterExample
:: MonadIO m
=> Show info
=> Fixpoint info
=> Loc info
=> PPrint info
=> Config
-> SInfo info
-> Result (SubcId, info)
-> m (Result (SubcId, info))
tryCounterExample cfg si res@Result
{ resStatus = Unsafe _ cids'
, resCounterexamples = cexs'
} | counterexample cfg = do
-- Build program from constraints
prog <- hornToProg cfg si

-- Check the constraints, returning a substitution map
let cids = fst <$> cids'
smtcex <- checkProg cfg si prog cids

-- Extend the smt counterexample to include additional bind info
let cexs = toFullCex si <$> smtcex

-- Store the counterexamples as JSON
forM_ cexs $ jsonCex cfg si
return res { resCounterexamples = cexs <> cexs' }
tryCounterExample _ _ res = return res

-- | Extend an SMT counterexample to a full counterexample.
toFullCex :: SInfo info -> SMTCounterexample -> FullCounterexample (SubcId, info)
toFullCex si (Counterexample env subcid trace) = Counterexample
{ cexEnv = substToCexEnv si subcid env
, cexConstraint = subcid
, cexFrames = toFullCex si <$> trace
}

-- | Extend an SMT counterexample environment (i.e. the substitution map) to a
-- full counterexample environment. With this, the variables are indexed by
-- `BindId` and they contain also their refinement type and user info.
substToCexEnv :: SInfo info -> SubcId -> Subst -> CexEnv (SubcId, info)
substToCexEnv si subcid (Su sub) = benv { beBinds = binds }
where
benv = bs si
horn = cm si Map.! subcid
ibenv = senv horn

-- The constraint head symbol and refinement
(csym, creft, _) = beBinds benv Map.! cbind horn

symbols = exprSymbolsSet $ crhs horn
symRefts = Map.fromList $ clhs benv horn
-- Make sure the rhs is always in the relevant set!
relevant = Map.insert csym creft
-- Get the relevant bindings i.e. those that affect the outcome of the rhs.
$ dropLikelyIrrelevantBindings symbols symRefts

-- This new substitution map contains only the relevant bindings.
sub' = Map.intersectionWith const sub relevant

-- Filter out all bind ids that are not in the constraint. Then map the
-- symbols back to the bind ids.
binds = Map.mapMaybe trans
. Map.filterWithKey (\bid _ -> memberIBindEnv bid ibenv)
. beBinds
$ benv

-- Extends a symbol from the bind environment with a concrete instance (and
-- the subcid, but this just there to match the type signature of `Result`
-- later down the line).
trans (sym, sreft, info) = extend <$> Map.lookup sym sub'
where
extend ex = (sym, sreft, (ex, (subcid, info)))
Loading
Loading