• No se han encontrado resultados

Relación entre la plataforma y los usuarios

2.3. Carpooling: el caso de BlaBlaCar

2.3.1. Relación entre la plataforma y los usuarios

current signal(?Name, ?Id, ?Handler)

Enumerate the currently defined signal handling. Nameis the signal name,Idis the numerical identifier andHandleris the currently defined handler (seeon signal/3).

4.12.1 Notes on signal handling

Before deciding to deal with signals in your application, please consider the following: • Portability

On MS-Windows, the signal interface is severely limited. Different Unix brands support differ- ent sets of signals, and the relation between signal name and number may vary. Currently, the system only supports signals numbered 1 to 3229. Installing a signal outside the limited set of supported signals in MS-Windows crashes the application.

• Safety

Immediately delivered signals (see below) are unsafe. This implies that foreign functions called from a handler cannot safely use the SWI-Prolog API and cannot use C longjmp(). Handlers defined as throw are unsafe. Handlers defined to call a predicate are safe. Note that the predicate can callthrow/1, but the delivery is delayed until Prolog is in a safe state.

The C-interface described in section11.4.13provides the optionPL SIGSYNCto select either safe synchronous or unsafe asynchronous delivery.

• Time of delivery

Usingthrow or a foreign handler, signals are delivered immediately (as defined by the OS). When using a Prolog predicate, delivery is delayed to a safe moment. Blocking system calls or foreign loops may cause long delays. Foreign code can improve on that by calling PL handle signals().

Signals are blocked when the garbage collector is active.

4.13

DCG Grammar rules

Grammar rules form a comfortable interface todifference lists. They are designed both to support writing parsers that build a parse tree from a list of characters or tokens and for generating a flat list from a term.

Grammar rules look like ordinary clauses using-->/2 for separating the head and body rather than :-/2. Expanding grammar rules is done by expand term/2, which adds two additional arguments to each term for representing the difference list.

The body of a grammar rule can contain three types of terms. A callable term is interpreted as a reference to a grammar rule. Code between{. . .}is interpreted as plain Prolog code, and finally, a list is interpreted as a sequence ofliterals. The Prolog control-constructs (\+/1,->/2,;//2,,/2 and!/0) can be used in grammar rules.

We illustrate the behaviour by defining a rule set for parsing an integer. integer(I) -->

digit(D0),

digits(D), { number_codes(I, [D0|D]) }. digits([D|T]) --> digit(D), !, digits(T). digits([]) --> []. digit(D) --> [D], { code_type(D, digit) }.

Grammar rule sets are called using the built-in predicatesphrase/2andphrase/3: phrase(:DCGBody, ?List)

Equivalent tophrase(DCGBody, InputList, []). phrase(:DCGBody, ?List, ?Rest)

True when DCGBody applies to the differenceList/Rest. Although DCGBody is typically a callableterm that denotes a grammar rule, it can be any term that is valid as the body of a DCG rule.

The example below calls the rule setinteger//1defined in section4.13and available from library(dcg/basics), bindingRestto the remainder of the input after matching the in- teger.

?- [library(dcg/basics)].

?- atom_codes(’42 times’, Codes), phrase(integer(X), Codes, Rest). X = 42

Rest = [32, 116, 105, 109, 101, 115]

The next example exploits a complete body. Given the following definition of digit weight//1, we can pose the query below.

digit_weight(W) --> [D], { code_type(D, digit(W)) }. ?- atom_codes(’Version 3.4’, Codes), phrase(("Version ", digit_weight(Major),".",digit_weight(Minor)), Codes).

4.13. DCG GRAMMAR RULES 121

Major = 3, Minor = 4.

The SWI-Prolog implementation of phrase/3verifies that theListandRest arguments are unbound, bound to the empty list or a list cons cell. Other values raise a type error.30 The predicatecall dcg/3is provided to use grammar rules with terms that are not lists.

Note that the syntax for lists of codes changed in SWI-Prolog version 7 (see section5.2). If a DCG body is translated, both"text"and‘text‘is a valid code-list literal in version 7. A version 7 string ("text") isnotacceptable for the second and third arguments ofphrase/3. This is typically not a problem for applications as the input of a DCG rarely appears in the source code. For testing in the toplevel, one must use double quoted text in versions prior to 7 and back quoted text in version 7 or later.

See alsoportray text/1, which can be used to print lists of character codes as a string to the top level and debugger to facilitate debugging DCGs that process character codes. The library apply macroscompilesphrase/3if the argument is sufficiently instantiated, eliminating the runtime overhead of translatingDCGBodyand meta-calling.

call dcg(:DCGBody, ?State0, ?State)

Asphrase/3, but without type checkingState0andState. This allows for using DCG rules for threading an arbitrary state variable. This predicate was introduced after type checking was added tophrase/3.31

A portable solution for threading state through a DCG can be implemented by wrapping the state in a list and use the DCG semicontext facility. Subsequently, the following predicates may be used to access and modify the state:32

state(S), [S] --> [S]. state(S0, S), [S] --> [S0].

As stated above, grammar rules are a general interface to difference lists. To illustrate, we show a DCG-based implementation ofreverse/2:

reverse(List, Reversed) :-

phrase(reverse(List), Reversed). reverse([]) --> [].

reverse([H|T]) --> reverse(T), [H].

30

The ISO standard allows for both raising a type error and accepting any term as input and output. Note the tail of the list is not checked for performance reasons.

31

After discussion with Samer Abdallah.