-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from bruderj15/107-smart-ite
107 smart ite
- Loading branch information
Showing
11 changed files
with
245 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
{-# LANGUAGE DerivingStrategies #-} | ||
{-# LANGUAGE UndecidableInstances #-} | ||
|
||
module Language.Hasmtlib.Example.McCarthyArrayAxioms where | ||
|
||
import Language.Hasmtlib | ||
import Control.Monad | ||
import Data.List (groupBy, sortOn, transpose) | ||
import Data.Function (on) | ||
|
||
main :: IO () | ||
main = do | ||
(res, sol) <- solveWith @SMT (solver z3) $ nqueens @IntSort 4 | ||
case res of | ||
Sat -> case sol of | ||
Nothing -> putStrLn "No solution" | ||
Just board -> forM_ board print | ||
r -> print r | ||
|
||
nqueens :: forall t s m. (MonadSMT s m, KnownSMTSort t, Num (Expr t), Orderable (Expr t)) => Int -> m [[Expr t]] | ||
nqueens n = do | ||
setLogic $ case sortSing @t of | ||
SIntSort -> "QF_LIA" | ||
SRealSort -> "QF_LRA" | ||
SBvSort _ _ -> "QF_BV" | ||
_ -> "ALL" | ||
|
||
board <- replicateM n $ replicateM n var | ||
|
||
forM_ (concat board) $ assert . queenDomain | ||
forM_ board $ assert . ((=== 1) . sum) | ||
forM_ (transpose board) $ assert . ((=== 1) . sum) | ||
forM_ (diagonals board) $ assert . ((<=? 1) . sum) | ||
|
||
return board | ||
|
||
queenDomain ::(Equatable (Expr t), Num (Expr t)) => Expr t -> Expr BoolSort | ||
queenDomain f = (f === 0) `xor` (f === 1) | ||
|
||
diagonals :: [[a]] -> [[a]] | ||
diagonals mat = diagonals1 mat ++ diagonals2 mat | ||
|
||
indexedMatrix :: [[a]] -> [((Int, Int), a)] | ||
indexedMatrix mat = [((i, j), val) | (i, row) <- zip [0..] mat, (j, val) <- zip [0..] row] | ||
|
||
diagonals1 :: [[a]] -> [[a]] | ||
diagonals1 mat = map (map snd) . groupBy ((==) `on` (uncurry (-) . fst)) . sortOn (uncurry (-) . fst) $ indexedMatrix mat | ||
|
||
diagonals2 :: [[a]] -> [[a]] | ||
diagonals2 mat = map (map snd) . groupBy ((==) `on` (uncurry (+) . fst)) . sortOn (uncurry (+) . fst) $ indexedMatrix mat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,20 @@ | ||
module Language.Hasmtlib.Example.IncrementalOptimization where | ||
|
||
import Prelude hiding ((&&)) | ||
import Language.Hasmtlib | ||
|
||
main :: IO () | ||
main = do | ||
res <- solveWith @OMT (solver $ debugging statistically z3) $ do | ||
res <- solveWith @OMT (solver $ debugging verbosely z3) $ do | ||
setLogic "QF_LIA" | ||
|
||
x <- var @IntSort | ||
y <- var @IntSort | ||
|
||
assert $ x >? -2 | ||
assertSoftWeighted (x >? -1) 5.0 | ||
assert $ x <? 10 && y <? 5 && (y <? 7 ==> x === 1) | ||
|
||
minimize x | ||
maximize $ x + y | ||
|
||
return x | ||
return (x,y) | ||
|
||
print res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.