Skip to content

Commit

Permalink
feat(ldfi2): introduce skeleton for translating formulae to sat
Browse files Browse the repository at this point in the history
  • Loading branch information
symbiont-stevan-andjelkovic committed Feb 10, 2021
1 parent 0c27ca9 commit f248786
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
6 changes: 4 additions & 2 deletions src/ldfi2/ldfi.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ tested-with: GHC ==8.10.3
library
hs-source-dirs: src/
exposed-modules: Ldfi
-- GHC boot libraries
-- GHC boot library dependencies:
-- (https://gitlab.haskell.org/ghc/ghc/-/blob/master/packages)
build-depends:
base >=4.14 && <4.15
, containers
, z3
-- Other dependencies:
build-depends:
z3
default-language: Haskell2010

test-suite test
Expand Down
46 changes: 30 additions & 16 deletions src/ldfi2/src/Ldfi.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{-# LANGUAGE DeriveFoldable #-}

module Ldfi where

import Data.Set (Set)
import qualified Data.Set as Set
import Numeric.Natural
import Z3.Monad

------------------------------------------------------------------------
-- * Traces
Expand Down Expand Up @@ -34,26 +37,32 @@ edges = foldMap (\e -> Set.singleton (from e, to e))
infixr 3 :&&
infixr 2 :||

data Formula
data FormulaF var
= Formula :&& Formula
| Formula :|| Formula
| And [Formula]
| Or [Formula]
| Neg Formula
| TT
| FF
| Var String
deriving (Eq, Show)

simplify :: Formula -> Formula
simplify (TT :&& r) = simplify r
simplify (l :&& r) = simplify l :&& simplify r
simplify (FF :|| r) = simplify r
simplify (l :|| TT) = simplify l
simplify (l :|| r) = simplify l :|| simplify r
simplify (And []) = TT
simplify (And [f]) = f
simplify (And fs) = And (map simplify fs)
| Var var
deriving (Eq, Show, Foldable)

type Formula = FormulaF String

getVars :: Ord var => FormulaF var -> Set var
getVars = foldMap Set.singleton

simplify1 :: Formula -> Formula
simplify1 (TT :&& r) = simplify1 r
simplify1 (l :&& r) = simplify1 l :&& simplify1 r
simplify1 (FF :|| r) = simplify1 r
simplify1 (l :|| TT) = simplify1 l
simplify1 (l :|| r) = simplify1 l :|| simplify1 r
simplify1 (And []) = TT
simplify1 (And [f]) = f
simplify1 (And fs) = And (map simplify1 fs)
simplify1 f = f

-- simplify (TT :&& r) = simplify r
-- simplify (And xs :&& y) = And (map simplify xs ++ [simplify y])
Expand All @@ -68,11 +77,10 @@ simplify (And fs) = And (map simplify fs)
-- expandAnd (And xs) = xs
-- expandAnd (l :&& r) = [l, r]
-- expandAnd f = [f]
simplify f = f

fixpoint :: Formula -> Formula
fixpoint f | simplify f == f = f
| otherwise = fixpoint (simplify f)
fixpoint f | simplify1 f == f = f
| otherwise = fixpoint (simplify1 f)

vars :: Set String -> Formula
vars = And . map Var . Set.toList
Expand Down Expand Up @@ -109,3 +117,9 @@ ldfi' ts =

ldfi :: [Trace] -> Formula
ldfi = fixpoint . ldfi'

------------------------------------------------------------------------
-- * SAT formula

toSatAst :: MonadZ3 z3 => Formula -> z3 AST
toSatAst = undefined

0 comments on commit f248786

Please sign in to comment.