probably.pgcl.substitute
Internals¶
- class probably.pgcl.substitute._Binder(*, deepcopy)[source]¶
All substitutions (the values in the subst dict) are assigned a numeric identifier. The binder manages those.
- Parameters:
deepcopy (
bool
) –
- bind(expr)[source]¶
Add the given expression to the binder, return a new
_BoundExpr
that points to the expression in the binder.- Parameters:
expr (
Union
[VarExpr
,BoolLitExpr
,NatLitExpr
,RealLitExpr
,UnopExpr
,BinopExpr
,CategoricalExpr
,SubstExpr
,TickExpr
,DUniformExpr
,CUniformExpr
,BernoulliExpr
,GeometricExpr
,PoissonExpr
,LogDistExpr
,BinomialExpr
,IidSampleExpr
]) –- Return type:
_BoundExpr
- resolve()[source]¶
In the internal list of bound expressions, replace all
_BoundExpr
by the the bound expressions.Cycles are handled using a two-step procedure (see module docs).
- lookup(bound)[source]¶
Given a
_BoundExpr
, return the associated expression._Deepcopies_ the expression if
deepcopy
is set toTrue
.- Parameters:
bound (
_BoundExpr
) –- Return type:
Union
[VarExpr
,BoolLitExpr
,NatLitExpr
,RealLitExpr
,UnopExpr
,BinopExpr
,CategoricalExpr
,SubstExpr
,TickExpr
,DUniformExpr
,CUniformExpr
,BernoulliExpr
,GeometricExpr
,PoissonExpr
,LogDistExpr
,BinomialExpr
,IidSampleExpr
]
- class probably.pgcl.substitute._Subst(*, subst=None, symbolic)[source]¶
Data structure for current substitutions to be applied.
It holds a mapping for the substitutions itself (Var/BoundExpr to BoundExpr),
and the set of symbolic variables that cannot be substituted (see section Symbolic variables for more information).
- Parameters:
- add_bound(binder, var, expr)[source]¶
Bind the expression using
binder
, then add a new substitution that replacesvar
by the newly created bound expression.- Parameters:
binder (
_Binder
) –expr (
Union
[VarExpr
,BoolLitExpr
,NatLitExpr
,RealLitExpr
,UnopExpr
,BinopExpr
,CategoricalExpr
,SubstExpr
,TickExpr
,DUniformExpr
,CUniformExpr
,BernoulliExpr
,GeometricExpr
,PoissonExpr
,LogDistExpr
,BinomialExpr
,IidSampleExpr
]) –
- apply(var_expr)[source]¶
Apply the substitutions to the given variable expression. Symbolic variables are replaced by new substitution expressions.
- Parameters:
var_expr (
VarExpr
) –- Return type:
Union
[VarExpr
,BoolLitExpr
,NatLitExpr
,RealLitExpr
,UnopExpr
,BinopExpr
,CategoricalExpr
,SubstExpr
,TickExpr
,DUniformExpr
,CUniformExpr
,BernoulliExpr
,GeometricExpr
,PoissonExpr
,LogDistExpr
,BinomialExpr
,IidSampleExpr
]
- probably.pgcl.substitute._bind_substs(binder, subst, expr_ref)[source]¶
Walk the expr_ref and replace all right-hand sides of each
SubstExpr
by a locally bound_BoundExpr
.This test asserts the order in which substitutions are applied: First inner ones, then outer.
>>> binder = _Binder(deepcopy=False) >>> subst = _Subst(symbolic=set()) >>> expr = SubstExpr({'x': VarExpr('x1')}, SubstExpr({'x': VarExpr('x2')}, VarExpr('x'))) >>> expr_ref = Mut.alloc(expr) >>> _bind_substs(binder, subst, expr_ref) >>> str(expr_ref.val) 'BoundExpr(1)' >>> binder.lookup(expr_ref.val) VarExpr('x2')
- Parameters:
binder (
_Binder
) –subst (
_Subst
) –expr_ref (
Mut
[Union
[VarExpr
,BoolLitExpr
,NatLitExpr
,RealLitExpr
,UnopExpr
,BinopExpr
,CategoricalExpr
,SubstExpr
,TickExpr
,DUniformExpr
,CUniformExpr
,BernoulliExpr
,GeometricExpr
,PoissonExpr
,LogDistExpr
,BinomialExpr
,IidSampleExpr
]]) –