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

Thierry Martinez's avatar
ode  
Thierry Martinez committed
8 9 10 11 12 13
kinetics(Reactants, Kinetics, Value) :-
  eval_kinetics(Reactants, Kinetics, ValueToSimplify),
  simplify(ValueToSimplify, Value).


eval_kinetics(Reactants, single_reactant, Value) :-
Thierry Martinez's avatar
Thierry Martinez committed
14
  !,
Thierry Martinez's avatar
Thierry Martinez committed
15
  (
Thierry Martinez's avatar
Thierry Martinez committed
16
    Reactants = [Value]
Thierry Martinez's avatar
Thierry Martinez committed
17
  ->
Thierry Martinez's avatar
Thierry Martinez committed
18
    true
19
  ;
Thierry Martinez's avatar
Thierry Martinez committed
20
    throw(error(not_single_reactant_reaction))
Thierry Martinez's avatar
Thierry Martinez committed
21 22
  ).

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

Thierry Martinez's avatar
ode  
Thierry Martinez committed
41
eval_kinetics(Reactants, FunctionApplication, Expression) :-
Thierry Martinez's avatar
Thierry Martinez committed
42
  function_apply(FunctionApplication, NewBody),
Thierry Martinez's avatar
Thierry Martinez committed
43
  !,
Thierry Martinez's avatar
ode  
Thierry Martinez committed
44
  eval_kinetics(Reactants, NewBody, Expression).
Thierry Martinez's avatar
Thierry Martinez committed
45

Thierry Martinez's avatar
ode  
Thierry Martinez committed
46 47
eval_kinetics(Reactants, Callable, Expression) :-
  term_morphism(kinetics:eval_kinetics(Reactants), Callable, Expression).
Thierry Martinez's avatar
Thierry Martinez committed
48 49


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

Thierry Martinez's avatar
Thierry Martinez committed
52 53 54 55
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).