**Examples**
CFLP is delivered as a collection of *Mathematica* packages, which add solving power to a normal *Mathematica* session. If CFLP is properly installed, a *Mathematica* user can enable it by loading the package `TSolve.m` .
In this section we illustrate the functionality of CFLP with a few examples.
**A Polynomial Approximation Problem**
Consider the problem of finding the relationship between the coefficients of a univariate polynomial of degree 3 and a univariate polynomial of degree 4 related by the constraint that they have the same values for .
In CFLP, the polynomial *f* of degree 3 can be specified by the -term where *a, b, c, d* are constants, and the polynomial *g* of degree 4 can be specified by the -term where *m, n, p, q, r* are variables whose values have to be computed from the conditions
which can be specified in the compact form
where `map[` *f, list*`]` applies *f* to each element of *list*. In CFLP, `map` can be specified via the following functional logic program.
The underlined symbols denote the free variables in the rewrite rules, and `[H|Tl]` denotes a list with head *H* and tail *Tl*.
To solve equation (1), we call
The equational symbol denotes *strict* equality, that is, the fact that the *values* of the two sides of the equation coincide. We identify the value of an expression with the result of rewriting it to an expression
without defined symbols.
The syntax call of `TSolve` is similar to the syntax call of `Solve` . The first three arguments specify the goal to be solved, the main variables of the goal, and the eliminatable variables
of the goal, respectively. In addition, the user of CFLP can control the behavior of the computation through various `TSolve` options. In this example, the following options are used.
The constant coefficients of *f* are given as data constructors, and the unknown coefficients of *g* are given as main variables.
A `TSolve` call may yield an infinite number of solutions. In this case it cannot produce an output with the list of all solutions but
it prints them out incrementally, as soon as they are computed. `TSolve` calls can be interrupted via a palette which is provided for the user convenience.
In this example, CFLP returns the unique solution
which is printed out as soon as it is computed, and collected in the output of the `TSolve` call.
Note the use of higher-order variables and -abstractions in the specification of the goal and functional logic program. CFLP is able to handle equations involving operators
defined outside the functional logic program. Furthermore, the computed answer is parametric, since *r* is a variable.
**Electrical Circuits**
This example illustrates the expressive power of the functional logic programming style extended with higher-order constructs
(function variables and -abstractions) and capabilities to solve differential equations, linear equations, and systems of polynomial equations. We
consider the problem of modeling the behavior in time of electrical circuits built from serial and parallel connections of
elementary components such as resistors, inductors, and capacitors.
First we introduce a new type constructor `ElComp` with associated data types `res` , `ind` , `cap` , `serial` , and `parallel` to specify the characteristics of the electrical circuits. Thus, a resistor with resistance is specified as `res[` 2`]` , a serial connection of two resistors R1 and R2 is specified as `serial[{res[` R1`], res[` R2`]}]` , and a parallel connection of a capacitor C and an inductor L is specified as `parallel[{cap[` C`], ind[` L`]}]` . The keyword `TyList` is the CFLP-type constructor for lists.
Next, we define a function `spec` which describes the behavior in time `t` of an electrical component as a function of current `i[t]` and voltage `v[t]` in the circuit. The CFLP definition of `spec` is recursive, where the base case describes the behavior in time of the elementary circuits (resistor, inductor, capacitor),
and the inductive case describes the behavior in time of serial and parallel connections of electrical components.
The CFLP program is given below. We do not explain the underlying electronic laws since it should be easy to read them off
from the program.
Consider the problem of finding the behavior in time of the current in an RLC circuit, under the restriction that the voltage
is constant in time and the current was initially set to 0.
In this case, the goal which we want to solve (in variable `i` ) is
`R0` , `L0` , `C0` , and `V` are data constructors which denote arbitrary but fixed characteristic values for the resistor, inductor, capacitor, and voltage
of the circuit. Now we can ask CFLP to solve the problem.
The system computes the parametric solution
which is represented in *Mathematica* by
Note the usage of type annotations in the goal, list of variables, and options of the `TSolve` call. CFLP has integrated a polymorphic type checker to verify the type consistency of the program and goal provided by the
user.
**A Problem Involving Solver Cooperation**
Consider the following program
in complex variables `X` , `Y` , and the goal
with indeterminates `X` , `H` , `Y` , `Z` . In this example, the operator denotes logical disjunction, and can be used in goals and conditional parts of rewrite rules to express alternative solutions.
Solving this goal requires a cooperation among solvers for linear, polynomial, and differential equations over the domain
of complex numbers. To solve `G` , we call
In this case, the solutions computed by CFLP are parametric, and the system has generated the auxiliary variables , , , , , to express them. It is not hard to see that these are all the solutions of `G` .
Copyright © 2001 Wolfram Media, Inc. All rights reserved.[Article Index][Prev Page][Next Page] |