Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
bostoncc/artifacts/maxvonHippel/ivy-tutorial/summation.ivy
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
39 lines (31 sloc)
747 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #lang ivy1.8 | |
| include numbers | |
| include collections | |
| module letsDoMath = { | |
| # Sum_{i=0}^N i = i (i + 1) / 2 | |
| # Proof Sketch: | |
| # Base Case: N=0 | |
| # Sum_{i=0}^0 i = 0 = 0 (0 + 1) / 2 = 0 | |
| # Inductive Step: | |
| # Suppose that for all i <= j, | |
| # Sum_{i=0}^N i = i (i + 1) / 2. | |
| # Then Sum_{i=0}^{N+1} i = N (N + 1) / 2 + (N + 1) | |
| # = ( N (N + 1) + 2N + 2 ) / 2 | |
| # = ( N^2 + N + 2N + 2 ) / 2 | |
| # = ( N^2 + 3N + 2 ) / 2 | |
| # = (N + 1)((N + 1) + 1) / 2 | |
| # QED. | |
| var steps : nat | |
| var sum : nat | |
| after init { | |
| steps := 0; | |
| sum := 0; | |
| } | |
| action step = { | |
| steps := steps + 1; | |
| sum := sum + steps; | |
| } | |
| invariant sum = (steps * (steps + 1) ) / 2 | |
| } | |
| instance ldm : letsDoMath | |
| export ldm.step |