Copyright | Copyright (c) 2016 the Hakaru team |
---|---|
License | BSD3 |
Maintainer | wren@community.haskell.org |
Stability | experimental |
Portability | GHC-only |
Safe Haskell | None |
Language | Haskell2010 |
- getStatements :: Dis abt [Statement abt Impure]
- putStatements :: [Statement abt Impure] -> Dis abt ()
- data ListContext abt p = ListContext {
- nextFreshNat :: !Nat
- statements :: [Statement abt p]
- type Ans abt a = ListContext abt Impure -> Assocs (Loc (abt '[])) -> [abt '[] (HMeasure a)]
- newtype Dis abt x = Dis {}
- runDis :: (ABT Term abt, Foldable f) => Dis abt (abt '[] a) -> f (Some2 abt) -> [abt '[] (HMeasure a)]
- bot :: ABT Term abt => Dis abt a
- emit :: ABT Term abt => Text -> Sing a -> (forall r. abt '[a] (HMeasure r) -> abt '[] (HMeasure r)) -> Dis abt (Variable a)
- emitMBind :: ABT Term abt => abt '[] (HMeasure a) -> Dis abt (Variable a)
- emitLet :: ABT Term abt => abt '[] a -> Dis abt (Variable a)
- emitLet' :: ABT Term abt => abt '[] a -> Dis abt (abt '[] a)
- emitUnpair :: ABT Term abt => Whnf abt (HPair a b) -> Dis abt (abt '[] a, abt '[] b)
- emit_ :: ABT Term abt => (forall r. abt '[] (HMeasure r) -> abt '[] (HMeasure r)) -> Dis abt ()
- emitMBind_ :: ABT Term abt => abt '[] (HMeasure HUnit) -> Dis abt ()
- emitGuard :: ABT Term abt => abt '[] HBool -> Dis abt ()
- emitWeight :: ABT Term abt => abt '[] HProb -> Dis abt ()
- emitFork_ :: (ABT Term abt, Traversable t) => (forall r. t (abt '[] (HMeasure r)) -> abt '[] (HMeasure r)) -> t (Dis abt a) -> Dis abt a
- emitSuperpose :: ABT Term abt => [abt '[] (HMeasure a)] -> Dis abt (Variable a)
- choose :: ABT Term abt => [Dis abt a] -> Dis abt a
- pushPlate :: ABT Term abt => abt '[] HNat -> abt '[HNat] (HMeasure a) -> Dis abt (Variable (HArray a))
- getIndices :: EvaluationMonad abt m p => m [Index (abt '[])]
- withIndices :: [Index (abt '[])] -> Dis abt a -> Dis abt a
- extendIndices :: ABT Term abt => Index (abt '[]) -> [Index (abt '[])] -> [Index (abt '[])]
- extendLocInds :: Variable HNat -> [Variable HNat] -> [Variable HNat]
- statementInds :: Statement abt p -> [Index (abt '[])]
- sizeInnermostInd :: ABT Term abt => Variable (a :: Hakaru) -> Dis abt (abt '[] HNat)
- data Loc :: (Hakaru -> *) -> Hakaru -> * where
- getLocs :: ABT Term abt => Dis abt (Assocs (Loc (abt '[])))
- putLocs :: ABT Term abt => Assocs (Loc (abt '[])) -> Dis abt ()
- insertLoc :: ABT Term abt => Variable a -> Loc (abt '[]) a -> Dis abt ()
- adjustLoc :: ABT Term abt => Variable (a :: Hakaru) -> (Assoc (Loc (abt '[])) -> Assoc (Loc (abt '[]))) -> Dis abt ()
- mkLoc :: ABT Term abt => Text -> Variable (a :: Hakaru) -> [Variable HNat] -> Dis abt (Variable a)
- freeLocError :: Variable (a :: Hakaru) -> b
- apply :: ABT Term abt => [(Index (abt '[]), Index (abt '[]))] -> abt '[] a -> Dis abt (abt '[] a)
The disintegration monad
List-based version
data ListContext abt p Source #
An ordered collection of statements representing the context surrounding the current focus of our program transformation. That is, since some transformations work from the bottom up, we need to keep track of the statements we passed along the way when reaching for the bottom.
The tail of the list takes scope over the head of the list. Thus, the back/end of the list is towards the top of the program, whereas the front of the list is towards the bottom.
This type was formerly called Heap
(presumably due to the
Statement
type being called Binding
) but that seems like a
misnomer to me since this really has nothing to do with allocation.
However, it is still like a heap inasmuch as it's a dependency
graph and we may wish to change the topological sorting or remove
"garbage" (subject to correctness criteria).
TODO: Figure out what to do with SWeight
, SGuard
, SStuff
,
etc, so that we can use an IntMap (Statement abt)
in order to
speed up the lookup times in select
. (Assuming callers don't
use unsafePush
unsafely: we can recover the order things were
inserted from their varID
since we've freshened them all and
therefore their IDs are monotonic in the insertion order.)
ListContext | |
|
type Ans abt a = ListContext abt Impure -> Assocs (Loc (abt '[])) -> [abt '[] (HMeasure a)] Source #
runDis :: (ABT Term abt, Foldable f) => Dis abt (abt '[] a) -> f (Some2 abt) -> [abt '[] (HMeasure a)] Source #
Run a computation in the Dis
monad, residualizing out all the
statements in the final evaluation context. The second argument
should include all the terms altered by the Dis
expression; this
is necessary to ensure proper hygiene; for example(s):
runDis (perform e) [Some2 e] runDis (constrainOutcome e v) [Some2 e, Some2 v]
We use Some2
on the inputs because it doesn't matter what their
type or locally-bound variables are, so we want to allow f
to
contain terms with different indices.
TODO: IntMap-based version
Operators on the disintegration monad
The "zero" and "one"
bot :: ABT Term abt => Dis abt a Source #
It is impossible to satisfy the constraints, or at least we
give up on trying to do so. This function is identical to empty
and mzero
for Dis
; we just give it its own name since this is
the name used in our papers.
TODO: add some sort of trace information so we can get a better idea what caused a disintegration to fail.
Emitting code
emit :: ABT Term abt => Text -> Sing a -> (forall r. abt '[a] (HMeasure r) -> abt '[] (HMeasure r)) -> Dis abt (Variable a) Source #
The empty measure is a solution to the constraints. reject :: (ABT Term abt) => Dis abt a reject = Dis $ _ _ -> [syn (Superpose_ [])]
Emit some code that binds a variable, and return the variable thus bound. The function says what to wrap the result of the continuation with; i.e., what we're actually emitting.
emitMBind :: ABT Term abt => abt '[] (HMeasure a) -> Dis abt (Variable a) Source #
Emit an MBind
(i.e., "m >>= x ->
") and return the
variable thus bound (i.e., x
).
emitLet :: ABT Term abt => abt '[] a -> Dis abt (Variable a) Source #
A smart constructor for emitting let-bindings. If the input
is already a variable then we just return it; otherwise we emit
the let-binding. N.B., this function provides the invariant that
the result is in fact a variable; whereas emitLet'
does not.
emitLet' :: ABT Term abt => abt '[] a -> Dis abt (abt '[] a) Source #
A smart constructor for emitting let-bindings. If the input
is already a variable or a literal constant, then we just return
it; otherwise we emit the let-binding. N.B., this function
provides weaker guarantees on the type of the result; if you
require the result to always be a variable, then see emitLet
instead.
emitUnpair :: ABT Term abt => Whnf abt (HPair a b) -> Dis abt (abt '[] a, abt '[] b) Source #
A smart constructor for emitting "unpair". If the input argument is actually a constructor then we project out the two components; otherwise we emit the case-binding and return the two variables.
emit_ :: ABT Term abt => (forall r. abt '[] (HMeasure r) -> abt '[] (HMeasure r)) -> Dis abt () Source #
Emit some code that doesn't bind any variables. This function
provides an optimisation over using emit
and then discarding
the generated variable.
emitGuard :: ABT Term abt => abt '[] HBool -> Dis abt () Source #
Emit an assertion that the condition is true.
emitFork_ :: (ABT Term abt, Traversable t) => (forall r. t (abt '[] (HMeasure r)) -> abt '[] (HMeasure r)) -> t (Dis abt a) -> Dis abt a Source #
Run each of the elements of the traversable using the same heap and continuation for each one, then pass the results to a function for emitting code.
emitSuperpose :: ABT Term abt => [abt '[] (HMeasure a)] -> Dis abt (Variable a) Source #
Emit a Superpose_
of the alternatives, each with unit weight.
choose :: ABT Term abt => [Dis abt a] -> Dis abt a Source #
Emit a Superpose_
of the alternatives, each with unit weight.
Overrides for original in Evaluation.Types
pushPlate :: ABT Term abt => abt '[] HNat -> abt '[HNat] (HMeasure a) -> Dis abt (Variable (HArray a)) Source #
For Arrays/Plate
getIndices :: EvaluationMonad abt m p => m [Index (abt '[])] Source #
Returns the current Indices. Currently, this is only applicable to the Disintegration Monad, but could be relevant as other partial evaluators begin to handle Plate and Array
statementInds :: Statement abt p -> [Index (abt '[])] Source #
Locs
adjustLoc :: ABT Term abt => Variable (a :: Hakaru) -> (Assoc (Loc (abt '[])) -> Assoc (Loc (abt '[]))) -> Dis abt () Source #
mkLoc :: ABT Term abt => Text -> Variable (a :: Hakaru) -> [Variable HNat] -> Dis abt (Variable a) Source #
freeLocError :: Variable (a :: Hakaru) -> b Source #