-----------------------------------------------------------------------------
-- |
-- 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 Prelude hiding ((!!))
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 = forall a. Symbolic a -> IO a
runSMT forall a b. (a -> b) -> a -> b
$ do SList [Integer]
a :: SList [Integer] <- forall a. SymVal a => String -> Symbolic (SBV a)
free String
"a"

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

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