Interacting in agda-mode with agda? - agda

It feels super awkward to interact with agda.
Consider the proof state:
_ = begin
5 ∸ 3
≡⟨⟩
4 ∸ 2 ≡⟨⟩
3 ∸ 1 ≡⟨⟩
2 ∸ 0 ≡⟨⟩ { 2 <cursor-goes-here> }0
When I type C-c C-l (type-check), it says
?0 : 2 ∸ 0 ≡ _y_131
_y_131 : ℕ [ at /home/bollu/work/plfa/src/plfa/part1/Naturals.lagda.md:586,5-10 ]
which doesn't seem like a great error? Nor does a refine (C-c C-r) give me a good error message: It only tells me:
cannot refine
How do I get adga to tell me:
You've finished the proof, except for a missing \qed
In general, what is the "preferred mode of interaction" when building proofs?

The overall issue
Your post starts by the following assumption:
It feels super awkward to interact with agda.
The reason that could explain your feeling is that you seem to assume that Agda can both infer a term and its type, in other words, both the property you wish to prove and a proof of it. Agda can often do one of these, but asking for both does not make much sense. As a comparison, imagine being on a bench in a park, when a complete strangers comes and sits next to you, saying nothing. You can see he would very much enjoy to ask you something, but, despite your efforts at making him speak, he remains silent. After a few minutes, the stranger yells at you that, despite him being thirsty, you did not bring the drink he was expected. In this metaphor, the stranger is you, and you are Agda. There is no way you could have known he was thirsty, and even less bring him his drink.
Concretely
You gave the following piece of code:
_ = begin
5 ∸ 3 ≡⟨⟩
4 ∸ 2 ≡⟨⟩
3 ∸ 1 ≡⟨⟩
2 ∸ 0 ≡⟨⟩ { 2 <cursor-goes-here> }0
This piece of code lacks a type signature which will allow Agda to help you more. Agda tells you so when you type check by providing you with the inferred type of the goal:
?0 : 2 ∸ 0 ≡ _y_131
_y_131 : ℕ [ at /home/bollu/work/plfa/src/plfa/part1/Naturals.lagda.md:586,5-10 ]
Here Agda says that your proof goal is that 2 ∸ 0 is equal to some unknown natural number y. This number being unknown there is very little chance Agda can help you go further in your proof effort because it does not even know what you wish to prove. As far as it knows, your goal could turn out to be 5 ∸ 3 ≡ 3 for wish there exists no proof term.
Getting back to our metaphor, you lacks the statement "I am thirsty". Should the stranger provide this piece of information, you could - possibly - react, which means Agda can try and help.
The solution
I'm assuming you wish to prove that the result of your subtraction is two, in which case the code is as follows:
test : 5 ∸ 3 ≡ 2
test = begin
5 ∸ 3 ≡⟨⟩
4 ∸ 2 ≡⟨⟩
3 ∸ 1 ≡⟨⟩
2 ∸ 0 ≡⟨⟩ {!!}
In this case, you can interact with Agda in various ways, which all lead to Agda providing you with a sound proof term:
You can call Agsy to solve the problem for you (CTRL-c CTRL-a), which leads to:
test : 5 ∸ 3 ≡ 2
test = begin
5 ∸ 3 ≡⟨⟩
4 ∸ 2 ≡⟨⟩
3 ∸ 1 ≡⟨⟩
2 ∸ 0 ≡⟨⟩ refl
You can try and refine the goal directly (CTRL-c CTRL-r), asking Agda if there exists any unique constructor which has the right type, which leads to the same:
test : 5 ∸ 3 ≡ 2
test = begin
5 ∸ 3 ≡⟨⟩
4 ∸ 2 ≡⟨⟩
3 ∸ 1 ≡⟨⟩
2 ∸ 0 ≡⟨⟩ refl
If you wish to wrap up your proof using \qed you can try and input _∎ into the hole after which refining (CTRL-c CTRL-r) gives:
test : 5 ∸ 3 ≡ 2
test = begin
5 ∸ 3 ≡⟨⟩
4 ∸ 2 ≡⟨⟩
3 ∸ 1 ≡⟨⟩
2 ∸ 0 ≡⟨⟩ {!!} ∎
Calling Agsy in the resulting goal naturally gives:
test : 5 ∸ 3 ≡ 2
test = begin
5 ∸ 3 ≡⟨⟩
4 ∸ 2 ≡⟨⟩
3 ∸ 1 ≡⟨⟩
2 ∸ 0 ≡⟨⟩ 2 ∎

Related

Design DFA accepting decimal strings divisible by 7

I'm a student studying DFAs looking for a DFA that could find if a decimal number is divisible by 7.
today I've solved divisibility problem for numbers 2,3,4,5,6,8,9 but I can't solve this problem for number 7. I've searched the web but I couldn't find any answer helping me or being understandable for me.
so now I'm here looking for help. thanks in advance.
The basic idea is that we will keep track of the current value, modulo seven, of the number we've seen so far. Each new digit takes the old number, multiplies by ten, and adds the new digit. Therefore, from the state corresponding to x (mod 7), adding digit d to the right means we go to the state corresponding to 10x + d (mod 7). This DFA has 70 states (the number of digits 0-9 times the number of remainders after division by seven 0-6).
q s q'
------------
q0 0 q0
q0 1 q1
q0 … …
q0 6 q6
q1 0 q3
q1 1 q4
q1 … …
q1 6 q2
…
q6 0 q4
q6 1 q5
q6 … …
q6 6 q3
Consider the processing of the number 36736:
(q0) --3--> (q3) --6--> (q1) --7--> (q3) --3--> (q5) --6--> (q0)
0 0*10+3 3*10+6 1*10+7 3*10+3 5*10+6
0+3 30+6 10+7 30+3 50+6
3 36 17 33 56
3 1 3 5 0
This number is divisible by seven because we end up in state q0, the state corresponding to zero modulo seven - meaning an even multiple of seven.
I think this will be helpful you can check this DFA by reminder(for ex. Binary 1001 = Decimal 9 and 9 mod 7 = 2 so our string 1001 should be end at q2.Check Image for DFA Design

Artificial Neural Network Toplogy

I am currently trying to revise for my final year exams and came across this question, I have looked everywhere in my lecture slides for any sort of help and cannot find any. Any help in providing insight in to how to solve this question would be appreciated (I am not just asking for the answer, I need to comprehend the topic). Furthermore, do I assume that all inputs are equal to 1? Do i include 7 inputs in the input layer? Im at a loss as to how to answer.
The question is as follows:
b) Determine, with justification, the simplest type and topology (i.e. number of neurons & layers) of artificial neural network that could learn the data set below.
Click here for picture of the dataset.
If I'm not mistaken, you have two inputs X1, X2, and one target output. For each input consisting, of two numbers X1, X2, the appropriate output ("target") is given.
As a first step, you could sketch the seven data points - just draw the 3 ones and 4 zeroes at the right places on on the square (X1, X2) ∈ [0, 1.05] × [0, 1]. Maybe you remember something similar from the lecture, possibly near a mention of "XOR".
The edit queue is full, so adding data from the linked image here
Pattern X1 X2 Target
1 0.01 -0.1 1
2 0.90 0.09 0
3 0.89 -0.05 0
4 1.05 0.95 1
5 -0.01 0.12 0
6 1.05 0.97 1
7 0.98 0.10 0
It looks like 1 possible solution is X1 >= 1.0 OR X2 <= -0.1
Alternatively, if you round each of X1 and X2, it becomes
Pattern X1 X2 Target
1 0 0 1
2 1 0 0
3 1 0 0
4 1 1 1
5 0 0 0
6 1 1 1
7 1 0 0
Then it IS XOR, and the solution is round(X1) XOR round(X2). In that case you can use 1 activation layer (like round, RELU, sigmoid, linear), 1 hidden layer of 2 neurons and 1 output layer of 1 neuron.
See this stackoverflow post for a detail of how to solve XOR with a neural net.

Octave Conditional Merging of matrices

I have searched for an Octave function that facilitates conditional merging of matrices but haven't one so far. My goal is to do this using vectors without looping. Here is an example of what I am trying to do.
A= [1 1
2 2
3 1
5 2];
B= [1 9
2 10];
I would like to get C as
C= [1 1 9
2 2 10
3 1 9
5 2 10];
Is there a function that takes A, B and the list of column(s) to join on and then produce C?
You can use the second output of ismember to find the occurrences of the second column of A in the first column of B and then use that to grab specific entries from the second column of B to construct C.
[~, inds] = ismember(A(:,2), B(:,1));
C = [A, B(inds,2)];
%// 1 1 9
%// 2 2 10
%// 3 1 9
%// 5 2 10

Quick way to determine the number of k-length paths from A to B in a dense complete graph

Given a complete dense graph (over 250.000 nodes) , what is the quickest way to determine the number of k-length paths from node A to B ?
I understand this is an old post, but I had the exact same question and could not find the answer.
I like to think of this problem as a "permutation without repetition", as the order of the nodes visited matters (permutation) and we aren't backtracking (no repetitions). The number of permutations without repetition is: n!/(n-r)!
For a complete graph with N nodes, there are N - 2 remaining nodes to choose from when creating a path between a given A and B. To create a path of length K, K-1 nodes must be chosen from the remaining nodes after A and B are excluded. Therefore, in this context, n = N - 2, and r = k - 1.
Plugging into the above formula yields:
(N-2)!/(N-K-1)!
Example: for N = 5, with nodes 0,1,2,3,4 the following paths are possible from 0 to 1:
0 1
0 2 1
0 2 3 1
0 2 3 4 1
0 2 4 1
0 2 4 3 1
0 3 1
0 3 2 1
0 3 2 4 1
0 3 4 1
0 3 4 2 1
0 4 1
0 4 2 1
0 4 2 3 1
0 4 3 1
0 4 3 2 1
This yields 1 path of length 1, 3 paths of length 2, 6 paths of length 3, and 6 paths of length 4.
This appears to work for any N>=2 and K<=N-1.
You can use basically dynamic programming: For each node Y and path length k, you can compute the number of paths from A to Y of length k if you know the number of paths from A to X of path length k-1 for all nodes X. Total complexity is O(KV), where K is the total path length you are trying to compute for and V is the number of vertices.

KNN Decision Boundary

I have two classes:
x={-3,-2,1} //represented by *
y={0,5,6,7} //represented by x
If k=3, how do you determine the decision boundary?
* * x * x x x
| | | | | | | | | | | | |
-5 -4 -3 -2 -1 0 1 2 3 4 5 6 7
Supposedly the correct answer is 1.5, between 1 and 2. How does that work?
The KNN algorithm classifies new observations by looking at the K nearest neighbors, looking at their labels, and assigning the majority (most popular) label to the new observation.
For KNN with K=3, anything < 1.5 will be classified as * and anything > 1.5 will be classified as x.
You can see this by trying out a few examples. Suppose you need to classify a value of 1. The three nearest neighbors are the * at 1, the x at 0, and the * at -2. Since there are two *'s and one x, 1 will be classified as *.
Now suppose you want to classify 2. Here, the three nearest neighbors are the x at 0, the * at 1, and the x at 5. So 2 would get classified as x.
The KNN process implicitly defines a decision boundary. The best way to determine it that I'm aware of is to try a bunch of examples and look for the transition boundary where observation classifications change from one class to another class. In your example this would look like this:
-5 -> *
-4 -> *
-3 -> *
-2 -> *
-1 -> *
0 -> *
1 -> *
2 -> x
3 -> x
4 -> x
You can see this in your example - the decision boundary is somewhere between 1 and 2. Hence the 1.5 answer.

Resources