Skip to content

Commit

Permalink
KD: Add sum c example
Browse files Browse the repository at this point in the history
  • Loading branch information
LeventErkok committed Sep 17, 2024
1 parent e658613 commit b7efbb7
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Documentation/SBV/Examples/KnuckleDragger/Induction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@ import Data.SBV.Tools.KnuckleDragger
-- >>> import Data.SBV.Tools.KnuckleDragger(runKD)
#endif

-- | Prove that sum of constants @c@ from @0@ to @n@ is @n*c@.
--
-- We have:
--
-- >>> runKD sumConstProof
-- Lemma: sumConst_correct Q.E.D.
sumConstProof :: KD Proof
sumConstProof = do
let sum :: SInteger -> SInteger -> SInteger
sum = smtFunction "sum" $ \n c -> ite (n .== 0) 0 (c + sum (n-1) c)

spec :: SInteger -> SInteger -> SInteger
spec n c = n * c

p :: SInteger -> SInteger -> SBool
p n c = observe "imp" (sum n c) .== observe "spec" (spec n c)

lemma "sumConst_correct" (\(Forall @"n" n) (Forall @"c" c) -> n .>= 0 .=> p n c) [induct2 p]

-- | Prove that sum of numbers from @0@ to @n@ is @n*(n-1)/2@.
--
-- We have:
Expand Down

0 comments on commit b7efbb7

Please sign in to comment.