Appendix I

Grammar

/* Picat syntax rules 
      [...] means optional
      {...} means 0, 1, or more occurrences
      (a | b) means choice
      "..." means a token
      %... one-line comment
      input tokens:
            atom_token
            integer_literal
            operator
            real_literal
            separator
            string_literal
            variable_token
            eor is "." followed by a whitespace character or eof
*/
program ->
    [module_declaration]
    {import_declaration}
    program_body

program_body ->
    {predicate_definition | function_definition | actor_definition | include_directive}

module_declaration -> 
    "module" atom_token eor

import_declaration ->
    import import_item {"," import_item} eor

import_item ->
    atom_token 

predicate_definition ->
    {predicate_directive} predicate_rule_or_fact {predicate_rule_or_fact}

function_definition ->
    {function_directive} function_rule_or_fact {function_rule_or_fact}

actor_definition ->
    ["private"] action_rule {(action_rule 
                              | nonbacktrackable_predicate_rule)}

function_directive ->
    "private"
    "table"

predicate_directive ->
    "private"
    "table" ["(" table_mode {"," table_mode} ")" ]
    "index" index_declaration {index_declaration}

include_directive ->
    "include" string_literal eor

index_declaration ->
    "(" index_mode {"," index_mode} ")"

index_mode ->
   "+"
   "-"
   
table_mode ->
   "+"
   "-"
   "min"
   "max"
   "mmin"
   "mmax"
   "nt"
      
predicate_rule_or_fact ->
    predicate_rule
    predicate_fact

function_rule_or_fact ->
    function_rule
    function_fact

predicate_rule ->
    head ["," condition] ("=>" | "?=>") body eor
    head (":-" | "-->") body eor

nonbacktrackable_predicate_rule ->
    head ["," condition] "=>" body eor

predicate_fact ->
    head eor

head ->
    atom_token ["(" [term {"," term}] ")"]

function_rule ->
    head "=" expression ["," condition] "=>" body eor    

function_fact ->
    head "=" argument eor

action_rule ->
    head ["," condition] "," "{" event_pattern "}"   => body eor

event_pattern ->
    term {',' term}

condition -> goal

body -> goal

argument ->
    negative_goal

goal ->
    disjunctive_goal

disjunctive_goal ->
    disjunctive_goal (";" | "||") conjunctive_goal
    conjunctive_goal "->" disjunctive_goal         % Prolog-style if-then
    conjunctive_goal

conjunctive_goal ->
    conjunctive_goal ("," | "&&") negative_goal
    negative_goal

negative_goal ->
   "not" negative_goal
   "\+" negative_goal
   "once" negative_goal
   equiv_constr

equiv_constr ->
   equiv_constr "#<=>" impl_constr
   impl_constr

impl_constr ->
   impl_constr "#=>" or_constr
   or_constr

or_constr ->
   or_constr "#\/" xor_constr
   xor_constr

xor_constr ->
   xor_constr "#^" and_constr
   and_constr

and_constr ->
   and_constr "#/\" not_constr
   not_constr

not_constr ->
   "#~" not_constr
   enclosed_goal

enclosed_goal ->
   "if" if_cond goal {"elseif" if_cond goal} ["else" goal] "end"
   "foreach" "(" iterator {"," (iterator | condition)} ")" goal "end"
   "while" "(" goal ")" ["do"] goal "end"
   "do" goal "while" "(" goal ")"
   expression {bin_rel_op expression}

iterator ->
   expression "in" expression
    
if_cond -> "(" goal ")" ["then"]       % "then" is optional
if_cond -> goal "then"                 % "then" is required if goal is 
                                       % not enclosed in parentheses

bin_rel_op ->
    "!="
    "!=="
    "#!=" 
    "#<" 
    "#<=" 
    "#=" 
    "#=<" 
    "#>" 
    "#>=" 
    "::"
    ":=" 
    "<"
    "<="
    "=" 
    "=.."
    "=:="
    "=<"
    "=="
    "=\="
    ">"
    ">="
    "@<" 
    "@<=" 
    "@=<" 
    "@>" 
    "@>=" 
    "in"
    "is"
    "notin"

expression -> 
    concat_expression 

concat_expression ->
    range_expression ["++" concat_expression]

range_expression ->
   or_expression [".." or_expression [".." or_expression]]
    
or_expression  ->
    xor_expression 
    or_expression "\/" xor_expression

xor_expression ->  
    and_expression 
    xor_expression "^" and_expression              % bitwise xor

and_expression ->
    shift_expression 
    and_expression  "/\" shift_expression

shift_expression ->
    additive_expression 
    shift_expression ( "<<" | ">>" ) additive_expression 

additive_expression ->
    multiplicative_expression
    additive_expression "+" multiplicative_expression
    additive_expression "-" multiplicative_expression

multiplicative_expression ->
    unary_expression
    multiplicative_expression "*" unary_expression
    multiplicative_expression "/" unary_expression
    multiplicative_expression "//" unary_expression
    multiplicative_expression "/>" unary_expression
    multiplicative_expression "/<" unary_expression
    multiplicative_expression "div" unary_expression
    multiplicative_expression "mod" unary_expression
    multiplicative_expression "rem" unary_expression
    
unary_expression ->
    power_expression
    "+" unary_expression
    "-" unary_expression
    "~" unary_expression                           % bitwise complement

power_expression ->
    primary_expression ["**" unary_expression]

primary_expression ->
    "(" goal ")"
    anonymous_function
    anonymous_predicate
    variable "[" argument {"," argument} "]"       % subscript notation
    variable "@" term ["@"]                        % as-pattern
    atom_or_call
    integer_literal
    real_literal
    string_literal
    variable_token
    higher_order_call
    list_expression
    array_expression
    function_call
    term_constructor
    atom_token "." higher_order_call               % module qualified higher-order call
    primary_expression "." atom_or_call            % dot-notation

atom_or_call ->
    atom_token ["(" [argument {"," argument}] ")"]  

higher_order_call ->
    variable "(" [argument {"," argument}] ")"     % higher order call variable

list_expression ->
    "[]"
    "[" argument list_expression_suffix "]"

list_expression_suffix ->
    ":" iterator {"," (iterator | condition)}      % list comprehension
    {"," argument} ["|" argument] 

array_expression ->
    "{}"
    "{" argument array_expression_suffix "}"    

array_expression_suffix ->
    ":" iterator {"," (iterator | condition)}      % array comprehension
    {"," argument} 

function_call ->
    [primary_expression "."] atom_token "(" [argument {"," argument}] ")"

/* Anonymous functions and predicates are closure-valued expressions.
   Their outer parentheses and bracketed parameter lists are required.
   An empty parameter list is written as "[]". */

anonymous_function ->
    "(" anonymous_head "->" expression ["," body] ")"

anonymous_predicate ->
    "(" anonymous_head "=>" body ")"

anonymous_head ->
     lambda_parameters

lambda_parameters ->
    "[]"
    "[" term {"," term} "]"

/* In an anonymous function, the first comma following the return expression
   separates that expression from the body. The body has the full goal syntax.
   Omitting the body is equivalent to using the body true.

   A parenthesized form beginning with lambda_parameters followed by "->",
   or "=>" is parsed as a lambda expression, rather than as an ordinary
   parenthesized goal. In particular, "([X] -> E)" is an anonymous function,
   not a Prolog-style if-then.

   Variables in parameter terms introduce bindings local to the lambda.
   Parameter terms follow the same matching rules as named head arguments.
   Other references
   to enclosing lexical bindings are captured; remaining variables are local
   to each invocation. Anonymous occurrences of _ are fresh independently.
   Nested lambdas obey the same lexical scope rules.

   Creating a lambda constructs a closure; it does not execute its body or
   evaluate its return expression. An anonymous function is invoked in a
   function context, and an anonymous predicate in a predicate context, using
   the existing higher_order_call syntax (or apply/call).

   An anonymous predicate has one rule, using =>. A function lambda
   follows ordinary function-rule behavior; its return expression is evaluated
   after its body. Calls within a lambda retain the defining module context.
   These scope and execution requirements are semantic, not grammar rules. */

term_constructor ->
    "$" goal ["$"]

/* A term has the same form as a goal except that it cannot contain assignments,
   loops or if statements. Note that subscript notations, range expressions, dot
   notations, list comprehensions, anonymous definitions, and higher-order function 
   calls are still treated as functions in term constructors */