Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "share/atspre_staload.hats"
- fn
- {a,b:vt@ype}
- cloptr_clear
- (f: a -<cloptr1> b ): void =
- cloptr_free($UNSAFE.castvwtp0{cloptr(void)}(f))
- datavtype
- Step
- (s:t@ype+,a:t@ype+) =
- | Yield of (s,a) | Skip of (s) | Done
- datatype
- Step_ = Yield_ | Skip_ | Done_
- datavtype
- Stream (s:t@ype+,a:t@ype+) =
- Stream of (s, (&s >> s, &a >> a) -<cloptr1> Step_)
- typedef ExpState = @(int, double)
- fn
- expdecay
- (
- N0 : int,
- dt : double,
- l0 : double,
- mx : &double >> _,
- st : &ExpState >> _
- ) : Step_ = let
- //
- val n0 = st.0
- //
- in
- //
- if
- (n0 >= N0)
- then Done_
- else let
- //
- val x0 = st.1
- //
- (*
- val () = println! ("n0 = ", n0)
- val () = println! ("x0 = ", x0)
- *)
- //
- val n1 = n0 + 1
- val x1 = x0 + dt*l0*x0
- val () = mx := x1
- val () = st.0 := n1
- val () = st.1 := x1
- //
- in Yield_ end // end-of-if
- //
- end // end of [let] // expdecay
- fn
- {s,a:t@ype}
- stream_clear
- (
- S0: Stream(s,a)
- ) : void =
- cloptr_free{void}
- (
- $UNSAFE.castvwtp0(f0)
- ) where { val+~Stream(_,f0) = S0 }
- fun
- {s,a:t@ype}
- getlast
- (
- str : !Stream(s,a)
- ) : Option_vt(a) = opt
- where {
- fun
- loop
- (f0: !
- (&s >> s, &a >> a) -<cloptr1> Step_,
- t0: int, st: &s >> s, mx: &a >> a
- ): Option_vt(a) =
- (
- case f0(st, mx) of
- | Skip_() => loop(f0, t0, st, mx)
- | Done_() =>
- if t0 > 0
- then Some_vt(mx) else None_vt()
- | Yield_() => loop(f0, 1, st, mx)
- )
- val+@Stream(st, f0) = str
- var mx = $UNSAFE.cast{a}(0)
- val opt = loop(f0, 0, st, mx)
- prval () = fold@(str)
- }
- implement main0 () = println!(x)
- where
- {
- val N0 = 100000000
- val dt = 1.0/4800000.0
- val x0 = 1.0
- val l0 = ~0.5
- val f0 =
- lam (st : &ExpState >> _, mx: &double >> _): Step_ =<cloptr1> expdecay(N0, dt, l0, mx, st)
- val str = Stream(@(0,x0),f0)
- val- ~Some_vt(x) = getlast<ExpState,double>(str)
- val () = stream_clear(str)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement