(* // This file is for Assignment 3, BU CAS CS 520, Fall, 2009 // // Instructor: Hongwei Xi (hwxi AT cs DOT bu DOT edu) // *) (* ****** ****** *) datasort num = // abstract dataprop EQUAL (num, num) = {x:num} EQUAL (x, x) abst@ype N (num) (* ****** ****** *) absprop ONE_num (num) val one: [x:num] (ONE_num x | N x) praxi one_isfun {x1,x2:num} (pf1: ONE_num x1, pf2: ONE_num x2): EQUAL (x1, x2) praxi one_istot (): [x:num] ONE_num x (* ****** ****** *) absprop MUL_num (num, num, num) praxi mul_isfun {x1,x2:num} {x,x':num} (pf1: MUL_num (x1, x2, x), pf2: MUL_num (x1, x2, x')): EQUAL (x, x') praxi mul_istot {x1,x2:num} (): [x:num] MUL_num (x1, x2, x) praxi mul_one_unit_l {x1,x2,x3:num} (pf1: ONE_num x1, pf2: MUL_num (x1, x2, x3)): EQUAL (x2, x3) praxi mul_one_unit_r {x1,x2,x3:num} (pf1: ONE_num x1, pf2: MUL_num (x2, x1, x3)): EQUAL (x2, x3) praxi mul_assoc {x1,x2,x3,x12,x23,x123:num} ( pf12: MUL_num (x1, x2, x12), pf23: MUL_num (x2, x3, x23), pf123: MUL_num (x12, x3, x123) ) : MUL_num (x1, x23, x123) (* ****** ****** *) fun mul_num_num {x1,x2:num} (_: N x1, _: N x2):<> [x:num] (MUL_num (x1, x2, x) | N x) overload * with mul_num_num (* ****** ****** *) dataprop POWER (num, int, num) = | {x:num} {r:num} POWERbas (x, 0, r) of ONE_num r | {x:num} {n:nat} {r,r1:num} POWERind (x, n+1, r1) of (POWER (x, n, r), MUL_num (x, r, r1)) (* ****** ****** *) prfun power_isfun {x:num} {n:nat} {r1,r2:num} (pf1: POWER (x, n, r1), pf2: POWER (x, n, r2)): EQUAL (r1, r2) // end of [power_isfun] (* ****** ****** *) prfun power_lemma1 {x:num} {n:nat} {xn,xnx:num} (pf1: POWER (x, n, xn), pf2: MUL_num (xn, x, xnx)):<> POWER (x, n+1, xnx) prfun power_lemma2 {x,xx:num} {n:nat} {xxn:num} (pf1: MUL_num (x, x, xx), pf2: POWER (xx, n, xxn)):<> POWER (x, n+n, xxn) prfun power_lemma3 {x:num} {n1:nat} {n2:nat} {xn1,xn2,xn:num} ( pf1: POWER (x, n1, xn1) , pf2: POWER (x, n2, xn2) , pf3: MUL_num (xn1, xn2, xn) ) :<> POWER (x, n1+n2, xn) (* ****** ****** *) // please implement the following function // -- 20 points for a correct implementation that is O(log n) // -- 20 additional points for a correct implementation that is tail-recursive and O(log n) fun power {x:num} {n:nat} (x: N x, n: int n):<> [r:num] (POWER (x, n, r) | N r) (* ****** ****** *) (* end of [power.sats] *)