autoppl  v0.8
A C++ template library for probabilistic programming
var_traits.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <cstdint>
6 
7 namespace ppl {
8 namespace util {
9 
10 template <class T>
11 struct VarBase : BaseCRTP<T>
12 { using BaseCRTP<T>::self; };
13 
14 template <class T>
15 struct ParamBase : VarBase<T>
16 {};
17 
18 template <class T>
19 struct TParamBase : VarBase<T>
20 {};
21 
22 template <class T>
23 struct DataBase : VarBase<T>
24 {};
25 
26 template <class T>
27 inline constexpr bool param_is_base_of_v =
28  std::is_base_of_v<ParamBase<T>, T>;
29 
30 template <class T>
31 inline constexpr bool tparam_is_base_of_v =
32  std::is_base_of_v<TParamBase<T>, T>;
33 
34 template <class T>
35 inline constexpr bool data_is_base_of_v =
36  std::is_base_of_v<DataBase<T>, T>;
37 
38 template <class VarType>
39 struct var_traits : var_expr_traits<VarType>
40 {
41 private:
43 public:
44  using id_t = typename VarType::id_t;
45  static constexpr bool is_cont_v = util::is_cont_v<typename base_t::value_t>;
46  static constexpr bool is_disc_v = util::is_disc_v<typename base_t::value_t>;
47 
48  static_assert(is_cont_v == !is_disc_v,
50 };
51 
52 template <class VarType>
53 struct param_traits : var_traits<VarType>
54 {
55  using constraint_t = typename VarType::constraint_t;
56 };
57 
58 template <class VarType>
59 struct tparam_traits : var_traits<VarType>
60 {};
61 
62 template <class VarType>
63 struct data_traits : var_traits<VarType>
64 {};
65 
66 #if __cplusplus <= 201703L
67 
68 template <class T>
69 inline constexpr bool is_param_v =
70  param_is_base_of_v<T> &&
71  has_type_id_t_v<T> &&
72  has_func_id_v<const T>
73  ;
74 
75 template <class T>
76 inline constexpr bool is_tparam_v =
77  tparam_is_base_of_v<T> &&
78  has_type_id_t_v<T> &&
79  has_func_id_v<const T>
80  ;
81 
82 template <class T>
83 inline constexpr bool is_data_v =
84  data_is_base_of_v<T> &&
85  has_type_id_t_v<T> &&
86  has_func_id_v<const T>
87  ;
88 
89 // A variable is one that can be assigned a distribution,
90 // i.e. a random variable.
91 template <class T>
92 inline constexpr bool is_var_v =
93  is_param_v<T> ||
94  is_tparam_v<T> ||
95  is_data_v<T>
96  ;
97 
98 template <class T>
99 inline constexpr bool is_dist_assignable_v =
100  is_param_v<T> ||
101  is_data_v<T>
102  ;
103 
104 #else
105 
106 template <class T>
107 concept data_c =
108  var_expr_c<T> &&
109  data_is_base_of_v<T> &&
110  requires (const T cx, size_t i) {
111  typename var_traits<T>::id_t;
112  { cx.id() } -> std::same_as<typename var_traits<T>::id_t>;
113  }
114  ;
115 
116 template <class T>
117 concept param_c =
118  var_expr_c<T> &&
119  param_is_base_of_v<T> &&
120  requires () {
121  typename var_traits<T>::id_t;
122  } &&
123  requires (T x, const T cx, size_t i) {
124  { cx.id() } -> std::same_as<typename var_traits<T>::id_t>;
125  }
126  ;
127 
128 template <class T>
129 concept tparam_c =
130  var_expr_c<T> &&
131  tparam_is_base_of_v<T> &&
132  requires () {
133  typename var_traits<T>::id_t;
134  } &&
135  requires (T x, const T cx, size_t i) {
136  { cx.id() } -> std::same_as<typename var_traits<T>::id_t>;
137  }
138  ;
139 
140 template <class T>
141 concept var_c =
142  param_c<T> ||
143  tparam_c<T> ||
144  data_c<T>
145  ;
146 
147 template <class T>
148 concept dist_assignable_c =
149  param_c<T> ||
150  data_c<T>
151  ;
152 
153 template <class T>
154 concept is_data_v = data_c<T>;
155 template <class T>
156 concept is_param_v = param_c<T>;
157 template <class T>
158 concept is_tparam_v = tparam_c<T>;
159 template <class T>
160 concept is_var_v = var_c<T>;
161 template <class T>
162 concept is_dist_assignable_v = dist_assignable_c<T>;
163 
164 #endif
165 
166 } // namespace util
167 } // namespace ppl
ppl::util::ParamBase
Definition: var_traits.hpp:16
ppl::util::param_traits
Definition: var_traits.hpp:54
ppl::util::VarBase
Definition: var_traits.hpp:12
ppl::util::param_traits::constraint_t
typename VarType::constraint_t constraint_t
Definition: var_traits.hpp:55
ppl::util::DataBase
Definition: var_traits.hpp:24
ppl::util::var_expr_traits
Definition: var_expr_traits.hpp:28
PPL_CONT_XOR_DISC
#define PPL_CONT_XOR_DISC
Definition: type_traits.hpp:42
type_traits.hpp
var_expr_traits.hpp
ppl::util::tparam_is_base_of_v
constexpr bool tparam_is_base_of_v
Definition: var_traits.hpp:31
ppl::util::is_var_v
constexpr bool is_var_v
Definition: var_traits.hpp:92
ppl::util::param_is_base_of_v
constexpr bool param_is_base_of_v
Definition: var_traits.hpp:27
ppl::util::var_traits::id_t
typename VarType::id_t id_t
Definition: var_traits.hpp:44
ppl::util::var_traits
Definition: var_traits.hpp:40
ppl::util::tparam_traits
Definition: var_traits.hpp:60
ppl::util::BaseCRTP
Definition: type_traits.hpp:65
ppl::util::is_tparam_v
constexpr bool is_tparam_v
Definition: var_traits.hpp:76
ppl::util::is_param_v
constexpr bool is_param_v
Definition: var_traits.hpp:69
ppl::util::var_traits::is_disc_v
static constexpr bool is_disc_v
Definition: var_traits.hpp:46
ppl::util::is_dist_assignable_v
constexpr bool is_dist_assignable_v
Definition: var_traits.hpp:99
ppl::util::var_traits::is_cont_v
static constexpr bool is_cont_v
Definition: var_traits.hpp:45
ppl::util::data_traits
Definition: var_traits.hpp:64
ppl::util::data_is_base_of_v
constexpr bool data_is_base_of_v
Definition: var_traits.hpp:35
ppl
Definition: bounded.hpp:11
ppl::util::TParamBase
Definition: var_traits.hpp:20
concept.hpp
ppl::util::is_data_v
constexpr bool is_data_v
Definition: var_traits.hpp:83