Skip to content

Instantly share code, notes, and snippets.

@doublec
Created October 6, 2019 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doublec/0681ee0672a6fa4d11b1206f9e4c1477 to your computer and use it in GitHub Desktop.
Save doublec/0681ee0672a6fa4d11b1206f9e4c1477 to your computer and use it in GitHub Desktop.
ATS proofs about factorial using dataprop
// ATS factorial proof examples, based on F* tutorial examples at:
// https://www.fstar-lang.org/tutorial/
//
// patsopt -tc --constraint-export -d fact_prop.dats |patsolve_z3 -i
//
#include "share/atspre_define.hats"
#include "share/atspre_staload.hats"
dataprop FACT (int, int) =
| FACTzero (0, 1)
| {n,r1:int | n > 0} FACTsucc (n, r1 * n) of (FACT (n-1, r1))
fun fact {n:nat} (n: int n): [r:int] (FACT (n, r) | int r) =
if n > 0 then let
val (pf1 | r1) = fact (n - 1)
val r = n * r1
in
(FACTsucc (pf1) | r)
end else begin
(FACTzero () | 1)
end
prfun factorial_is_positive{n:nat} .<n>.(n: int n):<> [r:int | r > 0] FACT(n,r) =
case n of
| 0 => FACTzero()
| _ =>> FACTsucc(factorial_is_positive (n - 1))
prfun factorial_is_greater_than_arg{n:nat | n > 2} .<n>.(n: int n):<> [r:int | r > n] FACT(n,r) =
case n of
| 3 => FACTsucc(FACTsucc(FACTsucc(FACTzero())))
| _ =>> FACTsucc(factorial_is_greater_than_arg (n - 1))
implement main0() = let
val (pf | r) = fact(5)
in
println!("5! = ", r)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment