functions.pl 2.35 KB
Newer Older
Thierry Martinez's avatar
Thierry Martinez committed
1 2 3
:- module(
  functions,
  [
4
    % Commands
Thierry Martinez's avatar
Thierry Martinez committed
5 6 7 8
    'function'/1,
    show_function/1,
    list_functions/0,
    delete_function/1,
9
    % Public API
Thierry Martinez's avatar
Thierry Martinez committed
10
    function_apply/2,
11
    list_model_functions/0,
Thierry Martinez's avatar
Thierry Martinez committed
12 13 14 15
    op(1010, fx, function)
  ]
).

Thierry Martinez's avatar
Thierry Martinez committed
16 17 18 19

:- devdoc('\\section{Commands}').


Thierry Martinez's avatar
Thierry Martinez committed
20 21 22 23 24 25 26 27
function(FunctionList) :-
  biocham_command(*),
  type(FunctionList, '*'(function_prototype = term)),
  doc('sets the value of functions.'),
  \+ (
    member(Function = Value, FunctionList),
    \+ (
      Function =.. [Functor | _Arguments],
Thierry Martinez's avatar
Thierry Martinez committed
28
      check_identifier_kind(Functor, function),
Thierry Martinez's avatar
Thierry Martinez committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
      catch(
        (
          find_item([kind: function, key: Functor, id: Id]),
          replace_item(Id, function, Functor, function(Function = Value))
        ),
        error(unknown_item),
        add_item([
          kind: function, key: Functor, item: function(Function = Value)
        ])
      )
    )
  ).


show_function(Functor) :-
  biocham_command,
  type(Functor, name),
  doc('shows the expression associated to the given function.'),
  find_item([kind: function, key: Functor, item: function(_ = Expression)]),
  write(Expression),
  nl.


list_functions :-
  biocham_command,
  doc('lists all known functions.'),
  list_items([kind: function]).


delete_function(FunctorSet) :-
  biocham_command,
  type(FunctorSet, {name}),
  doc('deletes some functions.'),
  \+ (
    member(Functor, FunctorSet),
    \+ (
      delete_item([kind: function, key: Functor])
    )
  ).
Thierry Martinez's avatar
Thierry Martinez committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88


:- devdoc('\\section{Public API}').


function_apply(FunctionApplication, NewBody) :-
  callable(FunctionApplication),
  functor(FunctionApplication, Functor, Arity),
  item([kind: function, key: Functor, item: (function(Head = Body))]),
  !,
  functor(Head, Functor, ApplicationArity),
  (
    Arity = ApplicationArity
  ->
    true
  ;
    throw(error(arity_mismatch(Functor, Arity, ApplicationArity)))
  ),
  Head =.. [Functor | Parameters],
  FunctionApplication =.. [Functor | Arguments],
  substitute(Parameters, Arguments, Body, NewBody).
89 90 91 92 93 94 95 96 97 98 99


list_model_functions :-
  devdoc('
    lists all the functions in a loadable syntax
    (auxiliary predicate of list_model).
  '),
  (
    item([no_inheritance, kind: function])
  ->
    write('function(\n'),
Thierry Martinez's avatar
Thierry Martinez committed
100
    write_successes(
101
      item([no_inheritance, kind: function, item: function(Head = Body)]),
Thierry Martinez's avatar
Thierry Martinez committed
102 103
      write(',\n'),
      format('  ~w = ~w', [Head, Body])
104 105 106 107 108
    ),
    write('\n).\n')
  ;
    true
  ).