From c8d3d1f24754dcd9bbbfd1f5f03393528fe4054e Mon Sep 17 00:00:00 2001 From: Reuben Gardos Reid <5456207+ReubenJ@users.noreply.github.com> Date: Thu, 14 Nov 2024 17:11:12 +0100 Subject: [PATCH] Add hash and equality definitions for structs --- Project.toml | 2 ++ src/HerbSpecification.jl | 2 ++ src/problem.jl | 14 ++++++-------- test/test_ioproblem.jl | 6 ++++++ 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/Project.toml b/Project.toml index d7828ad..67bda53 100644 --- a/Project.toml +++ b/Project.toml @@ -4,6 +4,8 @@ authors = ["Tilman Hinnerichs "] version = "0.1.1" [deps] +AutoHashEquals = "15f4f7f2-30c1-5605-9d31-71845cf9641f" [compat] +AutoHashEquals = "^2.2" julia = "1.8" diff --git a/src/HerbSpecification.jl b/src/HerbSpecification.jl index f3f7c49..123b054 100644 --- a/src/HerbSpecification.jl +++ b/src/HerbSpecification.jl @@ -1,5 +1,7 @@ module HerbSpecification +using AutoHashEquals + include("problem.jl") export diff --git a/src/problem.jl b/src/problem.jl index 05998a9..7871ed5 100644 --- a/src/problem.jl +++ b/src/problem.jl @@ -5,7 +5,7 @@ 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{InType, OutType} +@auto_hash_equals struct IOExample{InType, OutType} in::Dict{Symbol, InType} out::OutType end @@ -16,7 +16,7 @@ end A trace defining a wanted program execution for program synthesis. @TODO combine with Gen.jl """ -struct Trace{T} +@auto_hash_equals struct Trace{T} exec_path::Vector{T} end @@ -27,7 +27,7 @@ abstract type AbstractFormalSpecification end A specification based on a logical formula defined by a SMT solver. """ -struct SMTSpecification{F} <: AbstractFormalSpecification +@auto_hash_equals struct SMTSpecification{F} <: AbstractFormalSpecification formula::F end @@ -46,7 +46,7 @@ abstract type AbstractDependentTypeSpecification <: AbstractTypeSpecification en Defines a specification """ -struct AgdaSpecification{F} <: AbstractDependentTypeSpecification +@auto_hash_equals struct AgdaSpecification{F} <: AbstractDependentTypeSpecification formula::F end @@ -65,7 +65,7 @@ Program synthesis problem defined by an [`AbstractSpecification`](@ref)s. Has a !!! warning Please care that concrete `Problem` types with different values of `T` are never subtypes of each other. """ -struct Problem{T <: AbstractSpecification} +@auto_hash_equals struct Problem{T <: AbstractSpecification} name::AbstractString spec::T @@ -82,7 +82,7 @@ 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 <: AbstractVector{<:IOExample}, F} +@auto_hash_equals struct MetricProblem{T <: AbstractVector{<:IOExample}, F} name::AbstractString cost_function::F spec::T @@ -105,5 +105,3 @@ Overwrite `Base.getindex` to allow for slicing of input/output-based problems. """ 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]) - - diff --git a/test/test_ioproblem.jl b/test/test_ioproblem.jl index fb4c2ca..055c3b3 100644 --- a/test/test_ioproblem.jl +++ b/test/test_ioproblem.jl @@ -50,6 +50,12 @@ end @test subproblem.name == problem_name end end + + @testset "Test Equality" begin + p₁ = Problem([IOExample(Dict(:x => 1), 1)]) + p₂ = Problem([IOExample(Dict(:x => 1), 1)]) + @test p₁ == p₂ + end end # Tests for MetricProblem