Skip to content

Commit

Permalink
Merge pull request #45 from xukai92/fixcompiler
Browse files Browse the repository at this point in the history
Make compiler support Distributions wihtout parameters
  • Loading branch information
yebai committed Nov 14, 2016
2 parents 40846cb + 15bcfe5 commit e690bd1
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions src/core/compiler.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
# Helper function
doc"""
has_ops(ex)
Check if has optional arguments.
```julia
has_ops(parse("@assume x ~ Normal(0, 1; :static=true)")) # gives true
has_ops(parse("@assume x ~ Normal(0, 1)")) # gives false
has_ops(parse("@assume x ~ Binomial(; :static=true)")) # gives true
has_ops(parse("@assume x ~ Binomial()")) # gives false
```
"""
function has_ops(ex)
if length(ex.args[3].args) <= 1 # check if the D() has parameters
return false # Binominal() can have empty
elseif typeof(ex.args[3].args[2]) != Expr # check if has optional arguments
return false
elseif ex.args[3].args[2].head != :parameters # check if parameters valid
return false
end
true
end


# Operation for defining the prior
# Usage:
# @assume x ~ Dist
Expand All @@ -7,13 +32,13 @@ macro assume(ex)
dprintln(1, "marco assuming...")
@assert ex.args[1] == symbol("@~")
# Check if have extra arguements setting
if typeof(ex.args[3].args[2]) == Expr && ex.args[3].args[2].head == :parameters
if has_ops(ex)
# If static is set
if ex.args[3].args[2].args[1].args[1] == :static && ex.args[3].args[2].args[1].args[2] == :true
if ex.args[3].args[2].args[1].args[1] == :(:static) && ex.args[3].args[2].args[1].args[2] == :true
# Do something
end
# If param is set
if ex.args[3].args[2].args[1].args[1] == :param && ex.args[3].args[2].args[1].args[2] == :true
if ex.args[3].args[2].args[1].args[1] == :(:param) && ex.args[3].args[2].args[1].args[2] == :true
# Do something
end
# Remove the extra argument
Expand Down Expand Up @@ -42,13 +67,13 @@ macro observe(ex)

global TURING
# Check if have extra arguements setting
if typeof(ex.args[3].args[2]) == Expr && ex.args[3].args[2].head == :parameters
if has_ops(ex)
# If static is set
if ex.args[3].args[2].args[1].args[1] == :static && ex.args[3].args[2].args[1].args[2] == :true
if ex.args[3].args[2].args[1].args[1] == :(:static) && ex.args[3].args[2].args[1].args[2] == :true
# Do something
end
# If param is set
if ex.args[3].args[2].args[1].args[1] == :param && ex.args[3].args[2].args[1].args[2] == :true
if ex.args[3].args[2].args[1].args[1] == :(:param) && ex.args[3].args[2].args[1].args[2] == :true
# Do something
end
# Remove the extra argument
Expand Down

0 comments on commit e690bd1

Please sign in to comment.