hakaru-0.3.0: A probabilistic programming language

CopyrightCopyright (c) 2016 the Hakaru team
LicenseBSD3
Maintainerwren@community.haskell.org
Stabilityexperimental
PortabilityGHC-only
Safe HaskellNone
LanguageHaskell2010

Language.Hakaru.Evaluation.DisintegrationMonad

Contents

Description

Synopsis

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.)

Constructors

ListContext 

Fields

type Ans abt a = ListContext abt Impure -> Assocs (Loc (abt '[])) -> [abt '[] (HMeasure a)] Source #

newtype Dis abt x Source #

Constructors

Dis 

Fields

Instances

ABT Hakaru Term abt => EvaluationMonad abt (Dis abt) Impure Source # 
Monad (Dis abt) Source # 

Methods

(>>=) :: Dis abt a -> (a -> Dis abt b) -> Dis abt b #

(>>) :: Dis abt a -> Dis abt b -> Dis abt b #

return :: a -> Dis abt a #

fail :: String -> Dis abt a #

Functor (Dis abt) Source # 

Methods

fmap :: (a -> b) -> Dis abt a -> Dis abt b #

(<$) :: a -> Dis abt b -> Dis abt a #

Applicative (Dis abt) Source # 

Methods

pure :: a -> Dis abt a #

(<*>) :: Dis abt (a -> b) -> Dis abt a -> Dis abt b #

(*>) :: Dis abt a -> Dis abt b -> Dis abt b #

(<*) :: Dis abt a -> Dis abt b -> Dis abt a #

Alternative (Dis abt) Source # 

Methods

empty :: Dis abt a #

(<|>) :: Dis abt a -> Dis abt a -> Dis abt a #

some :: Dis abt a -> Dis abt [a] #

many :: Dis abt a -> Dis abt [a] #

MonadPlus (Dis abt) Source # 

Methods

mzero :: Dis abt a #

mplus :: Dis abt a -> Dis abt a -> Dis abt a #

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.

emitMBind_ :: ABT Term abt => abt '[] (HMeasure HUnit) -> Dis abt () Source #

Emit an MBind that discards its result (i.e., "m >>"). We restrict the type of the argument to be HUnit so as to avoid accidentally dropping things.

emitGuard :: ABT Term abt => abt '[] HBool -> Dis abt () Source #

Emit an assertion that the condition is true.

emitWeight :: ABT Term abt => abt '[] HProb -> Dis abt () Source #

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

withIndices :: [Index (abt '[])] -> Dis abt a -> Dis abt a Source #

extendIndices :: ABT Term abt => Index (abt '[]) -> [Index (abt '[])] -> [Index (abt '[])] Source #

statementInds :: Statement abt p -> [Index (abt '[])] Source #

sizeInnermostInd :: ABT Term abt => Variable (a :: Hakaru) -> Dis abt (abt '[] HNat) Source #

Locs

data Loc :: (Hakaru -> *) -> Hakaru -> * where Source #

Constructors

Loc :: Variable a -> [Variable HNat] -> Loc ast a 
MultiLoc :: Variable a -> [Variable HNat] -> Loc ast (HArray a) 

getLocs :: ABT Term abt => Dis abt (Assocs (Loc (abt '[]))) Source #

putLocs :: ABT Term abt => Assocs (Loc (abt '[])) -> Dis abt () Source #

insertLoc :: ABT Term abt => Variable a -> Loc (abt '[]) a -> Dis abt () Source #

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 #

apply :: ABT Term abt => [(Index (abt '[]), Index (abt '[]))] -> abt '[] a -> Dis abt (abt '[] a) Source #