package acgtk
Install
Dune Dependency
Authors
Maintainers
Sources
sha512=5d380a947658fb1201895cb4cb449b1f60f54914c563e85181d628a89f045c1dd7b5b2226bb7865dd090f87caa9187e0ea6c7a4ee3dc3dda340d404c4e76c7c2
doc/scripting.html
Scripting language
Introduction
In this chapter, we describe the scripting language which comes with ACGtk. This language, combined with the language of ACG grammars, is used to query ACGtk to perform operations on the grammars and terms, such as parsing or realisation. These queries are called commands.
Here is an example of a command:
"lambda z. John (seeks (a (unicorn z))) : string" | parse type=Sentence lex_syn | realize lex_sem;
There are three steps, called functions, executed by this command:
- The term
lambda z. John (seeks (a (unicorn z))) : string
is given to the parse function. - This function parses it in the lexicon
lex_syn
as aSentence
and sends all resulting terms to the realize function. - Then all these terms are realised in the lexicon
lex_sem
and the results are displayed.
Environment
The environment of a scripting session contains ACG definitions (signatures and lexicons). It is empty at the beginning of a session and some commands can modify it.
Functions
Functions are the basic block of this scripting language. Some functions are predefined, and it is also possible to define new functions by composing existing functions.
As shown in the above example, most functions take as input a (possibly empty) list of λ-terms and output an other list, which is meant to be given as input to the next function. In addition, some functions take additional arguments, such as a signature or lexicon name.
Arguments
Syntax
All arguments have a name and a type. In the following sections, this syntax is used to describe the list of arguments of a function:
f arg1:t1 arg2:t2 arg3=val3:t3
This represents the example function f
, which takes 3 arguments, named arg1
, arg2
and arg3
, in this order. Their types are respectively t1
, t2
and t3
, and the third argument (arg3
) has a default value (val3
).
The arguments must be given to a function separated by spaces, and in the right order, except if the name of the argument is explicitly written. Some arguments may have a default value, so they can be omitted, and all the others argument must be given.
For example, all these calls to the function f
are equivalents:
f arg1=val1 arg2=val2 arg3=val3 f val1 val2 val3 f val1 val2 f arg3=val3 arg1=val1 arg2=val2 f arg2=val2 val1
For boolean arguments, there is an alternative syntax with +
or -
signs. If arg_bool
is a boolean argument of function f
, the two following calls are equivalents and set the value of arg_bool
to true
:
f arg_bool=true f +arg_bool
And these two calls are also equivalent, with the value of arg_bool
set to false
:
f arg_bool=false f -arg_bool
Types
The arguments of a function can have the following types:
- String (
string
): an example of literal value of typestring
is"example"
. The quotes are optional when the string contains only digits, letters,-
,.
and/
, and mandatory otherwise.
- Integer (
int
): an example of literal value of typeint
is230
.
- Boolean (
bool
): there exists two possible values of typebool
:true
andfalse
.
- ACG entry (
entry
): a valid value of typeentry
is the name of a lexicon or a signature loaded in the environment.
- ACG lexicon (
lex
): a valid value of typelex
is the name of a lexicon loaded in the environment.
- ACG signature (
sig
): a valid value of typesig
is the name of a signature loaded in the environment. It is also possible to give a lexicon name where a signature name is expected, and the abstract signature of the lexicon will be used.
- ACG type (
type
): a valid value of type lex is a type written in ACG syntax, for exampleSentence
or"NP => (NP -> NP) -> VP"
. The quotes are optional for atomic types, and mandatory in other cases.
If the type name is followed by a plus sign +
, it means that a list is expected, with elements of this type separated by commas. For example, a valid value of type string+
is "string with spaces", string_without_spaces, "third string"
. A list may contain only one element, so "one string"
is also a valid value of type string+
.
List of predefined functions
In this part, we list the predefined functions. When [terms] |
is written before a function description, it means that this function expects a list of terms as input. When | [terms]
is written after a function description, it means that this function outputs a list of terms, which can be used as input of an other function, with a |
, or just printed as result of the whole command.
last | [terms]
This function returns the result of the last successful command. If there was no successful command in the current session, this function will throw an error.
[terms] | limit n=1:int | [terms]
This function truncate its input list to size
n
, and outputs it with no other changes. Ifn
is greater or equal to the size of its input list, it does nothing.
load paths:string+
This function loads all files of the list
paths
in the environment. They can either be ACG data (usually.acg
file) or compiled ACG data (usually.acgo
file).
[terms] | parse lexicon:lex type:type magic=false:bool stack_limit=5:int | [terms]
This function parses the first λ-term of its input list in the lexicon
lexicon
, using typetype
, and output the list of resulting λ-terms. The output list may be infinite or empty. Resulting λ-terms are presented according to increasing (depth, size) values (where depth represent the tree depth and size the overall number of nodes). In case of very ambiguous grammars, this can result in slow output. The optionalstack_limit
value sets the limit (10^stack_limit) beyond which sorting is not ensured, allowing for faster output.
[terms] | realize lexicons:lex+ svg="":string | [terms]
This function realizes all the λ-terms of its input list all lexicons in the list
lexicons
, and output the list of resulting λ-terms. Ifsvg
is not the empty string, it also saves a graph of the realizations in the filesvg
.
[terms] | check signature:sig | [terms]
This function typechecks all the λ-terms of its input list in the signature
signature
, and outputs them unchanged. So this function will do nothing if all the terms are correct, but will throw an error otherwise. Only terms typed by hand (using the term literal syntax) may be incorrect.
list-terms signature:sig type:type min_depth=0:int max_depth=10:int random=false:bool | [terms]
This function computes all possible λ-terms of type
type
in the signaturesignature
, with a depth betweenmin_depth
andmax_depth
, and outputs a list with all of these terms. This function is deterministic whenrandom
is false, otherwise the order of the generated terms will be random.
compose lexicon1:lex lexicon2:lex name:string
This function creates a new lexicon named
name
by composinglexicon1
withlexicon2
and adds it to the current environment.
idb lexicon:lex
This function prints the datalog program correspoding to the lexicon
lexicon
.
[terms] | query lexicon:lex type:type
This function outputs the facts (extensional database) and the query associated to its input term list of distinguished type
type
with respect to the lexiconlexicon
.
list
This function lists the signatures and lexicons in the current environment. This function can only be used alone in a command.
print entry:entry
This function prints the entry
entry
.
help fun="":string
Prints this help message: lists the functions in the current environment and their description. If parameter
fun
is not empty, lists all functions which name starts with its value.
Commands
There are two types of commands:
- Functions execution: this allows to execute one or more function, composed by the pipe
|
operator. - Function definition: this allows to assign a name to a new function which won't be executed now. It is also possible to define a list of arguments which will be given to the functions used inside the new defined function.
In the interactive mode, a command may end with a ;
, but it is not mandatory. In script mode, the ;
is always mandatory at the end of commands.
Functions execution
A command can execute one or more functions. If there are multiple functions in a single command, they are composed by the pipe |
operator, which gives the list of λ-terms for a function to the following function.
For example, in this command:
list-terms sig Sentence | realize lex
There are two function calls, the first one is list-terms sig Sentence
, which will output terms. These terms will be given as input to the second function call, realize lex
, which will also output other terms. Since it is the last function of the command, its output will be printed.
Term literal
A command may also start by a term literal. It will be given as input to the next function of the command. There are two possible syntaxes:
At the beginning of the command, followed by a pipe operator, for example:
"Eat John (A Pizza) : S" | realize sig
At the end of the command, preceded by a "<" operator. This syntax is allowed only when the command contains exactly one function. For example:
realize sig < "Eat John (A Pizza) : S"
These two commands are equivalent.
Function definition
It is possible to define custom functions by composing some other functions. The custom functions can have parameters, to give to the functions used in its definition.
Example of a function definition:
let parse_real lex1 t lex2 n=2 := parse lex1 t | limit n | realize lex2;
The parameters may have a default value, given with the operator "=" (in the example, the parameter n
has the default value 2
).
After the definition, it is possible to call it by its name (here parse_real
):
"lambda z. mary (loves (john z) : str" | parse_real string_lex sem_lex t=Sentence;
Examples
In this section we give some examples of commands. These commands do not end with a ;
, to they are only valid in interactive mode. To use them in scripts, a ;
must be added at the end of each command.
To print the help:
help
To load the file
"grammar.acgo"
:load grammar.acgo
To check if a term is correct in
sig
:"Loves John Mary : S" | check sig
To realize a term in the lexicon
lex
:"Seeks John (A Unicorn) : S" | realize lex
To parse a term in
lex1
, and then realize all resulting terms inlex2
:"lambda z. john (seeks (a (unicorn z))) : str" | parse lex1 Sentence | realize lex2
To generate a term of type
S
insig
:list-terms sig S | limit
To generate a list of 6 terms of type
S
insig
and realize them inlex
:list-terms sig S | limit 6 | realize lex
To create a graph in "graph.svg" of the realization of the last term in lexicons
lex1
andlex2
:last | limit | realize svg="graph.svg" lex1,lex2
To define a custom function to parse a term and realize all the resulting terms:
let parse_real parse_lex parse_type real_lex := parse parse_lex parse_type | realize real_lex