@@ -8,6 +8,37 @@ export here, rand!, randn!, randexp, randexp!,
88
99const Address = Union{Symbol,Pair{Symbol}}
1010
11+ Base. rand (:: Address , args... ) = rand (args... )
12+ Base. rand (rng:: AbstractRNG , :: Address , args... ) = rand (rng, args... )
13+
14+ Base. randn (:: Address , args... ) = randn (args... )
15+ Base. randn (rng:: AbstractRNG , :: Address , args... ) = randn (rng, args... )
16+
17+ for fn in (:rand! , :randn! , :randexp , :randexp! , :bitrand , :randstring ,
18+ :randsubseq , :randsubseq! , :shuffle , :shuffle! ,
19+ :randperm , :randperm! , :randcycle , :randcycle! )
20+ fn = GlobalRef (Random, fn)
21+ @eval $ fn (:: Address , args... ) = $ fn (args... )
22+ @eval $ fn (rng:: AbstractRNG , :: Address , args... ) = $ fn (rng, args... )
23+ end
24+
25+ for fn in (:rand , :rand! , :randn! , :randexp , :randexp! , :bitrand , :randstring ,
26+ :randsubseq , :randsubseq! , :shuffle , :shuffle! ,
27+ :randperm , :randperm! , :randcycle , :randcycle! )
28+ @eval begin
29+ @doc """
30+ $($ fn) ([rng=GLOBAL_RNG,] addr::Address, args...)
31+
32+ Traced execution of `$($ fn) `, where `addr` specifies the name / address of
33+ the random choice. By default, the address `addr` is ignored, but it can
34+ be intercepted to support inference in a probabilistic programming system.
35+ An `Address` is either a `Symbol`, or a `Pair` that begins with a `Symbol`.
36+ """
37+ $ fn
38+ end
39+ end
40+
41+ " Singleton type for the special [`here`](@ref) address."
1142struct Here end
1243
1344"""
@@ -16,28 +47,27 @@ struct Here end
1647Special address that refers to the address namespace of the calling context.
1748Supported when [`rand`](@ref) is called on a non-primitive stochastic function,
1849causing all named random variables sampled by that function to be spliced into
19- the address namespace of the calling context.
50+ the address namespace of the calling context.
2051"""
2152const here = Here ()
2253
54+ """
55+ rand([rng=GLOBAL_RNG,] addr::Union{Here,Address}, fn::Function, args...)
56+
57+ Traced execution of a stochastic function `fn`, where `addr` specifies the
58+ address namespace for any primitive random choices made within the call to `fn`.
59+ By default, the address `addr` is ignored, but it can be intercepted to support
60+ inference in a probabilistic programming system.
61+
62+ An `Address` is either a `Symbol`, or a `Pair` that begins with a `Symbol`.
63+ Alternatively, if the special address [`here`](@ref) is specified, then all
64+ traced random choices made by `fn` will be spliced into the namespace of the
65+ calling context.
66+ """
2367Base. rand (:: Here , fn:: Function , args... ) = fn (args... )
2468Base. rand (:: AbstractRNG , :: Here , fn:: Function , args... ) = fn (args... )
2569
2670Base. rand (:: Address , fn:: Function , args... ) = fn (args... )
2771Base. rand (:: AbstractRNG , :: Address , fn:: Function , args... ) = fn (args... )
2872
29- Base. rand (:: Address , args... ) = rand (args... )
30- Base. rand (rng:: AbstractRNG , :: Address , args... ) = rand (rng, args... )
31-
32- Base. randn (:: Address , args... ) = randn (args... )
33- Base. randn (rng:: AbstractRNG , :: Address , args... ) = randn (rng, args... )
34-
35- for fn in (:rand! , :randn! , :randexp , :randexp! , :bitrand , :randstring ,
36- :randsubseq , :randsubseq! , :shuffle , :shuffle! ,
37- :randperm , :randperm! , :randcycle , :randcycle! )
38- fn = GlobalRef (Random, fn)
39- @eval $ fn (:: Address , args... ) = $ fn (args... )
40- @eval $ fn (rng:: AbstractRNG , :: Address , args... ) = $ fn (rng, args... )
41- end
42-
4373end
0 commit comments