Skip to content

Commit

Permalink
Merge pull request #17 from Herb-AI/refactor-any-type-from-struct
Browse files Browse the repository at this point in the history
Remove any type from struct in IOExample and Trace
  • Loading branch information
ReubenJ authored Nov 14, 2024
2 parents eef2222 + 2da5364 commit 9d06378
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions src/problem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ An input-output example.
`in` is a [`Dict`](@ref) of `{Symbol,Any}` where the symbol represents a variable in a program.
`out` can be anything.
"""
struct IOExample
in::Dict{Symbol, Any}
out::Any
struct IOExample{InType, OutType}
in::Dict{Symbol, InType}
out::OutType
end

"""
Expand All @@ -16,8 +16,8 @@ end
A trace defining a wanted program execution for program synthesis.
@TODO combine with Gen.jl
"""
struct Trace
exec_path::Vector{Any}
struct Trace{T}
exec_path::Vector{T}
end

abstract type AbstractFormalSpecification end
Expand All @@ -27,8 +27,8 @@ abstract type AbstractFormalSpecification end
A specification based on a logical formula defined by a SMT solver.
"""
struct SMTSpecification <: AbstractFormalSpecification
formula::Function
struct SMTSpecification{F} <: AbstractFormalSpecification
formula::F
end


Expand All @@ -46,11 +46,16 @@ abstract type AbstractDependentTypeSpecification <: AbstractTypeSpecification en
Defines a specification
"""
struct AgdaSpecification <: AbstractDependentTypeSpecification
formula::Function
struct AgdaSpecification{F} <: AbstractDependentTypeSpecification
formula::F
end

const AbstractSpecification = Union{Vector{IOExample}, AbstractFormalSpecification, Vector{Trace}, AbstractTypeSpecification}
const AbstractSpecification = Union{
AbstractVector{<:IOExample},
AbstractFormalSpecification,
AbstractVector{<:Trace},
AbstractTypeSpecification
}

"""
struct Problem
Expand All @@ -77,17 +82,17 @@ end
Program synthesis problem defined by an specification and a metric. The specification has to be based on input/output examples, while the function needs to return a numerical value.
"""
struct MetricProblem{T <: Vector{IOExample}}
struct MetricProblem{T <: AbstractVector{<:IOExample}, F}
name::AbstractString
cost_function::Function
cost_function::F
spec::T

function MetricProblem(cost_function::Function, spec::T) where T<:Vector{IOExample}
new{T}("", cost_function, spec)
function MetricProblem(cost_function::F, spec::T) where {T<:AbstractVector{<:IOExample}, F}
new{T, F}("", cost_function, spec)
end

function MetricProblem(name::AbstractString, cost_function::Function, spec::T) where T<:Vector{IOExample}
new{T}(name, cost_function, spec)
function MetricProblem(name::AbstractString, cost_function::F, spec::T) where {T<:AbstractVector{<:IOExample}, F}
new{T, F}(name, cost_function, spec)
end

end
Expand All @@ -98,7 +103,7 @@ end
Overwrite `Base.getindex` to allow for slicing of input/output-based problems.
"""
Base.getindex(p::Problem{Vector{IOExample}}, indices) = Problem(p.name, p.spec[indices])
Base.getindex(p::MetricProblem{Vector{IOExample}}, indices) = MetricProblem(p.name, p.cost_function, p.spec[indices])
Base.getindex(p::Problem{<:AbstractVector{<:IOExample}}, indices) = Problem(p.name, p.spec[indices])
Base.getindex(p::MetricProblem{<:AbstractVector{<:IOExample}}, indices) = MetricProblem(p.name, p.cost_function, p.spec[indices])


0 comments on commit 9d06378

Please sign in to comment.