How to deconstruct hypothesis in Coq Proof Assistant? - logic

I'm trying to proof the theorem of Regular Languages in CoqIDE.
I need to deconstruct hypothesis H1 : s1 \in "a" || "b" into H2 : s1 \in "a" without creating additional sub-goals.
I used inversion H2 tactic, but it created additional sub-goal. So, is it possible to deconstruct hypothesis without creating additional sub-goals?
3 subgoals
w : word
H : w \in ("a" || "b");; "c"
s1, s2, s3 : list ascii
H1 : s1 \in "a" || "b"
______________________________________(1/3)
w = ["a"; "c"]

Related

ZDD with Quantification in Prolog

What would be the ZDD approach in Prolog that also provides quantifiers.
Notation for existential quantifier would be as follows:
X^F
where X is the bound variable and F is the formula. It corresponds
to the following formula:
F[X/0] v F[X/1]
How would one go about and write a Prolog routine that takes a ZDD
for F and a variable X and produces the ZDD for X^F ?
Daniel Pehoushek posted some interesting C-code, which I translated to Prolog. Its not directly working with ZDD, but rather with sets of sets of variables, which is also explained here. But I guess the algorithm can be translated from sets of sets of variables to ZDD.
It only needs two new operations, the rest works with the operations from library(lists). One operation is split/4 which gives the left and right branch for a tree root. And the other operation is join/4 which does the inverse. The main routine is bob/3:
bob(_, [], []) :- !.
bob([], A, A).
bob([V|W], A, T) :-
split(A, V, L, R),
intersection(L, R, H),
bob(W, H, P),
union(L, R, J),
bob(W, J, Q),
join(V, P, Q, T).
What does bob do? It transforms a formula P into a formula Q. The original formula P has propositional variables x1,..,xn. The resulting formulas has propositional variables x1',..,xn' which act as switches, where xi'=0 means ∀xi and xi'=1 means ∃xi. So the desired quantification result can be read off in the corresponding truth table row of Q.
Lets make an example:
P = (- A v B) & (-B v C)
Q = (A' v B') & (B' v C') & (A' v C')
A'B'C' Q
000 0
001 0
010 0
011 1 eg: (All A)(Exists B)(Exists C) P
100 0
101 1
110 1
111 1
Or as a Prolog call, computing Q from P for the above example:
?- bob([c,b,a], [[],[c],[b,c],[a,b,c]], A).
A = [[b, a], [c, a], [c, b], [c, b, a]]
Open source:
Some curious transformation by Daniel Pehoushek
https://gist.github.com/jburse/928f060331ed7d5307a0d3fcd6d4aae9#file-bob-pl

How to define the range function on a relation in Agda (set theory)

I'm trying to find a way to prove a couple of set theory-based problems in Agda, but I'm having a hard time defining the function range.
I took the definition of Subset from Proving decidability of subset in Agda and built on top of it. This is what I got so far:
open import Data.Bool as Bool using (Bool; true; false; T; _∨_; _∧_)
open import Data.Unit using (⊤; tt)
open import Level using (Level; _⊔_; 0ℓ) renaming (suc to lsuc)
open import Data.Product using (_×_) renaming (_,_ to ⟨_,_⟩)
Subset : ∀ {α} (A : Set α) -> Set _
Subset A = A → Bool
_∈_ : ∀ {α} {A : Set α} → A → Subset A → Set
a ∈ p = T (p a)
Relation : ∀ {α β} (A : Set α) (B : Set β) → Set (α ⊔ β)
Relation A B = Subset (A × B)
Range : ∀ {A B : Set} → Relation A B → Subset B
Range = ?
_⊆_ : ∀ {A : Set} → Subset A → Subset A → Set
A ⊆ B = ∀ x → x ∈ A → x ∈ B
wholeSet : ∀ (A : Set) → Subset A
wholeSet _ = λ _ → true
∀subset⊆set : ∀ {A : Set} {sub : Subset A} → sub ⊆ wholeSet A
∀subset⊆set = λ _ _ → tt
_∩_ : ∀ {A : Set} → Subset A → Subset A → Subset A
A ∩ B = λ x → (A x) ∧ (B x)
⊆-range-∩ : ∀ {A B : Set}
(F G : Relation A B)
→ Range (F ∩ G) ⊆ (Range F ∩ Range G)
⊆-range-∩ f g = ?
The problem is that Range takes as an input a function of type A × B → Bool and must return a function B → Bool such that a value B is true iff there exists a value A × B which is true in the initial function. Basically, I would need to iterate through all values of A to know whether B is in the range of the relation. Something impossible to do, isn't it?
There must be surely a better way to implement Range, doesn't it?
Here is the implementation I suggest :
open import Data.Unit
open import Data.Product renaming (_,_ to ⟨_,_⟩)
open import Data.Sum
open import Function
Change the definition of Subset to go to Set instead of Bool. I know this might be controversial, but in my experience this has always been the way to go, and also this is how subsets are implemented in the standard library. (By the way, if you are interested to see the implementation in the standard library, it is in the file Relation/Unary.agda). I also removed the levels of universe since you didn't use them in your later definitions, which led me to clean up the types of the module.
Subset : Set → Set₁
Subset A = A → Set
The definition of membership is changed accordingly.
_∈_ : ∀ {A} → A → Subset A → Set
a ∈ P = P a
Relation : ∀ A B → Set₁
Relation A B = Subset (A × B)
The range becomes then very natural : b is in the range of R if their exists an a such as R of a and b holds.
Range : ∀ {A B} → Relation A B → Subset B
Range R b = ∃ (R ∘ ⟨_, b ⟩) -- equivalent to ∃ \a → R ⟨ a , b ⟩
_⊆_ : ∀ {A} → Subset A → Subset A → Set
A ⊆ B = ∀ x → x ∈ A → x ∈ B
Not much to say about the wholeset
wholeSet : ∀ A → Subset A
wholeSet _ _ = ⊤
∀subset⊆set : ∀ {A sub} → sub ⊆ wholeSet A
∀subset⊆set _ _ = tt
_∩_ : ∀ {A} → Subset A → Subset A → Subset A
(A ∩ B) x = x ∈ A × x ∈ B
The proof of range inclusion is done very naturally with this definition.
⊆-range-∩ : ∀ {A B} {F G : Relation A B} → Range (F ∩ G) ⊆ (Range F ∩ Range G)
⊆-range-∩ _ ⟨ a , ⟨ Fab , Gab ⟩ ⟩ = ⟨ ⟨ a , Fab ⟩ , ⟨ a , Gab ⟩ ⟩
I also took the liberty to add the corresponding property about union.
_⋃_ : ∀ {A} → Subset A → Subset A → Subset A
(A ⋃ B) x = x ∈ A ⊎ x ∈ B
⋃-range-⊆ : ∀ {A B} {F G : Relation A B} → (Range F ⋃ Range G) ⊆ Range (F ⋃ G)
⋃-range-⊆ _ (inj₁ ⟨ a , Fab ⟩) = ⟨ a , inj₁ Fab ⟩
⋃-range-⊆ _ (inj₂ ⟨ a , Gab ⟩) = ⟨ a , inj₂ Gab ⟩

Haskell to Prolog or just Prolog delaunay triangulation

I have completed a haskell code to compute the delaunay triangulation of a given point set. However, now i am stuck as to how and what method needs to be completed in prolog
Haskell:
-- The type for a single point.
type Point a = (a,a)
-- The type for a pair of points.
type Pair a = (Point a, Point a)
-- The type for a triple of points.
type Triple a = (Point a, Point a, Point a)
-- Predicate for a triple of 3 points is in CCW order or not
isCCW :: Real a => Triple a -> Bool
isCCW ((x1, y1), (x2, y2), (x3, y3)) = (x2-x1)*(y3-y1)-(x3-x1)*(y2-y1) > 0
-- Convert a triple to a CCW triple
toCCW :: Real a => Triple a -> Triple a
toCCW (p1, p2, p3) = if (isCCW ((( p1, p2, p3 )))) then (p1, p2, p3)
else (p1, p3, p2)
-- Generate all pairs of points from a list of points.
-- Each pair should appear exactly once in the result.
pairsFromPoints :: Real a => [Point a] -> [Pair a]
pairsFromPoints [] = []
pairsFromPoints (x:xs) = map makePair xs ++ (pairsFromPoints xs)
where makePair y = (x,y)
-- Generate all unique CCW triples of points from a list of points
-- Each triple should appear exactly once in the result and be
-- CCW ordered.
triplesFromPoints :: Real a => [Point a] -> [Triple a]
triplesFromPoints [] = []
triplesFromPoints (x:xs) = map makeTriple (pairsFromPoints xs) ++ (triplesFromPoints xs)
where makeTriple (y,z) = toCCW(x,y,z)
And this is the Prolog code that I'm stuck on.
Prolog:
% concatenate(L1, L1, T) is true if and only if T is equal to the concatenation
% of lists L1 and L2.
%
concatenate(L1, L2, T).
% singletons(P, Q) is true if and only if Q is equivalent to the list obtained
% from P if each item in P is wrapped in "[" and "]" to create a singleton list.
%
singletons(P, Q).
% prefix_all(I, P, Q) is true if and only if P is a list of lists and Q is the
% list obtained by prepending I to each element in P.
%
prefix_all(I, P, Q).
% pairs_all(I, P, Q) is true if and only if Q is the list obtained by pairing I
% with each item in P.
%
pairs_all(I, P, Q).
% Predicate to test if three points are in counter-clockwise orientation.
%
is_ccw([[X1,Y1],[X2,Y2],[X3,Y3]]) :- (X2-X1)*(Y3-Y1)-(X3-X1)*(Y2-Y1) > 0.
% ccw(T, U) is true if and only if T and U are triples containing the same
% points and U is in counter-clockwise orientation.
%
ccw(T, U).
% ccw_triples(P, Q) is true if and only if Q is the list containing all the
% triples of points in the list P except arranged in ccw orientation.
%
ccw_triples(P, Q).
% pairs_of_points([H|T], Q) is true if and only if Q is a list containing all of
% the distinct pairs that can be made from the points in the list of points
% [H|T].
%
pairs_of_points([H|T], Q).
% triples_of_points([H|T], Q) is true if and only if Q is a list containing all
% of the distinct triples that can be made from the points in the list of points
% [H|T].
%
triples_of_points([H|T], X).
% is_delaunay_triangle(T, P) is true if and only if no point of the point set P
% is in the circle defined by the triple T (which here you may assume is in CCW
% orientation). This predicate is undefined if P is empty.
%
is_delaunay_triangle(T, P).
% delaunay_triangles(T, P, X) is true if and only if X is the subset of
% triangles from T that are Delaunay triangles for the point set P.
%
% HINT: Define this recursively on the list of triangles T.
%
delaunay_triangles(T, P, X).
% delaunay_triangulation(P, X) is true if and only if X is the list of Delaunay
% triangles for the point list P.
% HINT: Create temporary variables to describe all triples from P as well as all
% CCW triples from P. Use the predicates you've already defined above!
%
delaunay_triangulation(P, X).
I am not exactly sure what exactly the first four methods exactly mean, if someone could give me that as a start i would be content I'm not asking you to do my assignment either but any help would be greatly appreciated!
concatenate(L1, L1, T) is true if and only if T is equal to the concatenation of lists L1 and L2.
This should read concatenate(L1, L2, T) and is the standard append/3 predicate. It corresponds to Haskell's (++) function for list concatenation. For example, it should behave as follows:
?- concatenate([], [1, 2, 3], T).
T = [1, 2, 3].
?- concatenate([1, 2], [3, 4], T).
T = [1, 2, 3, 4].
singletons(P, Q) is true if and only if Q is equivalent to the list obtained from P if each item in P is wrapped in "[" and "]" to create a singleton list.
It looks like this should behave as follows:
?- singletons([foo, bar, baz, 42], Singletons).
Singletons = [[foo], [bar], [baz], [42]].
You may find this easier to do if you first define an auxiliary predicate that only wraps a single term in a list:
?- singleton(foo, Q).
Q = [foo].
?- singleton(foo, [foo]).
true.
(You do not need to use this in your definition of singletons/2, writing it might just clarify part of the problem.)
prefix_all(I, P, Q) is true if and only if P is a list of lists and Q is the list obtained by prepending I to each element in P.
The meaning of this depends on what "prepending" is supposed to mean, but the word prefix suggests that I is to be interpreted as a list that will be the prefix of any list in Q. So something like:
?- prefix_all([pre, fix], [[1, 2], [], [foo, bar, baz]], Q).
Q = [[pre, fix, 1, 2], [pre, fix], [pre, fix, foo, bar, baz]].
Once again, it may help to think about what it means to be the prefix of one list:
?- prefix_one([pre, fix], [1, 2], Xs).
Xs = [pre, fix, 1, 2].
Do not define this predicate! Think about what it means in terms of what you already know.
pairs_all(I, P, Q) is true if and only if Q is the list obtained by pairing I with each item in P.
This looks like it's meant to behave something like this:
?- pairs_all(foo, [1, 2, three, 4], Pairs).
Pairs = [ (foo, 1), (foo, 2), (foo, three), (foo, 4)].
Again, it may help to first define an auxiliary that constructs a single pair:
?- pair(foo, 5, Pair).
Pair = (foo, 5).

Can I have two values for a constant within an NDSolve function that are dependent on the output of the NDSolve function?

I have a system of ODE's. One of the ODE's has a constant parameter which I want to alter between two different values depending on one of the ODE solutions.
So for example let's say that I have the following equations:
{
A'[x] == -q A[x]B[x],
B'[x] == q A[x]B[x] - g B[x],
C'[x] == g B[x]
}
Now I can solve them easily using the NDSolve function when q and g are constant values. What I want to do though is vary the value of q so that it has one value when B[x] is below a certain threshold but then changes in value when B[x] rises above this threshold value.
I've tried using If statements and Piecewise functions outside of the NDSolve but I haven't managed to get it working.
This might do something like what you want. I left out the third equation, which seems superfluous.
Clear[f, g, s, t, x];
s[a_, b_] = Piecewise[{{a*b - b, b < 1}, {2 a*b - b, b >= 1}}];
t[a_, b_] = Piecewise[{{-a*b, b < 1}, {-2 a*b, b >= 1}}];
{f[x_], g[x_]} = {f[x], g[x]} /.
First[NDSolve[{
f'[x] == t[f[x], g[x]],
g'[x] == s[f[x], g[x]],
f[0] == 10, g[0] == 1},
{f[x], g[x]}, {x, 0, 2}]]

How to enum the patterns

How to generate all patterns in the following rule:
# as a separator, { and } means choose a string between { and }.
For example:
Input: a string
{a # b} c {d # e {f # g}}
Output should be:
a c d
b c d
a c e f
a c e g
First, parse the input string into a tree using any standard parsing algorithm. The tree-form expression for your example above would be something like (C = concatenate, S = select)
C(S(a,b),C(c,S(d,C(e,S(f,g)))))
Now then implement a procedure that recursively evaluates this tree (or expression, as you might call it) and returns a set of strings as the result of evaluation any subexpression. Then the evaluation goes like this
S(f,g) == "f", "g"
C(e,S(f,g)) == "ef", "eg"
S(d,C(e,S(f,g))) = "d", "ef", "eg"
C(c,S(d,C(e,S(f,g)))) = "cd", "cef", "ceg"
S(a,b) = "a", "b"
C(S(a,b),C(c,S(d,C(e,S(f,g))))) = "acd", "bcd", "acef", "bcef", "aceg", "bceg"
(by the way, you are missing bcef and bceg from your example)
The evaluation rules are:
S(X,Y) : evaluate X and Y and take the union of the sets
C(X,Y) : evaluate X and Y and form all concatenations from taking one string from set X and one string from set Y

Resources