I found a program I made on my calculator many years ago, and I am having trouble understanding what the line
Z-1(Z=0 → Z
does in it. I tried just changing it to
Z-1 → Z
but that ended up breaking it. Here is an excerpt, any information would be helpful!
ClrHome
Prompt A,B,C
A*C → D
B-1 → Z
Z-1(Z=0 → Z
D/Z → F
F+Z → G
This is pretty common for complex variable trickery without if statements and such. This is pretty much saying if Z=0, subtract 1 from Z. If Z isn't 0, Z=0 becomes 0, and Z-1(0 (order of operations is multiply first) is just Z-0, which will leave the variable unchanged. In normal code it would be like:
If not(Z
Z-1->Z
Just another thing to point out, If not(Z[newline] and (Z=0 have the same amount of tokens, which means you should do (not(Z instead to save space. Never in ti basic should you really have a =0 because that is 2 tokens for something not( can do in one.
Related
I’m trying to practice beta reduction but I’m stuck on how to reduce this problem:
(λx((λy.x)(λx.x))x)y
The outermost λx will obviously be substituted with y, but should I still proceed with reducing ((λy.x)(λx.x))? What am I missing here?
I’m trying to practice beta reduction but I’m stuck on how to reduce this problem:
(λx((λy.x)(λx.x))x)y
To perform the first β-reduction you have to look at the variables x bound by the outer λx, which are,
(λx((λy.x)(λx.x))x)y
^ ^
Note that in the inner λx.x shadows the outer lambda since it uses the same variable name. So the bound x here is bound to this inner lambda, not the outer one.
Another problem is that if you substitute y for x in λy.x then y is captured by the outer lambda. That cannot happen, so you need to perform an α-conversion (renaming) on λy.xto avoid the capture.
So first the α-conversion, the the β-reduction
(λx((λy.x)(λx.x))x)y
→ (λx((λz.x)(λx.x))x)y
→ ((λz.y)(λx.x))y
If you want to continue then λz.y returns y for any input,
((λz.y)(λx.x))y
→ yy
Note the λ-calculus doesn't set a reduction strategy, it only establishes an equivalence relation between terms. So you can pick another reduction order. For instance you could β-reduce the inner β-redex (λy.x)(λx.x) first.
(λx((λy.x)(λx.x))x)y
→ (λx(xx)y
→ yy
This would have avoided the α-conversion.
I have a Relation f defined as f: A -> B × C. I would like to write a firsr-order formula to constrain this relation to be a bijective function from A to B × C?
To be more precise, I would like the first order counter part of the following formula (actually conjunction of the three):
∀a: A, ∃! bc : B × C, f(a)=bc -- f is function
∀a1,a2: A, f(a1)=f(a2) → a1=a2 -- f is injective
∀(b, c) : B × C, ∃ a : A, f(a)=bc -- f is surjective
As you see the above formulae are in Higher Order Logic as I quantified over the relations. What is the first-order logic equivalent of these formulae if it is ever possible?
PS:
This is more general (math) question, rather than being more specific to any theorem prover, but for getting help from these communities --as I think there are mature understanding of mathematics in these communities-- I put the theorem provers tag on this question.
(Update: Someone's unhappy with my answer, and SO gets me fired up in general, so I say what I want here, and will probably delete it later, I suppose.
I understand that SO is not a place for debates and soapboxes. On the other hand, the OP, qartal, whom I assume is the unhappy one, wants to apply the answer from math.stackexchange.com, where ZFC sets dominates, to a question here which is tagged, at this moment, with isabelle and logic.
First, notation is important, and sloppy notation can result in a question that's ambiguous to the point of being meaningless.
Second, having a B.S. in math, I have full appreciation for the logic of ZFC sets, so I have full appreciation for math.stackexchange.com.
I make the argument here that the answer given on math.stackexchange.com, linked to below, is wrong in the context of Isabelle/HOL. (First hmmm, me making claims under ill-defined circumstances can be annoying to people.)
If I'm wrong, and someone teaches me something, the situation here will be redeemed.
The answerer says this:
First of all in logic B x C is just another set.
There's not just one logic. My immediate reaction when I see the symbol x is to think of a type, not a set. Consider this, which kind of looks like your f: A -> BxC:
definition foo :: "nat => int × real" where "foo x = (x,x)"
I guess I should be prolific in going back and forth between sets and types, and reading minds, but I did learn something by entering this term:
term "B × C" (* shows it's of type "('a × 'b) set" *)
Feeling paranoid, I did this to see if had fallen into a major gotcha:
term "f : A -> B × C"
It gives a syntax error. Here I am, getting all pedantic, and our discussion is ill-defined because the notation is ill-defined.
The crux: the formula in the other answer is not first-order in this context
(Another hmmm, after writing what I say below, I'm full circle. Saying things about stuff when the context of the stuff is ill-defined.)
Context is everything. The context of the other site is generally ZFC sets. Here, it's HOL. That answerer says to assume these for his formula, wich I give below:
Ax is true iff x∈A
Bx is true iff x∈B×C
Rxy is true iff f(x)=y
Syntax. No one has defined it here, but the tag here is isabelle, so I take it to mean that I can substitute the left-hand side of the iff for the right-hand side.
Also, the expression x ∈ A is what would be in the formula in a typical set theory textbook, not Rxy. Therefore, for the answerer's formula to have meaning, I can rightfully insert f(x) = y into it.
This then is why I did a lot of hedging in my first answer. The variable f cannot be in the formula. If it's in the formula, then it's a free variable which is implicitly quantified. Here's the formula in Isar syntax:
term "∀x. (Ax --> (∃y. By ∧ Rxy ∧ (∀z. (Bz ∧ Rxz) --> y = z)))"
Here it is with the substitutions:
∀x. (x∈A --> (∃y. y∈B×C ∧ f(x)=y ∧ (∀z. (z∈B×C ∧ f(x)=z) --> y = z)))
In HOL, f(x) = f x, and so f is implicitly, universally quantified. If this is the case, then it's not first-order.
Really, I should dig deep to recall what I was taught, that f(x)=y means:
(x,f(x)) = (x,y) which means we have to have (x,y)∈(A, B×C)
which finally gets me:
∀x. (x∈A -->
(∃y. y∈B×C ∧ (x,y)∈(A,B×C) ∧ (∀z. (z∈B×C ∧ (x,z)∈(A,B×C)) --> y = z)))
Finally, I guess it turns out that in the context of math.stackexchange.com, it's 100% on.
Am I the only one who feels compulsive about questioning what this means in the context of Isabelle/HOL? I don't accept that everything here is defined well enough to show that it's first order.
Really, qartal, your notation should be specific to a particular logic.
First answer
With Isabelle, I answer the question based on my interpretation of your
f: A -> B x C, which I take as a ZFC set, in particular a subset of the
Cartesian product A x (B x C)
You're sort of mixing notation from the two logics, that of ZFC
sets and that of HOL. Consequently, I might be off on what I think you're
asking.
You don't define your relation, so I keep things simple.
I define a simple ZFC function, and prove the first
part of your first condition, that f is a function. The second part would be
proving uniqueness. It can be seen that f satisfies that, so once a
formula for uniqueness is stated correctly, auto might easily prove it.
Please notice that the
theorem is a first-order formula. The characters ! and ? are ASCII
equivalents for \<forall> and \<exists>.
(Clarifications must abound when
working with HOL. It's first-order logic if the variables are atomic. In this
case, the type of variables are numeral. The basic concept is there. That
I'm wrong in some detail is highly likely.)
definition "A = {1,2}"
definition "B = A"
definition "C = A"
definition "f = {(1,(1,1)), (2,(1,1))}"
theorem
"!a. a \<in> A --> (? z. z \<in> (B × C) & (a,z) \<in> f)"
by(auto simp add: A_def B_def C_def f_def)
(To completely give you an example of what you asked for, I would have to redefine my function so its bijective. Little examples can take a ton of work.)
That's the basic idea, and the rest of proving that f is a function will
follow that basic pattern.
If there's a problem, it's that your f is a ZFC set function/relation, and
the logical infrastructure of Isabelle/HOL is set up for functions as a type.
Functions as ordered pairs, ZFC style, can be formalized in Isabelle/HOL, but
it hasn't been done in a reasonably complete way.
Generalizing it all is where the work would be. For a particular relation, as
I defined above, I can limit myself to first-order formulas, if I ignore that
the foundation, Isabelle/HOL, is, of course, higher-order logic.
I'm trying to write a program to solve the towers of hanoi problem in Prolog. None of the posts here helped me so I decided to ask for myself. I have written the following code. It works well for 2 disks but goes into an infinite loop for 3.
hanoi(1,A,B,_,[(A,B)]).
hanoi(X,A,B,C,Y):-
hanoi(X2,A,C,B,Y1),
hanoi(1,A,B,_,[Y2]),
hanoi(X2,C,B,A,Y3),
append(Y1,[Y2|Y3],Y),
X2 is X-1.
It is called in the following way:
?- hanoi(3, a, b, c, Y).
a,b,c are the pegs. 3 is the number of disks and X is where we want the result.
I need to get the result in Y. I'm trying to recursively find the moves for X-1 discs from peg 1 to 3 using 2, 1 disc from peg 1 to 2, X-1 discs from peg 3 to 2 and append them. I can't understand what I'm doing wrong. Any help or guidance would be appreciated! Thanks!
The obvious problem -
When you have a conjunction, like:
a, b, c
You have two readings of this, logical and procedural. The logical reading would be:
"The conjunction is true if a is true, and b is true, and c is true."
The procedural reading is:
"The conjunction will succeed if a is evaluated and succeeds, then b is evaluated and it also succeeds, and then c is evaluated and it also succeeds." (or, to put it in other words, do a depth-first search of the solution space)
If you are careful enough, the procedural reading is not necessary to argue about your code. However, this is only the case when all subgoals in the conjunction have full overlap of the logical and the procedural reading.
The offender here is is/2. It does not have a purely logical meaning. It evaluates the right-hand operand (the arithmetic expression) and unifies the result with the left-hand (usually an unbound variable).
Here, you have a conjunction (in the body of the second clause) that says, in effect, "evaluate a(X), then, if it succeeds, find the value of X". The obvious problem is that a(X) requires the value of X to be evaluated in such a way that it terminates.
So, move the is before all subgoals that use its result, or look into using constraints.
So I have an assignment to work on and there is one thing I'm confused about. Here is a similar problem to what I'm trying to accomplish. Say I have these rules:
size(3).
size(5).
size(7).
size(1).
size(2).
size(9).
And I want to find the minimum of size by taking the value 3 and comparing it to 5, storing 3 and comparing it to 7, then comparing it to 1, storing 1 and comparing it to 2...returning a value of 1.
The only way I can see of doing that without a high order procedure would be some form of backtracking while there are still size(X) values and each time altering the size variable. However, I don't see how you can both backtrack and save the new minimum value. I was wondering if anyone could help put me on the right track.
your assignment is intended to make you thinking about the peculiar control flow of Prolog, and indeed you are now to the point. I've coded the solution and placed some explanatory comment: fill in the ellipsis to complete the code
find_min_size(MinSize) :-
size(Hypothesis),
!, % without this cut the minimum is obtained more times...
find_min_size(Hypothesis, MinSize).
find_min_size(SoFar, MinSize) :-
% when I should keep searching?
...
% the exhaustive search come to end!
find_min_size(MinSize, MinSize).
I don't think in Prolog is possible to get O(N) performance, without 'higher order procedures' ( do you mean findall etc..? ). The code above will run in O(N^2). Indexing can't play a role, because size/1 will restart with unbound variable...
An interesting O(N) alternative is using a failure driven loop and the tech that #false explained so well when introducing call_nth, (also, see here a follow up). All of these are in the 'impure' realm of Prolog, though...
edit here the failure driven loop
find_min_size(MinSize) :-
State = (_, _),
( size(V),
arg(1, State, C),
( ( var(C) ; V < C ) -> U = V ; U = C ),
nb_setarg(1, State, U),
fail
; arg(1, State, MinSize)
).
What would be the approach to a kind of problem that sounds like this:
A says B lies
B says C lies
D says B lies
C says B lies
E says A and D lie
How many lie and how many tell the truth?
I am not looking for the answer to the problem above, but the approach to this kind of problem. Thanks a lot.
A -> !B
B -> !C
D -> !B
C -> !B
E -> !A & !D
Reminder:
X -> Y <=> !X | Y
Transform the 5 equations into logical propositions, and you will find answers.
To solve equations of the form
X1 = NOT X 3
X5 = NOT X 2
etc
Form a graph with nodes as Xi and connecting Xi and X j iff the equation Xi = NOT X j appears.
Now try to 2-colour the graph using Breadth First Search.
Assuming you're looking to solve this with a program... it's actually pretty easy to brute force, if you've got a reasonably small input set. For example, in this case you've basically got 5 Boolean variables - whether each person is a truth-teller or not.
Encode the statements as tests, and then run through every possible combination to see which ones are valid.
This is obviously a "dumb" solution and will fail for large input sets, but it's likely to be rather easier to code than a full "reasoning" engine. Often I find that you can get away with doing a lot less work by taking into account what size of problem you're actually going to encounter :)
Use a logic programming language such as Prolog. They are specifically designed to solve such problems.
Other alternatives include functional-logic languages and model checkers.