# \[Resolved\] Return in @model

**URL:** <https://discourse.julialang.org/t/resolved-return-in-model/47140>\
**Category:** Probabilistic Programming\
**Tags:** question, turing\
**Created:** [September 23, 2020, 3:58pm UTC](https://discourse.julialang.org/t/resolved-return-in-model/47140 "2020-09-23T15:58:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcql](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@mcql](https://discourse.julialang.org/u/mcql)\
**Post date:** [September 23, 2020, 3:58pm UTC](https://discourse.julialang.org/t/resolved-return-in-model/47140/1 "2020-09-23T15:58:19Z")

</div>

Hello,  
I would like to know what is the meaning of the return statement in a @model block in Turing.

In the documentation only some example use return in the model definition. The samples generation works as well without return a distribution.

When I use a model to generate samples what is the role of the return statement?

Thanks

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [September 23, 2020, 4:25pm UTC](https://discourse.julialang.org/t/resolved-return-in-model/47140/2 "2020-09-23T16:25:34Z")

</div>

_EDIT: I had thought this was a question about Soss (you didn’t specify a PPL in the text), but I just noticed the `turing` tag. So… maybe nevermind?_

Return statements are a `coming soon` feature. We want to be able to write, for example,

```julia
mycauchy() = @model begin 
    x ~ Normal()
    y ~ Normal()
    return x/y
end

```

So `rand(mycauchy())` would just return a `Float64`.

That part is simple enough. But how would we implement `logpdf(mycauchy(), c::Float64)`? In this case we can figure it out, but there are too many options for a way to do this in general, and they’re all much slower than we’d like.

What we need is a way to pass the _internal state_ as well. And a quick fix won’t really do it, we need the result to be nicely composable. For example, say we have a “slash distribution”,

```julia
slash = @model a,b begin 
    x ~ a
    y ~ b
    return x/y
end

```

The arguments to `slash` might be `Distribution`s, or could themselves for Soss models. We need things to propagate correctly in either case.

I think there will be a lot of advantages to this once we straighten it out, but it will take thinking through it some more to be sure we get it right. I think we’ll take some ideas from Gen on this, but there are some details to work through.

---

<div class="post-metadata">

**Author:** ![mcql](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@mcql](https://discourse.julialang.org/u/mcql)\
**Post date:** [September 23, 2020, 4:58pm UTC](https://discourse.julialang.org/t/resolved-return-in-model/47140/3 "2020-09-23T16:58:55Z")

</div>

Thanks for the answer but the question was about the Turing framework.

---

<div class="post-metadata">

**Author:** ![phg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phg/32/17184_2.png) [@phg](https://discourse.julialang.org/u/phg)\
**Post date:** [September 24, 2020, 7:19am UTC](https://discourse.julialang.org/t/resolved-return-in-model/47140/4 "2020-09-24T07:19:08Z")

</div>

When you define a model and instantiate it, you get a callable object based on the functional form that you wrote for your model:

```julia
@model function bla(x)
   y ~ Normal()
   x ~ Normal(y)
   return "hi"
end

m = bla([1,2,3]) # instantiate the model
m() # evaluate the "instance"

```

In that last `m()`, the content of the transformed function is run with some extra stuff, and `"hi"` will be returned.

But you almost never need that. The return value has almost no meaning in a Turing setting – what you are interested in is the trace. Usually, you instead call `sample(m, alg, N)` instead, which ignores the return value and internally does something akin to

```julia
vi = VarInfo()
m(vi)

```

where `vi` is written to, and after the call contains, most importantly, the sampled values of the variables, and the accumulated log-probability.

You can use `return` for early termination, and other hacks, but that’s changes of the operational behaviour, not really of the probabilistic meaning.

---

<div class="post-metadata">

**Author:** ![trappmartin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trappmartin/32/1165_2.png) [@trappmartin](https://discourse.julialang.org/u/trappmartin)\
**Post date:** [September 25, 2020, 1:59pm UTC](https://discourse.julialang.org/t/resolved-return-in-model/47140/5 "2020-09-25T13:59:10Z")

</div>

We are planing to utilise the return block in Turing.

In the future (once I started working on the PR), the return statement will allow you to specify which variables you want to store in your trace. If you don’t specify it, this will fall back to the current behaviour and if you specify a return statement you will be able to track transformed variables and the compiler will be able to marginalise out parameters if they are not of interest.
