You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For a minimal example, let me define a fake RNG, together with a function to get random float values from it:
typeRNG=objectprocrandom(rng: varRNG): float=1.0
Now, I define a RandomVar[A] as something that is able, given a RNG, to sample random instances of A. As a few concrete instances, I consider a constant, a uniform distribution, and a closure. I also define constructors for these types, as well as sample functions to make sure that they are instances of RandomVar[A]:
typeRandomVar[A] =concept x
var rng: RNG
x.sample(rng) is A
Constant[A] =object
value: A
Uniform=object
a, b: floatClosureVar[A] =proc(rng: varRNG): A
procsample[A](rng: varRNG, c: Constant[A]): A = c.value
procsample(rng: varRNG, u: Uniform): float= u.a + (u.b - u.a) * rng.random()
procsample[A](rng: varRNG, c: ClosureVar[A]): A =c(rng)
procconstant[A](a: A): Constant[A] =Constant[A](value: a)
procuniform(a, b: float): Uniform=Uniform(a: a, b: b)
What I am trying to do is define operators on random variables. Here is a function that will lift a function A => B to a function RandomVar[A] => RandomVar[B]:
When I try to use lift1 I get a compile time error:
whenisMainModule:
procsq(x: float): float= x * x
let
c =constant(3)
u =uniform(2, 18)
t =lift1[float, float](sq, u)
var rng: RNGecho(c isRandomVar[int])
echo(u isRandomVar[float])
# echo(t is RandomVar[float])echo rng.sample(c)
echo rng.sample(u)
# echo rng.sample(t)
The line t = lift1[float, float](sq, u) fails with
Error: type mismatch: got (RNG, RandomVar[system.float])
but expected one of:
proc sample[A](rng: var RNG; c: Constant[A]): A
proc sample[A](rng: var RNG; c: ClosureVar[A]): A
proc sample(rng: var RNG; u: Uniform): float
To reproduce the error more easily, here is the whole thing:
typeRNG=objectprocrandom(rng: varRNG): float=1.0typeRandomVar[A] =concept x
var rng: RNG
x.sample(rng) is A
Constant[A] =object
value: A
Uniform=object
a, b: floatClosureVar[A] =proc(rng: varRNG): A
procsample[A](rng: varRNG, c: Constant[A]): A = c.value
procsample(rng: varRNG, u: Uniform): float= u.a + (u.b - u.a) * rng.random()
procsample[A](rng: varRNG, c: ClosureVar[A]): A =c(rng)
procconstant[A](a: A): Constant[A] =Constant[A](value: a)
procuniform(a, b: float): Uniform=Uniform(a: a, b: b)
proclift1[A, B](f: proc(a: A): B, r: RandomVar[A]): ClosureVar[B] =procinner(rng: varRNG): B =f(rng.sample(r))
return inner
whenisMainModule:
procsq(x: float): float= x * x
let
c =constant(3)
u =uniform(2, 18)
t =lift1[float, float](sq, u)
var rng: RNGecho(c isRandomVar[int])
echo(u isRandomVar[float])
# echo(t is RandomVar[float])echo rng.sample(c)
echo rng.sample(u)
# echo rng.sample(t)
The text was updated successfully, but these errors were encountered:
For a minimal example, let me define a fake RNG, together with a function to get random float values from it:
Now, I define a
RandomVar[A]
as something that is able, given a RNG, to sample random instances ofA
. As a few concrete instances, I consider a constant, a uniform distribution, and a closure. I also define constructors for these types, as well assample
functions to make sure that they are instances ofRandomVar[A]
:What I am trying to do is define operators on random variables. Here is a function that will lift a function
A => B
to a functionRandomVar[A] => RandomVar[B]
:When I try to use
lift1
I get a compile time error:The line
t = lift1[float, float](sq, u)
fails withTo reproduce the error more easily, here is the whole thing:
The text was updated successfully, but these errors were encountered: