How can I solve equations with F#? - f#

How can I solve equations with F#?
For example (just a loose syntax I made up):
define a, b where (a + b = 5) and (a - b = 1)
define c where (c = c / 2)
print a, b, c
I want it to output 3, 2 and 0.
Also, can it do things like define d where d = c + a using solutions from previously defined equations?

Related

get the expression out of solve results

Cosider the simple solution:
sol: solve(b * x - a, x);
a
[x = -]
b
how can I get the expression part sol: a / b out of the above result?
solution was offered to me here.
Thanks to Johann Weilharter I found one way to extract the expression:
sol: ev(x, solve(b * x - a, x)[1]);
Of course, if there is more than one solution, you need to change 1 to the specific instance.
Alternatively, as pointed out in the comments of the question, one can also use
sol: rhs(first(solve(b * x - a, x)));
oneliner to do the job.
What you need is a symbolic evaluation library. If you are considering a python implementation, you can use SymPy.
import sympy as sym
x = sym.Symbol('x')
b = sym.Symbol('b')
a = sym.Symbol('a')
sol = sym.solve((b * x - a), x)
print(sol)
------
[a/b]

How to output equation without evaluation, but with variables replaced to their values?

I need to make tons of simple computations and present each step in my report with predefined manner:
(for ex i got B=2, C=3):
A=B+12-6/C^2; A=2+12-6/3^2=13.333;
I can get 1st block and answer like this:
B:2$ C:3$
A:'(B+12-6/C^2)$
print("A=",A,"; ","A= ??? =",ev(A, numer) );
and get:
6
A= (- --) + B + 12 ; A= ??? = 13.33333333333333
2
C
What i need instead of '???' to get desired output?
Maxima distinguishes two parts of figuring out a result: evaluation and simplification. Evaluation = substituting one thing (the value) for another thing (a variable or a function). Simplification = applying mathematical identities to get a "simpler", equivalent result.
In your problem, it appears you want to postpone simplification. You can say simp: false to do that. Here's one possible approach. I'll disable simplification, substitute values into the expression, print the substituted expression, and then re-enable simplification to get the final result.
(%i2) expr: A=B+12-6/C^2;
6
(%o2) A = (- --) + B + 12
2
C
(%i3) simp: false $
(%i4) subst ([B = 2, C = 3], expr);
- 2
(%o4) A = 12 + 2 + (- 6) 3
(%i5) simp: true $
(%i6) %o4;
40
(%o6) A = --
3
Note that many operations in Maxima happen by simplification (e.g. adding numbers together), so in general, Maxima will act noticeably different when simp is false. But in this case that's what you want.
EDIT: OP points out that the result after substitution is displayed in a somewhat different from. The reason for this has to do with some obscure implementation details of Maxima. Be that as it may, it's possible to work around that behavior by using the Lisp substitution function SUBST (referenced in Maxima as ?subst) instead of Maxima subst. SUBST is a little different than Maxima subst; the syntax is ?subst(new_thing, old_thing, some_expression). After substituting via SUBST, it's necessary to resimplify explicitly; one way to do that is to say expand(..., 0, 0) (which doesn't expand anything, the only effect is to resimplify).
(%i2) expr: A=B+12-6/C^2;
6
(%o2) A = (- --) + B + 12
2
C
(%i3) simp: false $
(%i4) ?subst (3, C, ?subst (2, B, expr));
6
(%o4) A = (- --) + 2 + 12
2
3
(%i5) simp: true $
(%i6) expand (%o4, 0, 0);
40
(%o6) A = --
3
Since SUBST is has a different effect on the internal representation, it is possible you could create an invalid expression, for some choices of new_thing, old_thing, and some_expression. I won't try to sort that out here.

Purely Functional Programming

So, I'm an experienced OOP programmer (primarily C++), just now starting to dip my toes in with functional programming. From my understanding, in a purely functional paradigm, functions shouldn't have conditionals and should be broken down as much as possible using currying. Could someone provide me the "pure" functional version of the following example? Preferably using every strict technique that would be part of the functional paradigm:
let rec greatestCommonFactor a b =
if a = 0 then b
elif a < b then greatestCommonFactor a (b - a)
else greatestCommonFactor (a - b) b
The example function that you have supplied is already purely functional. When we talk about a function purity, what we are actually talking about is the property of functions being referentially transparent.
An expression is referentially transparent if it can be replaced with its value without altering the effect of the program. To give a simple example, imagine the function:
let add2 x = x + 2
Now, anywhere that the value add2 2 appears in our program, we can substitute the value 4 without altering the behaviour of the program.
Imagine now that we add some additional behaviour into the function that prints to the console:
let add2Print x =
printfn "%d" x
x + 2
Although the result of the function is the same as before, we can no longer perform value substitution with the value 4 without changing the behaviour of our program because our function has the additional side-effect of printing to the console.
This function is no longer referentially transparent and therefore not a pure function.
let rec greatestCommonFactor a b =
if a = 0 then b
elif a < b then greatestCommonFactor a (b - a)
else greatestCommonFactor (a - b) b
Looking at this function that you have supplied, no side-effects are involved in its execution. We will always get the same output value for given inputs a and b, therefore this is already a pure function.
To be clear, there is absolutely no issue with functions containing conditionals in functional programming. Often, however, we make use of pattern matching rather than if/elif/else expressions but in the example you have described, this is purely stylistic. An alternative expression of your function using pattern matching would be:
let rec greatestCommonFactor a b =
match a with
|0 -> b
|a' when a' < b -> greatestCommonFactor a' (b - a')
|a' -> greatestCommonFactor (a' - b) b

How to prove commutative property for rational number in Agda?

I am trying to prove commutative property for agda. I tried to explore the standard library but there is lot of complex thing which i could not understand.
I tried in this way --
comm : (a b : Q) -> (a + b) === (b + a)
the problem here is + which is not defined over Q in library. Can't we proof this without defining + over Q.
Please guide me.
You cannot prove this without first defining +.
If you get confused exploring the standard library I suggest you try to prove something easier first, in order to become more acquainted with Agda, before tackling this.
Of course you can't prove commutativity of an undefined function _+_; as a stupid counter-example, would you expect to be able to prove (a - b) == (b - a)? If not, why not? _-_ is just as much of an undefined function as _+_ at this point; it just has a different name...
Note that you can define addition for ℚ using elementary school math:
n ÷ p + m ÷ q = (n * q + m * p) ÷ (p * q)
and simplifying it by dividing both n * q + m * p and p * q with their GCD. I have already explained the details of this last step in this answer.

ANTLR rewrite rules in grammar file

I have a rule that looks like this:
a : (b | c) d;
b : 'B';
c : 'C';
d : 'D';
With this grammar ANTLR builds a flat parse tree. How can I rewrite the first rule (and leave the other two unchanged) so that whatever is matched is being returned under a root node called A?
If the first production rule was like this:
a : b d;
then it could have been rewritten as
a : b d -> ^(A b d)
and it would have solved my problem. However the first grammar rule yields more than one possibility for the resulting parse tree ^(A b d) or ^(A c d).
How do I express this when rewriting the rule?
You can use the ? operator in the rewrite as follows.
a : (b | c) d -> ^(A b? c? d);

Resources