{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedLists #-}
module Documentation.SBV.Examples.WeakestPreconditions.Append where
import Data.SBV
import Data.SBV.Control
import Data.SBV.List ((.++))
import qualified Data.SBV.List as L
import Data.SBV.Tools.WeakestPreconditions
import GHC.Generics (Generic)
data AppS a = AppS { xs :: SList a
, ys :: SList a
, ts :: SList a
, zs :: SList a
}
deriving (Generic, Mergeable)
data AppC a = AppC [a] [a] [a] [a]
instance {-# OVERLAPS #-} (SymVal a, Show a) => Show (AppS a) where
show (AppS xs ys ts zs) = "{xs = " ++ sh xs ++ ", ys = " ++ sh ys ++ ", ts = " ++ sh ts ++ ", zs = " ++ sh zs ++ "}"
where sh v = case unliteral v of
Nothing -> "<symbolic>"
Just i -> show i
instance Show a => Show (AppC a) where
show (AppC xs ys ts zs) = "{xs = " ++ show xs ++ ", ys = " ++ show ys ++ ", ts = " ++ show ts ++ ", zs = " ++ show zs ++ "}"
instance Queriable IO (AppS Integer) (AppC Integer) where
create = AppS <$> freshVar_ <*> freshVar_ <*> freshVar_ <*> freshVar_
project (AppS xs ys ts zs) = AppC <$> getValue xs <*> getValue ys <*> getValue ts <*> getValue zs
embed (AppC xs ys ts zs) = return $ AppS (literal xs) (literal ys) (literal ts) (literal zs)
type A = AppS Integer
algorithm :: Stmt A
algorithm = Seq [ Assign $ \st -> st{zs = []}
, Assign $ \st@AppS{xs} -> st{ts = xs}
, loop "xs" (\AppS{xs, zs, ts} -> xs .== zs .++ ts)
, Assign $ \st@AppS{ys} -> st{ts = ys}
, loop "ys" (\AppS{xs, ys, zs, ts} -> xs .++ ys .== zs .++ ts)
]
where loop w inv = While ("walk over " ++ w)
inv
(Just (\AppS{ts} -> [L.length ts]))
(\AppS{ts} -> sNot (L.null ts))
$ Seq [ Assign $ \st@AppS{ts, zs} -> st{zs = zs `L.snoc` L.head ts}
, Assign $ \st@AppS{ts} -> st{ts = L.tail ts }
]
imperativeAppend :: Program A
imperativeAppend = Program { setup = return ()
, precondition = const sTrue
, program = algorithm
, postcondition = postcondition
, stability = noChange
}
where
postcondition :: A -> SBool
postcondition AppS{xs, ys, zs} = zs .== xs .++ ys
noChange = [stable "xs" xs, stable "ys" ys]
correctness :: IO (ProofResult (AppC Integer))
correctness = wpProveWith defaultWPCfg{wpVerbose=True} imperativeAppend