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
Copy file name to clipboardExpand all lines: docs/src/plugins/constraint_specification.md
+36Lines changed: 36 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -78,6 +78,42 @@ We can specify constraints over the first `toy_model` submodel using the followi
78
78
end
79
79
```
80
80
81
+
## Inline constraints on submodel calls
82
+
83
+
Constraints can also be specified **inline** at the call site of a submodel using the `where` keyword. This is useful when you want to attach constraints to a specific invocation without modifying the outer model's `@constraints` block. For example:
84
+
85
+
```@example constraints
86
+
@model function outer_toy_model(a, b, c)
87
+
a ~ toy_model(y = b, z = c) where {
88
+
constraints = @constraints begin
89
+
q(x, y, z) = q(x, y)q(z)
90
+
q(x) :: Normal
91
+
end
92
+
}
93
+
end
94
+
```
95
+
96
+
The `where { constraints = ... }` syntax accepts any constraint set produced by the `@constraints` macro. Constraint sets defined with the `@constraints function` form can also be passed by reference:
97
+
98
+
```@example constraints
99
+
@constraints function my_constraints()
100
+
q(x, y, z) = q(x, y)q(z)
101
+
q(x) :: Normal
102
+
end
103
+
104
+
@model function outer_toy_model(a, b, c)
105
+
a ~ toy_model(y = b, z = c) where { constraints = my_constraints() }
106
+
end
107
+
```
108
+
109
+
Inline constraints apply only to the specific submodel invocation they are attached to and propagate to any submodels nested within it. Their priority relative to other constraint sources, from highest to lowest, is:
110
+
111
+
1.**External constraints** — passed at model creation via `for q in submodel` or `for q in (submodel, index)` blocks.
112
+
2.**Inline constraints** — specified with `where { constraints = ... }` at the call site.
113
+
3.**Default constraints** — defined via `GraphPPL.default_constraints`.
114
+
115
+
This means that if external constraints also target the same submodel, they will override the inline constraints.
116
+
81
117
## Constraints over vector variables
82
118
83
119
When a model contains vector (or array) latent variables, we can specify factorization constraints over individual elements using the `begin` and `end` indexing syntax. For example, consider a random walk model where latent states `x` are coupled through sequential dependencies:
0 commit comments