- Documentation
- Reference manual
- The SWI-Prolog library
- library(aggregate): Aggregation operators on backtrackable predicates
- library(ansi_term): Print decorated text to ANSI consoles
- library(apply): Apply predicates on a list
- library(assoc): Association lists
- library(broadcast): Broadcast and receive event notifications
- library(charsio): I/O on Lists of Character Codes
- library(check): Consistency checking
- library(clpb): CLP(B): Constraint Logic Programming over Boolean Variables
- library(clpfd): CLP(FD): Constraint Logic Programming over Finite Domains
- library(clpqr): Constraint Logic Programming over Rationals and Reals
- library(csv): Process CSV (Comma-Separated Values) data
- library(dcg/basics): Various general DCG utilities
- library(dcg/high_order): High order grammar operations
- library(debug): Print debug messages and test assertions
- library(dicts): Dict utilities
- library(error): Error generating support
- library(gensym): Generate unique identifiers
- library(intercept): Intercept and signal interface
- library(iostream): Utilities to deal with streams
- library(listing): List programs and pretty print clauses
- library(lists): List Manipulation
- library(main): Provide entry point for scripts
- library(nb_set): Non-backtrackable set
- library(www_browser): Activating your Web-browser
- library(occurs): Finding and counting sub-terms
- library(option): Option list processing
- library(optparse): command line parsing
- library(ordsets): Ordered set manipulation
- library(pairs): Operations on key-value lists
- library(persistency): Provide persistent dynamic predicates
- library(pio): Pure I/O
- library(predicate_options): Declare option-processing of predicates
- library(prolog_jiti): Just In Time Indexing (JITI) utilities
- library(prolog_pack): A package manager for Prolog
- library(prolog_xref): Prolog cross-referencer data collection
- library(quasi_quotations): Define Quasi Quotation syntax
- library(random): Random numbers
- library(readutil): Read utilities
- library(record): Access named fields in a term
- library(registry): Manipulating the Windows registry
- library(settings): Setting management
- library(strings): String utilities
- library(simplex): Solve linear programming problems
- library(solution_sequences): Modify solution sequences
- library(tables): XSB interface to tables
- library(terms): Term manipulation
- library(thread): High level thread primitives
- library(thread_pool): Resource bounded thread management
- library(ugraphs): Unweighted Graphs
- library(url): Analysing and constructing URL
- library(varnumbers): Utilities for numbered terms
- library(yall): Lambda expressions
- The SWI-Prolog library
- Packages
- Reference manual
A.26 library(option): Option list processing
- See also
- -
library(record)
- Option processing capabilities may be declared using the directive predicate_options/3. - To be done
- We should consider putting many options in an assoc or record with appropriate preprocessing to achieve better performance.
The library(option)
provides some utilities for
processing option lists. Option lists are commonly used as an
alternative for many arguments. Examples of built-in predicates are open/4
and write_term/3.
Naming the arguments results in more readable code, and the list nature
makes it easy to extend the list of options accepted by a predicate.
Option lists come in two styles, both of which are handled by this
library.
- Name(Value)
- This is the preferred style.
- Name = Value
- This is often used, but deprecated.
Processing options inside time-critical code (loops) can cause
serious overhead. One possibility is to define a record using library(record)
and initialise this using make_<record>/2.
In addition to providing good performance, this also provides
type-checking and central declaration of defaults.
:- record atts(width:integer=100, shape:oneof([box,circle])=box). process(Data, Options) :- make_atts(Options, Attributes), action(Data, Attributes). action(Data, Attributes) :- atts_shape(Attributes, Shape), ...
Options typically have exactly one argument. The library does support options with 0 or more than one argument with the following restrictions:
- The predicate option/3
and select_option/4,
involving default are meaningless. They perform an
arg(1, Option, Default)
, causing failure without arguments and filling only the first option-argument otherwise. - meta_options/3 can only qualify options with exactly one argument.
- [semidet]option(?Option, +OptionList, +Default)
- Get an Option from OptionList. OptionList
can use the Name=Value as well as the Name(Value) convention.
Option Term of the form Name(?Value). - [semidet]option(?Option, +OptionList)
- Get an Option from OptionList. OptionList
can use the Name=Value as well as the Name(Value) convention. Fails
silently if the option does not appear in OptionList.
Option Term of the form Name(?Value). - [semidet]select_option(?Option, +Options, -RestOptions)
- Get and remove Option from an option list. As option/2, removing the matching option from Options and unifying the remaining options with RestOptions.
- [det]select_option(?Option, +Options, -RestOptions, +Default)
- Get and remove Option with default value. As select_option/3, but if Option is not in Options, its value is unified with Default and RestOptions with Options.
- [det]merge_options(+New, +Old, -Merged)
- Merge two option lists. Merged is a sorted list of options
using the canonical format Name(Value) holding all options from New
and Old, after removing conflicting options from Old.
Multi-values options (e.g.,
proxy(Host, Port)
) are allowed, where both option-name and arity define the identity of the option. - [det]meta_options(+IsMeta, :Options0, -Options)
- Perform meta-expansion on options that are module-sensitive. Whether an
option name is module-sensitive is determined by calling
call(IsMeta, Name)
. Here is an example:meta_options(is_meta, OptionsIn, Options), ... is_meta(callback).
Meta-options must have exactly one argument. This argument will be qualified.
- To be done
- Should be integrated with declarations from predicate_options/3.
- [det]dict_options(?Dict, ?Options)
- Convert between an option list and a dictionary. One of the arguments
must be instantiated. If the option list is created, it is created in
canonical form, i.e., using Option(Value) with the Options
sorted in the standard order of terms. Note that the conversion is not
always possible due to different constraints and conversion may thus
lead to (type) errors.
- Dict keys can be integers. This is not allowed in canonical option lists.
- Options can hold multiple options with the same key. This is not allowed in dicts.
- Options can have more than one value (
name(V1,V2)
). This is not allowed in dicts.
Also note that most system predicates and predicates using this library for processing the option argument can both work with classical Prolog options and dicts objects.