-----------------------------------------------------------------------------
-- |
-- Module    : Documentation.SBV.Examples.Lists.Nested
-- Copyright : (c) Levent Erkok
-- License   : BSD3
-- Maintainer: erkokl@gmail.com
-- Stability : experimental
--
-- Demonstrates nested lists
-----------------------------------------------------------------------------

{-# LANGUAGE OverloadedLists     #-}
{-# LANGUAGE ScopedTypeVariables #-}

{-# OPTIONS_GHC -Wall -Werror #-}

module Documentation.SBV.Examples.Lists.Nested where

import Data.SBV
import Data.SBV.Control

import Data.SBV.List ((.!!))
import qualified Data.SBV.List as L

-- | Simple example demonstrating the use of nested lists. We have:
--
-- Turned off. See: https://github.com/Z3Prover/z3/issues/2820
-- nestedExample
-- [[1,2,3],[4,5,6,7],[8,9,10],[11,12,13]]
nestedExample :: IO ()
nestedExample :: IO ()
nestedExample = Symbolic () -> IO ()
forall a. Symbolic a -> IO a
runSMT (Symbolic () -> IO ()) -> Symbolic () -> IO ()
forall a b. (a -> b) -> a -> b
$ do SList [Integer]
a :: SList [Integer] <- String -> Symbolic (SList [Integer])
forall a. SymVal a => String -> Symbolic (SBV a)
free "a"

                            SBool -> Symbolic ()
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain (SBool -> Symbolic ()) -> SBool -> Symbolic ()
forall a b. (a -> b) -> a -> b
$ SList [Integer]
a SList [Integer] -> SInteger -> SBV [Integer]
forall a. SymVal a => SList a -> SInteger -> SBV a
.!! 0 SBV [Integer] -> SBV [Integer] -> SBool
forall a. EqSymbolic a => a -> a -> SBool
.== [1, 2, 3]
                            SBool -> Symbolic ()
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain (SBool -> Symbolic ()) -> SBool -> Symbolic ()
forall a b. (a -> b) -> a -> b
$ SList [Integer]
a SList [Integer] -> SInteger -> SBV [Integer]
forall a. SymVal a => SList a -> SInteger -> SBV a
.!! 1 SBV [Integer] -> SBV [Integer] -> SBool
forall a. EqSymbolic a => a -> a -> SBool
.== [4, 5, 6, 7]
                            SBool -> Symbolic ()
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain (SBool -> Symbolic ()) -> SBool -> Symbolic ()
forall a b. (a -> b) -> a -> b
$ SList [Integer] -> SList [Integer]
forall a. SymVal a => SList a -> SList a
L.tail (SList [Integer] -> SList [Integer]
forall a. SymVal a => SList a -> SList a
L.tail SList [Integer]
a) SList [Integer] -> SList [Integer] -> SBool
forall a. EqSymbolic a => a -> a -> SBool
.== [[8, 9, 10], [11, 12, 13]]
                            SBool -> Symbolic ()
forall (m :: * -> *). SolverContext m => SBool -> m ()
constrain (SBool -> Symbolic ()) -> SBool -> Symbolic ()
forall a b. (a -> b) -> a -> b
$ SList [Integer] -> SInteger
forall a. SymVal a => SList a -> SInteger
L.length SList [Integer]
a SInteger -> SInteger -> SBool
forall a. EqSymbolic a => a -> a -> SBool
.== 4

                            Query () -> Symbolic ()
forall a. Query a -> Symbolic a
query (Query () -> Symbolic ()) -> Query () -> Symbolic ()
forall a b. (a -> b) -> a -> b
$ do CheckSatResult
cs <- Query CheckSatResult
checkSat
                                       case CheckSatResult
cs of
                                         Unk   -> String -> Query ()
forall a. HasCallStack => String -> a
error "Solver said unknown!"
                                         Unsat -> IO () -> Query ()
forall a. IO a -> Query a
io (IO () -> Query ()) -> IO () -> Query ()
forall a b. (a -> b) -> a -> b
$ String -> IO ()
putStrLn "Unsat"
                                         Sat   -> do [[Integer]]
v <- SList [Integer] -> Query [[Integer]]
forall a. SMTValue a => SBV a -> Query a
getValue SList [Integer]
a
                                                     IO () -> Query ()
forall a. IO a -> Query a
io (IO () -> Query ()) -> IO () -> Query ()
forall a b. (a -> b) -> a -> b
$ [[Integer]] -> IO ()
forall a. Show a => a -> IO ()
print [[Integer]]
v