Skip to content

Instantly share code, notes, and snippets.

@m4lvin
Created September 21, 2018 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m4lvin/54fe773cf7c813354c0889a5bf2bc2e7 to your computer and use it in GitHub Desktop.
Save m4lvin/54fe773cf7c813354c0889a5bf2bc2e7 to your computer and use it in GitHub Desktop.
some stack extra-deps methods break stack ghci
Notes
With the below three files `stack build` and `stack exec queens` both work but `stack ghci` fails.
Still, `stack exec ghci` and then `import Data.HasCacBDD` work, but calling any function from the library fails.
Changing stack.yaml to "stack.yaml WORKING" below makes everything work.
So the question is, why does
============ stack.yaml ============
resolver: lts-12.9
packages:
- '.'
# stack ghci does not work:
extra-deps:
- HasCacBDD-0.1.0.0
# stack ghci does work:
extra-deps:
- git: https://github.com/m4lvin/HasCacBDD.git
commit: fec7e64bd1d87d456cfe3abcb057563a3d789215
# stack ghci does work
extra-deps:
- https://hackage.haskell.org/package/HasCacBDD-0.1.0.0/HasCacBDD-0.1.0.0.tar.gz
============ queens.cabal ============
name: queens
version: 1.0.0
author: Malvin Gattinger
maintainer: malvin@w4eg.eu
category: Logic
build-type: Simple
cabal-version: >=1.23
Executable queens
ghc-options: -Wall -O2
default-language: Haskell2010
main-is: Main.hs
build-depends: base >= 4.8 && < 5,
HasCacBDD,
directory,
filepath,
process
============ Main.hs ============
{-
Copied and adapted from https://github.com/jwaldmann/haskell-obdd/blob/master/examples/Queens.hs
The N Queens problem. The propositional variables correspond to the positions on the board.
-}
import Data.HasCacBDD
import Data.List
import System.Environment ( getArgs )
type Position = (Int,Int)
positions :: Int -> [Position]
positions n = [ (a,b) | a <- [1 .. n], b <- [1 .. n] ]
pos2int :: Int -> Position -> Int
pos2int n p = k where Just k = elemIndex p (positions n)
pos2bdd :: Int -> Position -> Bdd
pos2bdd n p = var $ pos2int n p
threatens :: Position -> Position -> Bool
threatens (a,b) (c,d) =
a == c -- same column
|| b == d -- same row
|| a+b == c+d -- same diagonal
|| a-b == c-d -- same antidiagonal
board :: Int -> Bdd
board n = each_column_is_occupied `con` no_threats where
each_column_is_occupied = conSet [ disSet [pos2bdd n (col,row) | row <- [1..n] ] | col <- [1..n] ]
no_threats = conSet [ disSet [neg (pos2bdd n p1), neg (pos2bdd n p2) ]
| p1 <- positions n, p2 <- positions n, p1 < p2, p1 `threatens` p2 ]
main :: IO ()
main = do
args <- getArgs
case map read args :: [Int] of
[arg] -> mainf arg
_ -> mainf 8
mainf :: Int -> IO ()
mainf n = do
let d = board n
print $ Data.HasCacBDD.satCountWith (map snd $ positions n) d
showBoard :: Assignment -> String
showBoard as = unlines [ [ charAt(col,row) | col <- [1..n] ] | row <- [1..n] ] where
charAt pos = if Just True == lookup (pos2int n pos) as then 'X' else '.'
n = round $ sqrt (fromIntegral $ length as :: Float)
@m4lvin
Copy link
Author

m4lvin commented Jun 20, 2019

This problem should be solved since HasCacBDD-0.1.0.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment