From cb2aa8b8d35b5add785d123b6f6907d207fbcdf5 Mon Sep 17 00:00:00 2001 From: mforets Date: Fri, 1 Mar 2019 19:54:03 -0300 Subject: [PATCH 01/11] add subtypes function --- src/helper_functions.jl | 116 ++++++++++++++++++++++++++++ test/check_method_implementation.jl | 17 +--- 2 files changed, 117 insertions(+), 16 deletions(-) diff --git a/src/helper_functions.jl b/src/helper_functions.jl index 1ac0b3de72..780c1618d5 100644 --- a/src/helper_functions.jl +++ b/src/helper_functions.jl @@ -1,3 +1,5 @@ +import InteractiveUtils: subtypes + # default tolerance for matrix condition number (see 'isinvertible') const DEFAULT_COND_TOL = 1e6 @@ -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) +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) +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) +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) + + subtypes_to_test = subtypes(interface) + @assert !isempty(subtypes_to_test) "an interface must have a subtype" + + # 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 diff --git a/test/check_method_implementation.jl b/test/check_method_implementation.jl index e0eccd45b6..69eeb1006b 100644 --- a/test/check_method_implementation.jl +++ b/test/check_method_implementation.jl @@ -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 From fdfd3ade4a52492ffe0d09a2d1f7ba279b324d44 Mon Sep 17 00:00:00 2001 From: mforets Date: Sat, 2 Mar 2019 12:13:18 -0300 Subject: [PATCH 02/11] revise subtypes --- docs/src/lib/utils.md | 6 ++++++ src/LazySet.jl | 46 +++++++++++++++++++++++++++++++++++++++++ src/helper_functions.jl | 12 +++++------ 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/docs/src/lib/utils.md b/docs/src/lib/utils.md index 0fae7f8e1a..f2178fad11 100644 --- a/docs/src/lib/utils.md +++ b/docs/src/lib/utils.md @@ -46,3 +46,9 @@ CachedPair Approximations.UnitVector StrictlyIncreasingIndices ``` + +### Inspection of set interfaces + +```@docs +subtypes(::Type, ::Bool=false) +``` diff --git a/src/LazySet.jl b/src/LazySet.jl index b275cd5104..0f578947a0 100644 --- a/src/LazySet.jl +++ b/src/LazySet.jl @@ -33,6 +33,8 @@ Every concrete `LazySet` must define the following functions: `Real` - `dim(S::LazySet)::Int` -- the ambient dimension of `S` +The subytpes of `LazySet`, including abstract interfaces: + ```jldoctest julia> subtypes(LazySet) 17-element Array{Any,1}: @@ -54,6 +56,50 @@ julia> subtypes(LazySet) ResetMap Translation ``` + +If we only consider *concrete* subtypes, then: + +```jldoctest +julia> subtypes(LazySet, true) +37-element Array{Type,1}: + CacheMinkowskiSum + CartesianProduct + CartesianProductArray + ConvexHull + ConvexHullArray + EmptySet + ExponentialMap + ExponentialProjectionMap + Intersection + IntersectionArray + LinearMap + MinkowskiSum + MinkowskiSumArray + ResetMap + Translation + Ball2 + Ballp + Ellipsoid + HPolyhedron + HalfSpace + Hyperplane + Line + Universe + HPolytope + VPolytope + Ball1 + LineSegment + Zonotope + VPolygon + BallInf + Hyperrectangle + Interval + SymmetricIntervalHull + HPolygon + HPolygonOpt + Singleton + ZeroSet + ``` """ abstract type LazySet{N} end diff --git a/src/helper_functions.jl b/src/helper_functions.jl index 780c1618d5..29c9d61ba6 100644 --- a/src/helper_functions.jl +++ b/src/helper_functions.jl @@ -529,15 +529,16 @@ end # @eval end # if """ - subtypes(interface::Type, concrete::Bool=true) + subtypes(interface::Type, concrete::Bool=false) 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 +- `concrete` -- (optional, default: `false`) if `true`, seek further the inner + abstract subtypes of the given interface, otherwise return abstract + subtypes of `interface` ### Output @@ -615,11 +616,10 @@ julia> subtypes(Real, true) UInt8 ``` """ -function subtypes(interface, concrete::Bool=true) +function subtypes(interface::Type, concrete::Bool=false) subtypes_to_test = subtypes(interface) - @assert !isempty(subtypes_to_test) "an interface must have a subtype" - + # do not seek the concrete subtypes further if !concrete return subtypes_to_test From ac2d9a77fed35e7221a66f50755d65c8bbffaca7 Mon Sep 17 00:00:00 2001 From: mforets Date: Sat, 2 Mar 2019 13:09:13 -0300 Subject: [PATCH 03/11] sort output and remove type annotation --- docs/src/lib/utils.md | 2 +- src/LazySet.jl | 38 ++++++++++++++++++------------------ src/helper_functions.jl | 43 ++++++++++++++++++++--------------------- 3 files changed, 41 insertions(+), 42 deletions(-) diff --git a/docs/src/lib/utils.md b/docs/src/lib/utils.md index f2178fad11..fae9dcdc15 100644 --- a/docs/src/lib/utils.md +++ b/docs/src/lib/utils.md @@ -50,5 +50,5 @@ StrictlyIncreasingIndices ### Inspection of set interfaces ```@docs -subtypes(::Type, ::Bool=false) +subtypes(::Any, ::Bool) ``` diff --git a/src/LazySet.jl b/src/LazySet.jl index 0f578947a0..7895aa1cc7 100644 --- a/src/LazySet.jl +++ b/src/LazySet.jl @@ -62,43 +62,43 @@ If we only consider *concrete* subtypes, then: ```jldoctest julia> subtypes(LazySet, true) 37-element Array{Type,1}: + Ball1 + Ball2 + BallInf + Ballp CacheMinkowskiSum CartesianProduct CartesianProductArray ConvexHull ConvexHullArray + Ellipsoid EmptySet ExponentialMap ExponentialProjectionMap + HPolygon + HPolygonOpt + HPolyhedron + HPolytope + HalfSpace + Hyperplane + Hyperrectangle Intersection IntersectionArray + Interval + Line + LineSegment LinearMap MinkowskiSum MinkowskiSumArray ResetMap + Singleton + SymmetricIntervalHull Translation - Ball2 - Ballp - Ellipsoid - HPolyhedron - HalfSpace - Hyperplane - Line Universe - HPolytope - VPolytope - Ball1 - LineSegment - Zonotope VPolygon - BallInf - Hyperrectangle - Interval - SymmetricIntervalHull - HPolygon - HPolygonOpt - Singleton + VPolytope ZeroSet + Zonotope ``` """ abstract type LazySet{N} end diff --git a/src/helper_functions.jl b/src/helper_functions.jl index 29c9d61ba6..6ad1554e7b 100644 --- a/src/helper_functions.jl +++ b/src/helper_functions.jl @@ -529,20 +529,19 @@ end # @eval end # if """ - subtypes(interface::Type, concrete::Bool=false) + subtypes(interface, concrete::Bool) Return the concrete subtypes of a given interface. ### Input - `interface` -- an abstract type, usually a set interface -- `concrete` -- (optional, default: `false`) if `true`, seek further the inner - abstract subtypes of the given interface, otherwise return abstract - subtypes of `interface` +- `concrete` -- if `true`, seek further the inner abstract subtypes of the given + interface, otherwise return abstract subtypes of `interface` ### Output -A list with the subtypes of the abstract type `interface`. +A list with the subtypes of the abstract type `interface`, sorted alphabetically. ### Examples @@ -565,20 +564,20 @@ concrete types are obtained), using the `concrete` flag: ```jldoctest julia> subtypes(AbstractPolytope, true) 14-element Array{Type,1}: - HPolytope - VPolytope Ball1 - LineSegment - Zonotope - VPolygon BallInf - Hyperrectangle - Interval - SymmetricIntervalHull HPolygon HPolygonOpt + HPolytope + Hyperrectangle + Interval + LineSegment Singleton + SymmetricIntervalHull + VPolygon + VPolytope ZeroSet + Zonotope ``` This function can be applied to other abstract types as well: @@ -594,21 +593,21 @@ julia> subtypes(Real, false) julia> subtypes(Real, true) 20-element Array{Type,1}: - Rational BigFloat + BigInt + Bool Float16 Float32 Float64 - Irrational - Bool - IntervalArithmetic.DecoratedInterval - IntervalArithmetic.Interval - BigInt Int128 Int16 Int32 Int64 Int8 + IntervalArithmetic.DecoratedInterval + IntervalArithmetic.Interval + Irrational + Rational UInt128 UInt16 UInt32 @@ -616,13 +615,13 @@ julia> subtypes(Real, true) UInt8 ``` """ -function subtypes(interface::Type, concrete::Bool=false) +function subtypes(interface, concrete::Bool) subtypes_to_test = subtypes(interface) # do not seek the concrete subtypes further if !concrete - return subtypes_to_test + return sort(subtypes_to_test, by=string) end result = Vector{Type}() @@ -639,5 +638,5 @@ function subtypes(interface::Type, concrete::Bool=false) append!(subtypes_to_test, new_subtypes) end end - return result + return sort(result, by=string) end From 0b46b92dd2cace691c13613061674f9fc7c4d82f Mon Sep 17 00:00:00 2001 From: mforets Date: Sat, 2 Mar 2019 13:13:49 -0300 Subject: [PATCH 04/11] fix concrete docs --- src/helper_functions.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helper_functions.jl b/src/helper_functions.jl index 6ad1554e7b..776f05b656 100644 --- a/src/helper_functions.jl +++ b/src/helper_functions.jl @@ -537,7 +537,7 @@ Return the concrete subtypes of a given interface. - `interface` -- an abstract type, usually a set interface - `concrete` -- if `true`, seek further the inner abstract subtypes of the given - interface, otherwise return abstract subtypes of `interface` + interface, otherwise return only the direct subtypes of `interface` ### Output From 8062a7559095f0f2d15d351acf52dcf0adad67fb Mon Sep 17 00:00:00 2001 From: Marcelo Forets Date: Sat, 2 Mar 2019 17:12:32 -0300 Subject: [PATCH 05/11] Update docs/src/lib/utils.md --- docs/src/lib/utils.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/lib/utils.md b/docs/src/lib/utils.md index fae9dcdc15..2079fa6bb8 100644 --- a/docs/src/lib/utils.md +++ b/docs/src/lib/utils.md @@ -50,5 +50,5 @@ StrictlyIncreasingIndices ### Inspection of set interfaces ```@docs -subtypes(::Any, ::Bool) +LazySets.subtypes(::Any, ::Bool) ``` From 4cc27cfd0bfd0e2092deab05949fa2c749b6b32b Mon Sep 17 00:00:00 2001 From: Marcelo Forets Date: Sat, 2 Mar 2019 17:13:05 -0300 Subject: [PATCH 06/11] Update src/LazySet.jl --- src/LazySet.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LazySet.jl b/src/LazySet.jl index 7895aa1cc7..3534071c3f 100644 --- a/src/LazySet.jl +++ b/src/LazySet.jl @@ -60,7 +60,7 @@ julia> subtypes(LazySet) If we only consider *concrete* subtypes, then: ```jldoctest -julia> subtypes(LazySet, true) +julia> LazySets.subtypes(LazySet, true) 37-element Array{Type,1}: Ball1 Ball2 From f7f5bd4c0337ca744398e76535a911b928bc024c Mon Sep 17 00:00:00 2001 From: Marcelo Forets Date: Sat, 2 Mar 2019 17:13:21 -0300 Subject: [PATCH 07/11] Update src/helper_functions.jl --- src/helper_functions.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helper_functions.jl b/src/helper_functions.jl index 776f05b656..a5f69f1193 100644 --- a/src/helper_functions.jl +++ b/src/helper_functions.jl @@ -549,7 +549,7 @@ Consider the `AbstractPolytope` interface. If we include the abstract subtypes of this interface, ```jldoctest -julia> subtypes(AbstractPolytope, false) +julia> LazySets.subtypes(AbstractPolytope, false) 4-element Array{Any,1}: AbstractCentrallySymmetricPolytope AbstractPolygon From f6fb3b8e846025d16cae389a14058786010f4ff3 Mon Sep 17 00:00:00 2001 From: Marcelo Forets Date: Sat, 2 Mar 2019 17:13:46 -0300 Subject: [PATCH 08/11] Update src/helper_functions.jl --- src/helper_functions.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helper_functions.jl b/src/helper_functions.jl index a5f69f1193..112e85b869 100644 --- a/src/helper_functions.jl +++ b/src/helper_functions.jl @@ -562,7 +562,7 @@ We can use this function to obtain the concrete subtypes of concrete types are obtained), using the `concrete` flag: ```jldoctest -julia> subtypes(AbstractPolytope, true) +julia> LazySets.subtypes(AbstractPolytope, true) 14-element Array{Type,1}: Ball1 BallInf From 694f26115e7559b9124b3ef1a2b447d0971dac27 Mon Sep 17 00:00:00 2001 From: mforets Date: Sat, 2 Mar 2019 17:33:18 -0300 Subject: [PATCH 09/11] revise docs --- src/LazySet.jl | 6 ++++-- src/helper_functions.jl | 24 +++++++++++++----------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/LazySet.jl b/src/LazySet.jl index 3534071c3f..06775f8530 100644 --- a/src/LazySet.jl +++ b/src/LazySet.jl @@ -33,10 +33,12 @@ Every concrete `LazySet` must define the following functions: `Real` - `dim(S::LazySet)::Int` -- the ambient dimension of `S` -The subytpes of `LazySet`, including abstract interfaces: +The subtypes of `LazySet` (including abstract interfaces): ```jldoctest -julia> subtypes(LazySet) +julia> using LazySet: subtypes + +julia> subtypes(LazySet, false) 17-element Array{Any,1}: AbstractCentrallySymmetric AbstractPolyhedron diff --git a/src/helper_functions.jl b/src/helper_functions.jl index 112e85b869..3b75096975 100644 --- a/src/helper_functions.jl +++ b/src/helper_functions.jl @@ -49,9 +49,7 @@ It can be used with vector-valued arguments via the dot operator. ### Examples ```jldoctest -julia> import LazySets.sign_cadlag - -julia> sign_cadlag.([-0.6, 1.3, 0.0]) +julia> LazySets.sign_cadlag.([-0.6, 1.3, 0.0]) 3-element Array{Float64,1}: -1.0 1.0 @@ -211,13 +209,15 @@ and `(false, 0)` otherwise. ### Examples ```jldoctest -julia> LazySets.samedir([1, 2, 3], [2, 4, 6]) +julia> using LazySets: samedir + +julia> samedir([1, 2, 3], [2, 4, 6]) (true, 0.5) -julia> LazySets.samedir([1, 2, 3], [3, 2, 1]) +julia> samedir([1, 2, 3], [3, 2, 1]) (false, 0) -julia> LazySets.samedir([1, 2, 3], [-1, -2, -3]) +julia> samedir([1, 2, 3], [-1, -2, -3]) (false, 0) ``` @@ -548,8 +548,10 @@ A list with the subtypes of the abstract type `interface`, sorted alphabetically Consider the `AbstractPolytope` interface. If we include the abstract subtypes of this interface, -```jldoctest -julia> LazySets.subtypes(AbstractPolytope, false) +```jldoctest subtypes +julia> using LazySets: subtypes + +julia> subtypes(AbstractPolytope, false) 4-element Array{Any,1}: AbstractCentrallySymmetricPolytope AbstractPolygon @@ -561,8 +563,8 @@ 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> LazySets.subtypes(AbstractPolytope, true) +```jldoctest subtypes +julia> subtypes(AbstractPolytope, true) 14-element Array{Type,1}: Ball1 BallInf @@ -582,7 +584,7 @@ julia> LazySets.subtypes(AbstractPolytope, true) This function can be applied to other abstract types as well: -```jldoctest +```jldoctest subtypes julia> subtypes(Real, false) 5-element Array{Any,1}: AbstractFloat From aae7e2c85e6dc29684d5ac8b4e8b44bb2954ebfb Mon Sep 17 00:00:00 2001 From: mforets Date: Sat, 2 Mar 2019 18:48:25 -0300 Subject: [PATCH 10/11] fix typo --- src/LazySet.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LazySet.jl b/src/LazySet.jl index 06775f8530..489475c61c 100644 --- a/src/LazySet.jl +++ b/src/LazySet.jl @@ -36,7 +36,7 @@ Every concrete `LazySet` must define the following functions: The subtypes of `LazySet` (including abstract interfaces): ```jldoctest -julia> using LazySet: subtypes +julia> using LazySets: subtypes julia> subtypes(LazySet, false) 17-element Array{Any,1}: From fc57dfa9cefb02a81d47208748232a11c6dca266 Mon Sep 17 00:00:00 2001 From: mforets Date: Mon, 4 Mar 2019 10:42:27 -0300 Subject: [PATCH 11/11] revise subtypes --- src/helper_functions.jl | 41 +++++------------------------------------ 1 file changed, 5 insertions(+), 36 deletions(-) diff --git a/src/helper_functions.jl b/src/helper_functions.jl index 3b75096975..37e2c95465 100644 --- a/src/helper_functions.jl +++ b/src/helper_functions.jl @@ -1,4 +1,8 @@ -import InteractiveUtils: subtypes +@static if VERSION < v"0.7-" + import Base.subtypes +else + import InteractiveUtils: subtypes +end # default tolerance for matrix condition number (see 'isinvertible') const DEFAULT_COND_TOL = 1e6 @@ -581,41 +585,6 @@ julia> subtypes(AbstractPolytope, true) ZeroSet Zonotope ``` - -This function can be applied to other abstract types as well: - -```jldoctest subtypes -julia> subtypes(Real, false) -5-element Array{Any,1}: - AbstractFloat - AbstractIrrational - Integer - IntervalArithmetic.AbstractInterval - Rational - -julia> subtypes(Real, true) -20-element Array{Type,1}: - BigFloat - BigInt - Bool - Float16 - Float32 - Float64 - Int128 - Int16 - Int32 - Int64 - Int8 - IntervalArithmetic.DecoratedInterval - IntervalArithmetic.Interval - Irrational - Rational - UInt128 - UInt16 - UInt32 - UInt64 - UInt8 -``` """ function subtypes(interface, concrete::Bool)