Advertisement
Guest User

UPrNNYqX6LU

a guest
Jan 1st, 2018
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1.  
  2. #include "share/atspre_staload.hats"
  3.  
  4. fn
  5. {a,b:vt@ype}
  6. cloptr_clear
  7. (f: a -<cloptr1> b ): void =
  8. cloptr_free($UNSAFE.castvwtp0{cloptr(void)}(f))
  9.  
  10. datavtype
  11. Step
  12. (s:t@ype+,a:t@ype+) =
  13. | Yield of (s,a) | Skip of (s) | Done
  14.  
  15. datatype
  16. Step_ = Yield_ | Skip_ | Done_
  17.  
  18. datavtype
  19. Stream (s:t@ype+,a:t@ype+) =
  20. Stream of (s, (&s >> s, &a >> a) -<cloptr1> Step_)
  21.  
  22. typedef ExpState = @(int, double)
  23.  
  24. fn
  25. expdecay
  26. (
  27. N0 : int,
  28. dt : double,
  29. l0 : double,
  30. mx : &double >> _,
  31. st : &ExpState >> _
  32. ) : Step_ = let
  33. //
  34. val n0 = st.0
  35. //
  36. in
  37. //
  38. if
  39. (n0 >= N0)
  40. then Done_
  41. else let
  42. //
  43. val x0 = st.1
  44. //
  45. (*
  46. val () = println! ("n0 = ", n0)
  47. val () = println! ("x0 = ", x0)
  48. *)
  49. //
  50. val n1 = n0 + 1
  51. val x1 = x0 + dt*l0*x0
  52. val () = mx := x1
  53. val () = st.0 := n1
  54. val () = st.1 := x1
  55. //
  56. in Yield_ end // end-of-if
  57. //
  58. end // end of [let] // expdecay
  59.  
  60. fn
  61. {s,a:t@ype}
  62. stream_clear
  63. (
  64. S0: Stream(s,a)
  65. ) : void =
  66. cloptr_free{void}
  67. (
  68. $UNSAFE.castvwtp0(f0)
  69. ) where { val+~Stream(_,f0) = S0 }
  70.  
  71. fun
  72. {s,a:t@ype}
  73. getlast
  74. (
  75. str : !Stream(s,a)
  76. ) : Option_vt(a) = opt
  77. where {
  78. fun
  79. loop
  80. (f0: !
  81. (&s >> s, &a >> a) -<cloptr1> Step_,
  82. t0: int, st: &s >> s, mx: &a >> a
  83. ): Option_vt(a) =
  84. (
  85. case f0(st, mx) of
  86. | Skip_() => loop(f0, t0, st, mx)
  87. | Done_() =>
  88. if t0 > 0
  89. then Some_vt(mx) else None_vt()
  90. | Yield_() => loop(f0, 1, st, mx)
  91. )
  92. val+@Stream(st, f0) = str
  93. var mx = $UNSAFE.cast{a}(0)
  94. val opt = loop(f0, 0, st, mx)
  95. prval () = fold@(str)
  96. }
  97.  
  98. implement main0 () = println!(x)
  99. where
  100. {
  101. val N0 = 100000000
  102. val dt = 1.0/4800000.0
  103. val x0 = 1.0
  104. val l0 = ~0.5
  105. val f0 =
  106. lam (st : &ExpState >> _, mx: &double >> _): Step_ =<cloptr1> expdecay(N0, dt, l0, mx, st)
  107. val str = Stream(@(0,x0),f0)
  108. val- ~Some_vt(x) = getlast<ExpState,double>(str)
  109. val () = stream_clear(str)
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement