Advertisement
SkullCoder

Coq AR Ex 4.4

Nov 13th, 2019
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (* basic.v *)
  2. Require Import List Lia.
  3. Require Import String.
  4. Import ListNotations Nat.
  5. Open Scope string.
  6. Open Scope nat_scope.
  7.  
  8. Ltac inv H := inversion H; subst; clear H.
  9.  
  10. Inductive formula {Pi} :=
  11.   Bot | Top | V (p:Pi) | Not (a:formula) |
  12.   And (a b:formula) | Or (a b:formula) |
  13.   To (a b:formula) | Equiv (a b:formula).
  14.  
  15. Notation "F ∨ G" := (Or F G)(at level 71).
  16. Notation "F ∧ G" := (And F G)(at level 71).
  17. Notation "F → G" := (To F G)(at level 71).
  18. Notation "F ↔ G" := (Equiv F G)(at level 71).
  19. Notation "¬ F" := (Not F)(at level 70).
  20. Notation "x ∈ xs" := (xs x)(at level 80, only parsing).
  21. Notation "T 'set'" := (T -> Prop)(at level 80, only parsing).
  22.  
  23. Definition StringVar := @V string.
  24. Coercion StringVar : string >-> formula.
  25.  
  26. Definition bool2nat (b:bool) := if b then 1 else 0.
  27. Coercion bool2nat : bool >-> nat.
  28.  
  29. Inductive polarity := TT | FF | NN.
  30.  
  31.  
  32.  
  33. (* functions.v *)
  34.  
  35. Definition app {X} {Y} (f:X->Y) (x:option X) : option Y :=
  36.   match x with
  37.     None => None
  38.   | Some x => Some (f x)
  39.   end.
  40.  
  41. Fixpoint subst {Pi} (F G:@formula Pi) p :=
  42.   match F, p with
  43.     _, [] => Some G
  44.   | (Not F), 1::p => app Not (subst F G p)
  45.   | (And F1 F2), 1::p => app (fun f => And f F2) (subst F1 G p)
  46.   | (And F1 F2), 2::p => app (And F1) (subst F2 G p)
  47.   | (Or F1 F2), 1::p => app (fun f => Or f F2) (subst F1 G p)
  48.   | (Or F1 F2), 2::p => app (Or F1) (subst F2 G p)
  49.   | (To F1 F2), 1::p => app (fun f => To f F2) (subst F1 G p)
  50.   | (To F1 F2), 2::p => app (To F1) (subst F2 G p)
  51.   | (Equiv F1 F2), 1::p => app (fun f => Equiv f F2) (subst F1 G p)
  52.   | (Equiv F1 F2), 2::p => app (Equiv F1) (subst F2 G p)
  53.   | _, _ => None
  54.   end.
  55.  
  56. Definition negPol p :=
  57.   match p with
  58.     TT => FF
  59.   | FF => TT
  60.   | NN => NN
  61.   end.
  62.  
  63. Fixpoint pol {Pi} (F:@formula Pi) p :=
  64.   match F,p with
  65.     _, [] => Some TT
  66.   | (Not F), 1::p => app negPol (pol F p)
  67.   | (And F1 F2), 1::p => pol F1 p
  68.   | (And F1 F2), 2::p => pol F2 p
  69.   | (Or F1 F2), 1::p => pol F1 p
  70.   | (Or F1 F2), 2::p => pol F2 p
  71.   | (To F1 F2), 1::p => app negPol (pol F1 p)
  72.   | (To F1 F2), 2::p => pol F2 p
  73.   | (Equiv F1 F2), 1::p => Some NN
  74.   | (Equiv F1 F2), 2::p => Some NN
  75.   | _,_ => None
  76.   end.
  77.  
  78. Fixpoint eval {Pi} (A:Pi->bool) (F:@formula Pi) : nat :=
  79.   match F with
  80.     Bot => 0
  81.   | Top => 1
  82.   | V p => A p
  83.   | Not F => 1-eval A F
  84.   | And F G => min (eval A F) (eval A G)
  85.   | Or F G => max (eval A F) (eval A G)
  86.   | To F G => max (1-eval A F) (eval A G)
  87.   | Equiv F G => if eval A F =? eval A G then 1 else 0
  88.   end.
  89.  
  90. Definition sat {Pi} A (F:@formula Pi) : Prop := eval A F = 1.
  91.  
  92. Definition satisfiable {Pi} (F:@formula Pi) : Prop :=
  93.   exists A, sat A F.
  94.  
  95.  
  96.  
  97. (* properties.v *)
  98.  
  99. Lemma polEmpty {Pi} (H:@formula Pi) : pol H [] = Some TT.
  100. Proof.
  101.   destruct H;reflexivity.
  102. Qed.
  103.  
  104. Lemma substEmpty {Pi} (H F:@formula Pi) : subst H F [] = Some F.
  105. Proof.
  106.   destruct H;reflexivity.
  107. Qed.
  108.  
  109. Lemma appInv {X Y} (f:X->Y) x b: app f x = Some b <-> exists a, x=Some a /\ b=f a.
  110. Proof.
  111.   unfold app.
  112.   split.
  113.   - intros H. destruct x.
  114.     + exists x;split;congruence.
  115.     + congruence.
  116.   - intros (a&H0&H1). now subst.
  117. Qed.
  118.  
  119. Lemma evalDestruct {Pi} (A:Pi->bool) F: eval A F = 0 \/ eval A F = 1.
  120. Proof.
  121.   induction F;cbn.
  122.   all: try (destruct IHF1 as [->| ->], IHF2 as [-> | ->];cbn;tauto).
  123.   - now left.
  124.   - now right.
  125.   - destruct A;tauto.
  126.   - destruct eval;tauto.
  127. Qed.
  128.  
  129. Lemma evalInd {Pi} (A:Pi->bool) F (p:nat->Prop): (eval A F = 0 -> p 0) -> (eval A F = 1 -> p 1) -> forall b, eval A F = b -> p b.
  130. Proof.
  131.   intros.
  132.   destruct (evalDestruct A F);subst;rewrite H2;eauto.
  133. Qed.
  134.  
  135.  
  136. (* poscomb.v *)
  137.  
  138. Section poscomb.
  139.   Variable (Π:Type).
  140.   Implicit Type (N:@formula Π set). (* Set of formulas (possible infinite) *)
  141.   Definition satSet A N := forall F, F ∈ N -> sat A F.
  142.   Definition satisfiableSet N := exists A, satSet A N.
  143.  
  144.   Inductive poscomb N : formula set :=
  145.   | poscombBase F: F ∈ N -> F ∈ poscomb N
  146.   | poscombAnd F F': F ∈ poscomb N -> F' ∈ poscomb N -> F ∧ F' ∈ poscomb N
  147.  | poscombOr F F': F ∈ poscomb N -> F' ∈ poscomb N -> F ∨ F' ∈ poscomb N.
  148.  
  149.   Lemma poscombSat N A : satSet A N -> forall F, F ∈ poscomb N -> sat A F.
  150.   Proof.
  151.     intros H F.
  152.     induction 1;unfold sat in *;cbn;try lia.
  153.     now apply H in H0.
  154.   Qed.
  155.  
  156.   Lemma L12 N: satisfiableSet N -> forall F, F ∈ poscomb N -> satisfiable F.
  157.   Proof.
  158.     intros [A H] F G. exists A.
  159.     apply (poscombSat N A H F G).
  160.   Qed.
  161.  
  162. End poscomb.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement