1:- module(plammar_operators, [
    2    merge_operators/3,
    3    normalise_operators/2
    4  ]).    5
    6:- use_module(library(lists), [member/2, append/3]).    7:- use_module(library(apply), [maplist/3]).    8
    9:- use_module(util).   10
   11merge_operators(Old, [], Old).
   12merge_operators(Old, [OpN|Ops], Merged) :-
   13  OpN = op(PrecN, Spec, Name),
   14  OpO = op(PrecO, Spec, Name),
   15  member(OpO, Old),
   16  (
   17    PrecN = PrecO,
   18    merge_operators(Old, Ops, Merged)
   19  ;
   20    !,
   21    warning('Conflicting operators: ~p, ~p', [OpN, OpO]),
   22    false
   23  ), !.
   24merge_operators(Old, [Op|Ops], Merged) :-
   25  merge_operators([Op|Old], Ops, Merged).
   26
   27normalise_operators([], []).
   28normalise_operators([Op0|Ops0], Ops) :-
   29  normalise_operator(Op0, List),
   30  normalise_operators(Ops0, Ops1),
   31  append(List, Ops1, Ops).
   32
   33normalise_operator(Op, Res) :-
   34  Op = op(Prec, Spec, A),
   35  ( is_list(A) ->
   36    maplist(build_op(Prec, Spec), A, Res)
   37  ; otherwise ->
   38    Res = [Op]
   39  ).
   40
   41build_op(Prec, Spec, A, op(Prec, Spec, A))