Guest User

Fibonorial.dats

a guest
Dec 2nd, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. (*
  2. **
  3. ** Fibonorial(verify)
  4. **
  5. ** The given implementation is of a particular
  6. ** programming style in ATS, combining programming with
  7. ** theorem-proving in a syntactically intertwined manner
  8. **
  9. *)
  10.  
  11. (* ****** ****** *)
  12.  
  13. #include
  14. "share/atspre_define.hats"
  15. #include
  16. "share/atspre_staload.hats"
  17.  
  18. (* ****** ****** *)
  19.  
  20. #define ATS_MAINATSFLAG 1
  21. #define ATS_DYNLOADNAME "my_dynload"
  22.  
  23. dataprop
  24. FIB (int, int) =
  25. | FIB0 (0, 0)
  26. | FIB1 (1, 1)
  27. | {n:nat}
  28. {r0,r1:int}
  29. FIB2 (n+2, r0+r1) of (FIB (n, r0), FIB (n+1, r1))
  30. // end of [FIB] // end of [dataprop]
  31.  
  32. (* ****** ****** *)
  33. //
  34. fun
  35. fibats{n:nat}
  36. (n: int (n))
  37. : [r:int] (FIB (n, r) | int r) = let
  38. fun loop
  39. {i:nat | i <= n}{r0,r1:int}
  40. (
  41. pf0: FIB (i, r0), pf1: FIB (i+1, r1)
  42. | ni: int (n-i), r0: int r0, r1: int r1
  43. ) : [r:int] (FIB (n, r) | int r) =
  44. if (ni > 0)
  45. then loop{i+1}(pf1, FIB2 (pf0, pf1) | ni - 1, r1, r0 + r1)
  46. else (pf0 | r0)
  47. // end of [if]
  48. // end of [loop]
  49. in
  50. loop {0} (FIB0 (), FIB1 () | n, 0, 1)
  51. end // end of [fibats]
  52. //
  53. (* ****** ****** *)
  54. //
  55. val N = 10
  56. val (pf | res) = fibats(N)
  57. val () = println! ("fibats(", N, ") = ", res)
  58. //
  59. (* ****** ****** *)
  60.  
  61. dataprop
  62. FIBONORIAL (int, int) =
  63. | FIBONORIAL0 (0, 1)
  64. | {n: nat} {r1,r2: int}
  65. FIBONORIALN (n+1, r1*r2) of (FIB(n+1, r1), FIBONORIAL(n, r2))
  66. // end of [FIBONORIAL]
  67.  
  68. (* ****** ****** *)
  69.  
  70. fun
  71. fibonorialats
  72. {n:nat}
  73. (
  74. n: int (n)
  75. ) : [r:int] (FIBONORIAL(n,r) | int r) = let
  76. //
  77. fun loop
  78. {i:nat | i <= n}
  79. {r0,r1,r2 : int}
  80. (
  81. pf0: FIB(i,r0), pf1: FIB(i+1, r1), pf2: FIBONORIAL(i, r2)
  82. | iterationsLeft: int (n-i), r0: int r0, r1: int r1, r2: int r2
  83. ) : [r:int] (FIBONORIAL(n,r) | int r) =
  84. if (iterationsLeft > 0)
  85. then loop{i+1}(pf1, FIB2(pf0, pf1), FIBONORIALN(pf1, pf2) | iterationsLeft-1, r1, r0+r1, r1 * r2)
  86. else (pf2 | r2)
  87. // end of [if]
  88. in
  89. loop{0}(FIB0(), FIB1(), FIBONORIAL0() | n, 0, 1, 1)
  90. end // end of [fibonorialats]
  91.  
  92. (* ****** ****** *)
  93. //
  94. val N = 10
  95. val (pf | res) = fibonorialats(N)
  96. val () = println! ("fibonorialats(", N, ") = ", res)
  97. //
  98. (* ****** ****** *)
  99.  
  100. implement main0 () = ()
  101.  
  102. (* ****** ****** *)
  103.  
  104. (* end of [Fibonorial.dats] *)
Advertisement
Add Comment
Please, Sign In to add comment