// // Course: BU CAS CS 520, Fall 2010 // Instructor: Hongwei Xi (hwxi AT cs DOT bu DOT edu) // Lecture on Tuesday, Sep. 7, 2010 // // // How to compile: // atscc-o tally0 tally0.dats // How to test: // ./tally0 // (* ****** ****** *) (* #atsdoc_dataprop() #atsdoc_text(" This dataprop is for encoding the relation of the following funcition: #atsdoc_table('\ tally(0) | = | 0 tally(n) | = | n + tally(n-1) if n >= 1 ') ") *) dataprop TALLY (int, int) = | TALLYbas (0, 0) of () | {n:nat} {s:int} TALLYind (n+1, s+n+1) of TALLY (n, s) // end of [TALLY] (* // HX: a standard implementation fun tally0 (n: int): int = if n > 0 then n + tally0 (n-1) else 0 // end of [tally0] *) // // HX: programming with theorem-proving // (* #atsdoc_fun() *) fun tally0 {n:nat} (n: int n): [s:int] (TALLY (n, s) | int (s)) = if n > 0 then let val (pf1 | ans1) = tally0 (n-1) prval pf = TALLYind (pf1) val ans = ans1 + n in (pf | ans) end else (TALLYbas | 0) // end of [tally0] implement main () = () where { #define N 100 val (pf | ans) = tally0 (N) val () = printf ("sum(1 ... %i) = %i\n", @(N, ans)) } // end [main] (* ****** ****** *) (* end of [tally0.dats] *)