Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1159 - Add subtypes function with concrete argument #1185

Merged
merged 12 commits into from
Mar 4, 2019
116 changes: 116 additions & 0 deletions src/helper_functions.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import InteractiveUtils: subtypes
mforets marked this conversation as resolved.
Show resolved Hide resolved

# default tolerance for matrix condition number (see 'isinvertible')
const DEFAULT_COND_TOL = 1e6

Expand Down Expand Up @@ -525,3 +527,117 @@ end

end # @eval
end # if

"""
subtypes(interface::Type, concrete::Bool=true)

Return the concrete subtypes of a given interface.

### Input

- `interface` -- an abstract type, usually a set interface
- `concrete` -- (optional, default: `true`) if `true`, seek further the inner
subtypes of the given interface

### Output

A list with the subtypes of the abstract type `interface`.

### Examples

Consider the `AbstractPolytope` interface. If we include the abstract subtypes
of this interface,

```jldoctest
julia> subtypes(AbstractPolytope, false)
mforets marked this conversation as resolved.
Show resolved Hide resolved
4-element Array{Any,1}:
AbstractCentrallySymmetricPolytope
AbstractPolygon
HPolytope
VPolytope
```

We can use this function to obtain the concrete subtypes of
`AbstractCentrallySymmetricPolytope` and `AbstractPolygon` (further until all
concrete types are obtained), using the `concrete` flag:

```jldoctest
julia> subtypes(AbstractPolytope, true)
mforets marked this conversation as resolved.
Show resolved Hide resolved
14-element Array{Type,1}:
HPolytope
VPolytope
Ball1
LineSegment
Zonotope
VPolygon
BallInf
Hyperrectangle
Interval
SymmetricIntervalHull
HPolygon
HPolygonOpt
Singleton
ZeroSet
```

This function can be applied to other abstract types as well:

```jldoctest
julia> subtypes(Real, false)
mforets marked this conversation as resolved.
Show resolved Hide resolved
5-element Array{Any,1}:
AbstractFloat
AbstractIrrational
Integer
IntervalArithmetic.AbstractInterval
Rational

julia> subtypes(Real, true)
20-element Array{Type,1}:
Rational
BigFloat
Float16
Float32
Float64
Irrational
Bool
IntervalArithmetic.DecoratedInterval
IntervalArithmetic.Interval
BigInt
Int128
Int16
Int32
Int64
Int8
UInt128
UInt16
UInt32
UInt64
UInt8
```
"""
function subtypes(interface, concrete::Bool=true)
mforets marked this conversation as resolved.
Show resolved Hide resolved

subtypes_to_test = subtypes(interface)
@assert !isempty(subtypes_to_test) "an interface must have a subtype"
mforets marked this conversation as resolved.
Show resolved Hide resolved

# do not seek the concrete subtypes further
if !concrete
return subtypes_to_test
end

result = Vector{Type}()
i = 0
while i < length(subtypes_to_test)
i += 1
subtype = subtypes_to_test[i]
new_subtypes = subtypes(subtype)
if isempty(new_subtypes)
# base type found
push!(result, subtype)
else
# yet another interface layer
append!(subtypes_to_test, new_subtypes)
end
end
return result
end
17 changes: 1 addition & 16 deletions test/check_method_implementation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,7 @@ function check_method_implementation(interface::Type,
)::Bool
# first collect all base types that are subtypes of this interface
# NOTE: 'isleaftype' does not work due to type parameters
subtypes_to_test = subtypes(interface)
@assert !isempty(subtypes_to_test) "an interface must have a subtype"
base_types = Vector{Type}()
i = 0
while i < length(subtypes_to_test)
i += 1
subtype = subtypes_to_test[i]
new_subtypes = subtypes(subtype)
if isempty(new_subtypes)
# base type found
push!(base_types, subtype)
else
# yet another interface layer
append!(subtypes_to_test, new_subtypes)
end
end
base_types = LazySets.subtypes(interface, true)

# now check all base types
for subtype in base_types
Expand Down