Reference
SetBuilders.SetBuilders — ModuleMain module for SetBuilders – Julia Package for Predicate and Enumerable Sets
Exports
@setbuild: Builds a SetBuilders set.@setpkg: Loads a set from a Julia module.ismember/in/∈: Checks if an object is a member of a set.describe: Generates a set description string.fmap: Converts an element in the domain to element(s) in the codomain.bmap: Converts an element in the codomain to element(s) in the domain.complement/~: Performs the set complement operation.SBSet: Represents the top-level type of all SetBuilders sets.
Exports through the Base Module
union/∪: Performs the set union operation.intersection/∩: Performs the set intersection operation.setdiff/-: Performs the set difference operation.symdiff: Performs the set symmetric difference operation.push!: Adds an element to an EnumerableSet.pop!: Removes an element from an EnumerableSet.
SetBuilders.SBSet — TypeSBSet - TypeThe SBSet type is the supertype of all SetBuilders set types.
Examples
julia> I = @setbuild(Integer)
TypeSet(Integer)
julia> I isa SBSet
trueSetBuilders.bmap — Functionbmap(set::MappedSet, elems; on_mapping::Function, on_nomapping::Function,
on_member::Function, on_nomember::Function)convert elems in the argument to element(s) in domain of a MappedSet.
Keywords
- on_mapping: A callback function that will be called when a mapping is successful.
- on_nomapping: A callback function that will be called when a mapping is not successful.
- on_member: A callback function that will be called when a membership check is successful.
- on_nomember: A callback function that will be called when a membership check is not successful.
SetBuilders.complement — Methodcomplement(set <: SBSet)generates a complement set
julia> complement(@setbuild()) == @setbuild(Any)
true
julia> ~@setbuild() == @setbuild(Any)
trueSetBuilders.describe — Methoddescribe(set <: SBSet; mark=nothing, collect=nothing) :: Stringreturns a string describing a set
Example
julia> I = @setbuild(Integer)
TypeSet(Integer)
julia> P = @setbuild(x in I, 0 <= x < 10)
PredicateSet((x ∈ TypeSet(Integer)) where 0 <= x < 10)
julia> println(describe(P))
{ x ∈ A | 0 <= x < 10 }, where
A = { x ∈ ::Integer }Keywords
mark specifies a set to apply markings to. User also can change the marking letters by assigining a tuple of a set and a mark string.
julia> println(describe(P, mark=(I, "## ")))
{ x ∈ A | 0 <= x < 10 }, where
## A = { x ∈ ::Integer }SetBuilders.fmap — Functionfmap(set::MappedSet, elems; on_mapping::Function, on_nomapping::Function,
on_member::Function, on_nomember::Function)convert elems in the argument to element(s) in codomain of a MappedSet.
Keywords
- on_mapping: A callback function that will be called when a mapping is successful.
- on_nomapping: A callback function that will be called when a mapping is not successful.
- on_member: A callback function that will be called when a membership check is successful.
- on_nomember: A callback function that will be called when a membership check is not successful.
SetBuilders.ismember — Methodismember(elem, set <: SBSet; on_member::Function, on_nomember::Function)returns true if elem is a member of set. Otherwise returns false.
Keywords
on_member
A callback function registered with on_member will be called when elem is known to be a member of set.
on_nomember
A callback function registered with on_nomember will be called when elem is known not to be a member of set.
julia> I = @setbuild(Integer)
TypeSet(Integer)
julia> P = @setbuild(x in I, 0 <= x < 10)
PredicateSet((x ∈ TypeSet(Integer)) where 0 <= x < 10)
julia> F = h -> println(describe(h[1].set, mark=h[end].set))
#7 (generic function with 1 method)
julia> ismember(-1, P, on_nomember=F)
=> { x ∈ A | 0 <= x < 10 }, where
A = { x ∈ ::Integer }
falseSetBuilders.@setbuild — Macro@setbuild([args...[; kwargs...]])The @setbuild macro creates various SetBuilders sets.
The number of arguments varies depending on the type of set to create.
No argument
- EmptySet
julia> @setbuild()
EmptySet()One argument
- TypeSet
The first argument is any Julia type.
julia> @setbuild(Integer)
TypeSet(Integer)- EnumerableSet
The first argument is a Vector with optionally Julia type
julia> @setbuild([1, 2, 3])
EnumerableSet([{Int64}*3])
julia> @setbuild(Int32[])
EnumerableSet([{Int32}*0])- UniversalSet
The first argument is Any type
julia> @setbuild(Any)
UniversalSet()Two arguments
- PredicateSet
The first argument is the domain of the set. The second argument is the predicate of the set.
julia> I = @setbuild(Integer)
TypeSet(Integer)
julia> @setbuild(x in I, 0 <= x < 5)
PredicateSet((x ∈ TypeSet(Integer)) where 0 <= x < 5)Four arguments
- MappedSet
The first argument is the domain of the set. The second argument is the codomain of the set. The third argument is the forward mapping of the set. The fourth argument is the backward mapping of the set.
julia> I = @setbuild(Integer)
TypeSet(Integer)
julia> @setbuild(x in I, y in I, y = x + 1, x = y - 1)
MappedSet((x ∈ TypeSet(Integer)) -> (y ∈ TypeSet(Integer)))Keyword arguments
The main usage of keyword arguments is to provide expressions inside of @setbuild with the references defined outside of @setbuild.
For example, in the following code example, c=k keyword argument provides @setbuild macro with the value of k so that y = x + c or x = y - c can be correctly evaluated.
julia> I = @setbuild(Integer)
TypeSet(Integer)
julia> k = 1
1
julia> @setbuild(x in I, y in I, y = x + c, x = y - c, c=k)
MappedSet((x ∈ TypeSet(Integer)) -> (y ∈ TypeSet(Integer)))Keyword names starting with sb_ are reserved for internal uses by SetBuilders. The keyword arguments sbonmember and sbonnomember are equivalent to the onmember and onnomember keyword arguments for the ismember function. One difference with the sbon* keywords is that their effects apply to every membership check after the set's creation.
SetBuilders.@setpkg — Macro@setpkg command[ command-arguments... ]The @setpkg macro enables sharing sets among developers and users.
commands
load: loads sets from a local file, also known as a setfile
@setpkg load <path/to/file>The setfile is a regular Julia module customized for SetBuilders.
Examples
Assuming that the file myset.sjl contains the following Julia code:
module MySetModule
export MYSET
I = @setbuild(Integer)
MYSET = @setbuild(x in I, x > 0)
endMYSET can be used as shown in the example below:
julia> @setpkg load "myset.sjl"
julia> using SetBuilders.MySetModule
julia> 1 in MYSET
true
julia> 0 in MYSET
falsekeyword names starting with "sb_" are reserved for SetBuilders internal uses.