I'm trying to solve an optimisation problem with sbv in Haskell, but get a compiler error.
The solution is a list of values, and I have a function to check the solution is valid (the constraint), and a function to calculate a number to minimize.
I get this compiler error on my minimal example:
/home/t/sbvExample/Main.hs:28:5: error:
• No instance for (S.SymVal S.SBool)
arising from a use of ‘Sl.length’
• In the expression: Sl.length xs
In an equation for ‘toNum’: toNum xs = Sl.length xs
|
28 | Sl.length xs
| ^^^^^^^^^^^^
Here's the code:
{-# LANGUAGE ScopedTypeVariables #-}
module Main (main) where
import qualified Data.SBV.List as Sl
import qualified Data.SBV as S
main :: IO ()
main = do
result <- S.optimize S.Lexicographic help
print result
help :: S.SymbolicT IO ()
help = do
xs :: S.SList S.SBool <- S.sList "xs"
S.constrain $ isValid xs
S.minimize "goal" $ toNum xs
isValid :: S.SList S.SBool -> S.SBool
isValid xs =
Sl.length xs S..> 0
toNum :: S.SList S.SBool -> S.SInteger
toNum xs =
Sl.length xs
So in this silly minimal example I would expect a list containing one item.
For convenience I put it on Github, so build it with:
git clone https://github.com/8n8/sbvExample
cd sbvExample
stack build
You're getting this error message because there is no instance of SymVal for
SBool
, only forBool
, andS.length
expects aS.SList
ofSymVal
values:You can fix this by changing
toNum
andisValid
to accept yS.SList Bool
, and changing the type ofxs
toS.SList Bool
:Your
isValid
andtoNum
functions are also overly specialized, since they only require aSymVal
class constraint. The following is more general and still compiles:EDIT
Weren't it for
toNum
failing to typecheck, you would have also seen thatS.sList
also doesn't typecheck, since its type signature has aSymVal
constraint on the type parameter of the returnedS.SList
:Deleting
isValid
andtoNum
, and only keeping theS.sList
constructor:throws this error:
Which, in my opinion, tells a bit more about the actual issue. It just goes to show that a minimal example can sometimes be even more minimal, and therefore more helpful.