Did you know ... Search Documentation:
SWI-Prolog owl logo library(option): Option list processing

A.32 library(option): Option list processing

See also
- library(record)
- Option processing capabilities may be declared using the directive predicate_options/3.

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.

SWI-Prolog dicts provide a convenient and efficient alternative to option lists. For this reason, both built-in predicates and predicates that use this library support dicts transparantly.

Processing option lists inside time-critical code (loops) can cause serious overhead. The above mentioned dicts is the preferred mitigation. A more portable alternative 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.

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, +Options)
Get an Option from Options. Fails silently if the option does not appear in Options. If Option appears multiple times in Options, the first value is used.
Option Term of the form Name(?Value).
Options is a list of Name(Value) or Name=Value or a dict.
[det]option(?Option, +Options, +Default)
Get an Option from Options. If Option does not appear in Options, unify the value with Default. If Option appears multiple times in Options, the first value is used. For example
?- option(max_depth(D), [x(a), max_depth(20)], 10).
D = 20.
?- option(max_depth(D), [x(a)], 10).
D = 10.
Option Term of the form Name(?Value).
Options is a list of Name(Value) or Name=Value or a dict.
[semidet]select_option(?Option, +Options, -RestOptions)
Get and remove Option from Options. As option/2, removing the matching option from Options and unifying the remaining options with RestOptions. If Option appears multiple times in Options, the first value is used. Note that if Options contains multiple terms that are compatible to Option, the first is used to set the value of Option and the duplicate appear in 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 sets. If Old is a dict, Merged is a dict. Otherwise 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. This predicate removes all but the first option on the same key.
  • 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.

Tags are associated to your profile if you are logged in
Tags:
LogicalCaptain said (2020年10月31日T10:19:34):0 upvotes 0 0 downvotes
Picture of user LogicalCaptain.

Note in particular that you cannot backtrack over the list of options via option/2

Esentially option/2 behaves like memberchk/2 but demands that the 'Option' argument not be an uninstantiated variable.

This precludes detecting option repetition (as in [verbose(true),verbose(true)]) or options with multiple values (as in [file(foo),file(bar)]). You will have to implement this differently.

However, you can easily override "older" options with "newer" one added at the head of the Options list:

Expected, you get the "earliest b(X)":

?- option(b(X),[a(1),b(2),b(3),c(4)]).
X = 2.

Maybe unexpected: "there is no b(3) ... or it is overriden by some other b(X)"

?- option(b(3),[a(1),b(2),b(3),c(4)]).
false.

(Would it be nicer to allow backtracking?)

How it is done in (for example) venerable Perl: https://perldoc.perl.org/Getopt/Long.html#Options-with-multiple-values.

Fun with testing:

:- begin_tests(options).
test("Getting an option from an empty option-list fails as expected",fail) :-
 option(b(_),[]).
test("Getting a option from an option-list that doesn't contain that option fails as expected",fail) :-
 option(d(_),[a(1),b(2),c(3)]).
test("Leaving the 'Option' argument an unbound variable throws (instead of backtracking over all options)", [error(instantiation_error)]) :-
 option(_,[a(1),b(2),c(3)]).
test("Normal way: Getting the argument of a b(X)",true(X == 2)) :-
 option(b(X),[a(1),b(2),c(3)]).
test("Normal way: Getting the argument of a b(X), if there are multiple solutions, yields just one solution",true(Bag == [2])) :-
 bagof(X, option(b(X),[a(1),b(2),b(3),c(3)]), Bag).
 
test("Normal way: Checking the presence of a ground b(x)",true) :-
 option(b(2),[a(1),b(2),c(3)]).
 
% This is bad and should be changed!
test("Normal way: Checking the presence of a ground b(x) that comes after a b(y) unexpectedly fails",fail) :-
 option(b(3),[a(1),b(2),b(3),c(4)]).
 
test("Normal way: Getting the argument of an option with a complex argument",true([X,Y,Z] == [1,2,3])) :-
 option(b(x(X,Y,Z)),[a(1),b(x(1,2,3)),c(3)]).
test("What if the option argument in the list is a variable? Unification happens!",true(X == Y)) :-
 option(b(X),[a(1),b(Y),c(3)]).
test("What if the option argument in the list is a variable (take 2)?",true(foo == Y)) :-
 option(b(foo),[a(1),b(Y),c(3)]).
test("A duplicate option will yield a single entry (no backtracking)",true(Bag == [2])) :-
 bagof(X, option(b(X),[a(1),b(2),b(3),c(4)]), Bag).
test("Checking the presence of an 'entry with no arguments' (an atom), which exists") :-
 option(b,[a(1),b,c(4)]).
test("Checking the presence of an 'entry with no arguments' (an atom), which does not exist", fail) :-
 option(b,[a(1),b(2),c(4)]).
test("Checking the presence of an 'entry with zero arguments' (an zero-arg compound), doesn't work", error(domain_error(_,_))) :-
 option(b(),[a(1),b(),c(4)]).
test("The 'entry with zero arguments' can be in the list but won't be found", fail) :-
 option(b,[a(1),b(),c(4)]).
test("Checking the presence of an 'entry with multiple arguments'", true([X,Y] == [2,5])) :-
 option(b(X,Y),[a(1),b(2,5),c(4)]).
test("Checking the presence of an 'entry with multiple arguments', where the 'Option' argument is partially instantiated", true([X] == [2])) :-
 option(b(X,5),[a(1),b(2,5),c(4)]).
test("Default value is used if the option is missing",true(X == false)) :-
 option(foo(X),[],false).
test("Default value is not used if the option is there",true(X == true)) :-
 option(foo(X),[foo(true)],false).
test("Complex default value is used if the option is missing",true(X == h(x,y))) :-
 option(foo(X),[],h(x,y)).
 
test("Getting default value if one is just testing",fail) :-
 option(foo(bar),[foo(true)],false).
test("Getting default value if one is just testing") :-
 option(foo(bar),[foo(bar)],false).
 
:- end_tests(options).

See also

https://eu.swi-prolog.org/pldoc/man?section=predicate_options

login to add a new annotation post.

AltStyle によって変換されたページ (->オリジナル) /