kinetics.pl 1023 Bytes
Newer Older
Thierry Martinez's avatar
Thierry Martinez committed
1 2 3 4 5 6 7
:- module(
  kinetics,
  [
    kinetics/3
  ]
).

Thierry Martinez's avatar
Thierry Martinez committed
8 9
kinetics(Reactants, single_reactant, Value) :-
  !,
Thierry Martinez's avatar
Thierry Martinez committed
10
  (
Thierry Martinez's avatar
Thierry Martinez committed
11
    Reactants = [Value]
Thierry Martinez's avatar
Thierry Martinez committed
12
  ->
Thierry Martinez's avatar
Thierry Martinez committed
13
    true
14
  ;
Thierry Martinez's avatar
Thierry Martinez committed
15
    throw(error(not_single_reactant_reaction))
Thierry Martinez's avatar
Thierry Martinez committed
16 17
  ).

Thierry Martinez's avatar
Thierry Martinez committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
kinetics(Reactants, product(Pattern in List, Expression), Value) :-
  !,
  (
    Pattern = S * O
  ->
    true
  ;
    throw(error(invalid_pattern))
  ),
  (
    List = [reactants]
  ->
    true
  ;
    throw(error(invalid_list))
  ),
  make_product(Reactants, S, O, Expression, Value).

kinetics(Reactants, FunctionApplication, Expression) :-
Thierry Martinez's avatar
Thierry Martinez committed
37
  function_apply(FunctionApplication, NewBody),
Thierry Martinez's avatar
Thierry Martinez committed
38 39
  !,
  kinetics(Reactants, NewBody, Expression).
Thierry Martinez's avatar
Thierry Martinez committed
40

Thierry Martinez's avatar
Thierry Martinez committed
41 42
kinetics(Reactants, Callable, Expression) :-
  term_morphism(kinetics(Reactants), Callable, Expression).
Thierry Martinez's avatar
Thierry Martinez committed
43 44


Thierry Martinez's avatar
Thierry Martinez committed
45
make_product([], _S, _O, _Expression, 1).
Thierry Martinez's avatar
Thierry Martinez committed
46

Thierry Martinez's avatar
Thierry Martinez committed
47 48 49 50
make_product([RS * RO | Tail], S, O, Expression, Product) :-
  substitute([S, O], [RS, RO], Expression, SubstitutedExpression),
  Product = SubstitutedExpression * TailProduct,
  make_product(Tail, S, O, Expression, TailProduct).