Skip to content

Commit 52fe69f

Browse files
committed
trying the traits thing
1 parent 0b4a831 commit 52fe69f

File tree

9 files changed

+33
-21
lines changed

9 files changed

+33
-21
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ MLStyle = "d8e11817-5142-5d16-987a-aa16d5891078"
1313
MappedArrays = "dbb5928d-eab1-5f90-85c2-b9b0edb7c900"
1414
NamedTupleTools = "d9ec5142-1e00-5aa0-9d6a-321866360f50"
1515
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
16+
SimpleTraits = "699a6c99-e7fa-54fc-8d76-47d257e15c1d"
1617
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
1718
TransformVariables = "84d833dd-6860-57f9-a1a7-6da5db126cff"
1819

@@ -23,6 +24,7 @@ KeywordCalls = "0.1.3"
2324
MLStyle = "0.4"
2425
MappedArrays = "0.4"
2526
NamedTupleTools = "0.13"
27+
SimpleTraits = "0.9"
2628
julia = "1.3"
2729

2830
[extras]

src/MeasureBase.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ using Random
66
using ConcreteStructs
77
using MLStyle
88
using KeywordCalls
9+
using SimpleTraits
910
using Compat
1011

1112
export

src/absolutecontinuity.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ not, it should be a transform (`Pushforward` or `Pullback`) of a
6868
function representative(μ)
6969
function f(μ)
7070
# Check if we're done
71-
isprimitive(μ) && return μ
71+
isprimtype(μ) && return μ
7272
ν = basemeasure(μ)
7373
return ν
7474
end

src/primitives.jl

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
abstract type PrimitiveMeasure <: AbstractMeasure end
2-
3-
export isprimitive
4-
51
"""
6-
isprimitive(μ)
2+
isprimtype(μ)
73
84
Most measures are defined in terms of other measures, for example using a
95
density or a pushforward. Those that are not are considered (in this library,
106
it's not a general measure theory thing) to be _primitive_. The canonical
117
example of a primitive measure is `Lebesgue(X)` for some `X`.
128
139
The default method is
14-
isprimitive(μ) = false
10+
isprimtype(μ) = false
1511
1612
So when adding a new primitive measure, it's necessary to add a method for its type
1713
that returns `true`.
1814
"""
19-
isprimitive(μ) = false
20-
isprimitive(::PrimitiveMeasure) = true
15+
function isprimtype end
16+
17+
@traitdef IsPrimType{X}
18+
@traitdef IsPrimType{X} <- isprimtype(X)
19+
isprimtype(X) = false # default
2120

2221
export basemeasure
2322

@@ -31,9 +30,17 @@ For measures not defined in this way, we'll typically have `basemeasure(μ) ==
3130
"""
3231
function basemeasure end
3332

34-
basemeasure::PrimitiveMeasure) = μ
33+
basemeasure::M) where {IsPrimType{M}} = μ
3534

3635
include("primitives/trivial.jl")
3736
include("primitives/lebesgue.jl")
3837
include("primitives/counting.jl")
3938
include("primitives/dirac.jl")
39+
40+
@traitdef IsRepType{X}
41+
42+
@traitimpl IsRepType{X} <- isreptype(X)
43+
isreptype(X) = false # set default
44+
45+
# Every primitive measure is also a representative
46+
isreptype::M) where {IsPrimType{M}} = true

src/primitives/counting.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
export CountingMeasure
22

3-
struct CountingMeasure{X} <: PrimitiveMeasure end
3+
struct CountingMeasure{X} <: AbstractMeasure end
4+
5+
isprimtype(::CountingMeasure) = true
46

57
function Base.show(io::IO, μ::CountingMeasure{X}) where {X}
68
io = IOContext(io, :compact => true)

src/primitives/dirac.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11

22
export Dirac
33

4-
struct Dirac{X} <: PrimitiveMeasure
4+
struct Dirac{X} <: AbstractMeasure
55
x::X
66
end
77

8+
isprimtype(::Dirac) = true
9+
810
sampletype::Dirac{X}) where {X} = X
911

1012
function::Dirac{X})(s) where {X}
1113
μ.x s && return 1
1214
return 0
1315
end
14-
isprimitive(::Dirac) = true
16+
isprimtype(::Dirac) = true
1517

1618
logdensity::Dirac, x) = (x == μ.x) ? 0.0 : -Inf
1719

src/primitives/lebesgue.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
export Lebesgue
44

5-
struct Lebesgue{X} <: PrimitiveMeasure end
5+
struct Lebesgue{X} <: AbstractMeasure end
6+
7+
isprimtype(::Lebesgue) = true
68

79
function Base.show(io::IO, μ::Lebesgue{X}) where X
810
io = IOContext(io, :compact => true)

src/primitives/primitives.jl

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/primitives/trivial.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
export TrivialMeasure
22

3-
struct TrivialMeasure <: PrimitiveMeasure end
3+
struct TrivialMeasure <: AbstractMeasure end
4+
5+
isprimtype(::TrivialMeasure) = true
46

57
sampletype(::TrivialMeasure) = Nothing

0 commit comments

Comments
 (0)