Skip to content

Instantly share code, notes, and snippets.

@MgaMPKAy
Created May 10, 2012 16:00
Show Gist options
  • Save MgaMPKAy/2654107 to your computer and use it in GitHub Desktop.
Save MgaMPKAy/2654107 to your computer and use it in GitHub Desktop.
Sofeware Fundation & Coq
Inductive day : Type :=
| monday : day
| tuesday : day
| wendnesday : day
| thursday : day
| friday : day
| saturday : day
| sunday : day.
Definition next_weekday (d:day) : day :=
match d with
| monday => tuesday
| tuesday => wendnesday
| wendnesday => thursday
| thursday => friday
| friday => monday
| saturday => monday
| sunday => monday
end.
Eval simpl in (next_weekday friday).
Eval simpl in (next_weekday (next_weekday saturday)).
Example test_next_weekday:
(next_weekday (next_weekday saturday)) = tuesday.
Proof.
simpl.
reflexivity.
Qed.
Inductive bool : Type :=
| true : bool
| false : bool.
Definition negb (b:bool) : bool :=
match b with
| true => false
| false => true
end.
Definition andb (b1:bool) (b2:bool) : bool :=
match b1 with
| true => b2
| false => false
end.
Definition orb (b1:bool) (b2:bool) : bool :=
match b1 with
| true => true
| false => b2
end.
Example test_orb1:(orb true false) = true.
Proof.
simpl.
reflexivity.
Qed.
Definition admit {T: Type} : T. Admitted.
Definition nandb (b1:bool) (b2:bool) : bool :=
negb (andb b1 b2).
Example test_nandb1: (nandb true false) = true.
Proof.
simpl.
reflexivity.
Qed.
Example test_nandb2: (nandb false false) = true.
Proof. simpl. reflexivity. Qed.
Example test_nandb3: (nandb false true) = true.
Proof. simpl. reflexivity. Qed.
Example test_nandb4: (nandb true true) = false.
Proof. simpl. reflexivity. Qed.
Definition andb3 (b1:bool) (b2:bool) (b3:bool) : bool :=
(andb (andb b1 b2) b3).
Example test_andb31: (andb3 true true true) = true.
Proof. simpl. reflexivity. Qed.
Example test_andb32: (andb3 false true true) = false.
Proof. simpl. reflexivity. Qed.
Example test_andb33: (andb3 true false true) = false.
Proof. simpl. reflexivity. Qed.
Example test_andb34: (andb3 true true false) = false.
Proof. simpl. reflexivity. Qed.
Module Playground1.
Inductive nat : Type :=
| O : nat
| S : nat -> nat.
Definition pred (n:nat) : nat :=
match n with
| O => O
| S n' => n'
end.
End Playground1.
Definition minustwo (n:nat) : nat :=
match n with
| O => O
| S O => O
| S (S n') => n'
end.
Check (S (S (S (S O)))).
Eval simpl in (minustwo 4).
Check S.
Check pred.
Check minustwo.
Fixpoint evenb (n:nat) : bool :=
match n with
| O => true
| S O => false
| S (S n') => evenb n'
end.
Definition oddb (n:nat) : bool := negb (evenb n).
Example test_oddb1: (oddb (S O)) = true.
Proof. simpl. reflexivity. Qed.
Example test_oddb2: (oddb (S (S (S (S O))))) = false.
Proof. simpl. reflexivity. Qed.
Module Playground2.
Fixpoint plus (n:nat) (m:nat) : nat :=
match n with
| O => m
| S n' => S (plus n' m)
end.
Eval simpl in (plus (S (S (S O))) (S (S O))).
Fixpoint mult (n m:nat) : nat :=
match n with
| O => O
| S n' => plus m (mult n' m)
end.
Fixpoint minus (n m:nat) : nat :=
match n, m with
| O, _ => O
| S _, O => n
| S n', S m' => minus n' m'
end.
End Playground2.
Fixpoint exp (base power : nat) : nat :=
match power with
| O => S O
| S p => mult base (exp base p)
end.
Example test_mult1: (mult 3 3) = 9.
Proof. simpl. reflexivity. Qed.
Fixpoint factorial (n:nat) : nat :=
match n with
| O => S O
| S n' => mult n (factorial n')
end.
Example test_factorial1: (factorial 3) = 6.
Proof. simpl. reflexivity. Qed.
Example test_factorial2: (factorial 5) = (mult 10 12).
Proof. simpl. reflexivity. Qed.
Notation "x + y" := (plus x y)
(at level 50, left associativity) : nat_scrope.
Notation "x - y" := (minus x y)
(at level 50, left associativity) : nat_scope.
Notation "x * y" := (mult x y)
(at level 40, left associativity) : nat_scope.
Check ((0 + 1) - 1).
Fixpoint beq_nat (n m:nat) : bool :=
match n with
| O => match m with
| O => true
| S m' => false
end
| S n' => match m with
| O => false
| S m' => beq_nat n' m'
end
end.
Fixpoint ble_nat (n m : nat) : bool :=
match n with
| O => true
| S n' =>
match m with
| O => false
| S m' => ble_nat n' m'
end
end.
Example test_ble_nat1: (ble_nat 2 2) = true.
Proof. simpl. reflexivity. Qed.
Example test_ble_nat2: (ble_nat 2 4) = true.
Proof. simpl. reflexivity. Qed.
Example test_ble_nat3: (ble_nat 4 2) = false.
Proof. simpl. reflexivity. Qed.
Definition blt_nat (n m : nat) : bool :=
(andb (ble_nat n m) (negb (beq_nat n m))).
Example test_blt_nat1: (blt_nat 2 2) = false.
Proof. simpl. reflexivity. Qed.
Example test_blt_nat2: (blt_nat 2 4) = true.
Proof. simpl. reflexivity. Qed.
Example test_blt_nat3: (blt_nat 4 2) = false.
Proof. simpl. reflexivity. Qed.
Theorem plus_O_n : forall n:nat, 0 + n = n.
Proof.
simpl.
reflexivity.
Qed.
Theorem plus_O_n' : forall n:nat, 0 + n = n.
Proof.
reflexivity.
Qed.
Eval simpl in (forall n:nat, n + 0 = n).
Eval simpl in (forall n:nat, 0 + n = n).
(* Definition of plus affect simpl *)
Theorem plus_O_n'' : forall n:nat, 0 + n = n.
Proof.
intros n.
reflexivity.
Qed.
Theorem plus_1_l : forall n:nat, 1 + n = S n.
Proof.
intros.
reflexivity.
Qed.
Theorem plus_id_example : forall n m:nat, n = m -> n + n = m + m.
Proof.
intros n m H.
rewrite <- H.
reflexivity.
Qed.
Theorem plus_id_exercise : forall n m o : nat,
n = m -> m = o -> n + m = m + o.
Proof.
intros n m o H1 H2.
rewrite -> H1.
rewrite <- H2.
reflexivity.
Qed.
Theorem mult_0_plus : forall n m : nat,
(0 + n) * m = n * m.
Proof.
intros n m.
rewrite -> plus_O_n.
reflexivity.
Qed.
Theorem mult_1_plus : forall n m : nat,
(1 + n) * m = m + (n * m).
Proof.
intros.
rewrite -> plus_1_l.
(* no nessary *)
simpl.
reflexivity.
Qed.
Theorem plus_1_neq_0_firsttry : forall n:nat,
beq_nat (n + 1) 0 = false.
Proof.
intros n.
destruct n as [| n'].
reflexivity.
reflexivity.
Qed.
Theorem negb_involutive : forall b:bool,
negb (negb b) = b.
Proof.
destruct b.
reflexivity.
reflexivity.
Qed.
Theorem zero_nbeq_plus_1 : forall n:nat,
beq_nat 0 (n + 1) = false.
Proof.
intros n.
destruct n.
reflexivity.
reflexivity.
Qed.
Require String.
Open Scope string_scope.
Ltac move_to_top x :=
match reverse goal with
| H : _ |- _ => try move x after H
end.
Tactic Notation "assert_eq" ident(x) constr(v) :=
let H := fresh in
assert (x = v) as H by reflexivity;
clear H.
Tactic Notation "Case_aux" ident(x) constr(name) :=
first [
set (x := name); move_to_top x
| assert_eq x name; move_to_top x
| fail 1 "because we are working on a different case" ].
Tactic Notation "Case" constr(name) := Case_aux Case name.
Tactic Notation "SCase" constr(name) := Case_aux SCase name.
Tactic Notation "SSCase" constr(name) := Case_aux SSCase name.
Tactic Notation "SSSCase" constr(name) := Case_aux SSSCase name.
Tactic Notation "SSSSCase" constr(name) := Case_aux SSSSCase name.
Tactic Notation "SSSSSCase" constr(name) := Case_aux SSSSSCase name.
Tactic Notation "SSSSSSCase" constr(name) := Case_aux SSSSSSCase name.
Tactic Notation "SSSSSSSCase" constr(name) := Case_aux SSSSSSSCase name.
Theorem andb_true_elim1 : forall b c:bool,
andb b c = true -> b = true.
Proof.
intros b c H.
destruct b.
Case "b = true".
reflexivity.
Case "b = false".
rewrite <- H.
reflexivity.
Qed.
Theorem andb_true_elim2 : forall b c:bool,
andb b c = true -> c = true.
Proof.
intros b c H.
destruct c.
Case "c = true".
reflexivity.
Case "c = false".
rewrite <- H.
destruct b.
SCase "b = true".
reflexivity.
SCase "b = true".
reflexivity.
Qed.
Theorem plus_0_r : forall n:nat, n + 0 = n.
Proof.
intros n.
induction n as [| n'].
Case "n = 0".
reflexivity.
Case "n = S n'".
simpl.
rewrite -> IHn'.
reflexivity.
Qed.
Theorem minus_diag : forall n,
minus n n = 0.
Proof.
intros n.
induction n.
Case "n = 0".
reflexivity.
Case "n = S n".
simpl.
rewrite -> IHn.
reflexivity.
Qed.
Theorem mult_0_r : forall n:nat,
n * 0 = 0.
Proof.
intros n.
induction n.
Case "n = 0".
reflexivity.
Case "n = S n".
simpl.
rewrite -> IHn.
reflexivity.
Qed.
Theorem plus_n_Sm : forall n m : nat,
S (n + m) = n + (S m).
Proof.
intros n m.
induction n as [|n'].
Case "n = 0".
simpl.
reflexivity.
Case "n = S n'".
simpl.
rewrite -> IHn'.
reflexivity.
Qed.
Theorem plus_comm : forall n m : nat,
n + m = m + n.
Proof.
intros n m.
induction n as [|n'].
Case "n = 0".
rewrite -> plus_0_r.
reflexivity.
Case "n = S n'".
simpl.
rewrite -> IHn'.
rewrite -> plus_n_Sm.
reflexivity.
Qed.
(* TODO *)
Theorem plus_comm3 : forall n m : nat,
n + m = m + n.
Proof.
intros.
induction m as [|m'].
Case "m = 0".
induction n as [|n'].
SCase "n = 0".
reflexivity.
SCase "n = S n'".
simpl.
rewrite -> IHn'.
reflexivity.
Case "m = S m'".
simpl.
rewrite <- IHm'.
induction n as [|n'].
SCase "n = 0".
simpl.
reflexivity.
SCase "n = S n'".
simpl.
(* auto 2. *)
(* or *)
rewrite <- plus_n_Sm. reflexivity.
Qed.
Fixpoint double (n : nat) :=
match n with
| O => O
| S n' => S (S (double n'))
end.
Lemma double_plus : forall n, double n = n + n.
Proof.
intros.
induction n as [|n'].
Case "n = 0".
simpl.
reflexivity.
Case "n = S n''".
simpl.
rewrite -> IHn'.
rewrite -> plus_n_Sm.
reflexivity.
Qed.
Theorem beq_nat_refl : forall n : nat,
true = beq_nat n n.
Proof.
intros.
induction n as [|n'].
Case "n = 0".
reflexivity.
Case "n = S n".
simpl.
rewrite <- IHn'.
reflexivity.
Qed.
Theorem plus_swap : forall n m p: nat,
n + (m + p) = m + (n + p).
Proof.
intros.
assert (H: p + n = n + p).
Case "Proof of assertion".
rewrite -> plus_comm.
reflexivity.
rewrite <- H.
rewrite -> plus_comm.
induction m as [|m'].
Case "m = 0".
reflexivity.
Case "m = S m'".
simpl.
rewrite -> IHm'.
reflexivity.
Qed.
(* TODO *)
Theorem evenb_n__oddb_sn : forall n:nat,
evenb n = negb (evenb (S n)).
Proof.
intros.
assert (forall m : nat, evenb m = oddb (S m)).
Case "Proof of assertion".
intros.
induction m as [|m'].
SCase "m = 0".
reflexivity.
SCase "m = S m'".
admit.
admit.
Qed.
(* More exersice *)
Require Export Basics.
Module NatList.
Inductive natprod : Type :=
pair : nat -> nat -> natprod.
Definition fst (p : natprod) : nat :=
match p with
| pair x y => x
end.
Definition snd (p : natprod) : nat :=
match p with
| pair x y => y
end.
Notation "( x , y )" := (pair x y).
Eval simpl in (fst (3, 4)).
Definition fst' (p : natprod) : nat :=
match p with
| (x, y) => x
end.
Definition snd' (p : natprod) : nat :=
match p with
| (x, y) => y
end.
Definition swap_pair (p : natprod) : natprod :=
match p with
| (x, y) => (y, x)
end.
Theorem surjective_pairing' : forall (n m:nat),
(n, m) = (fst (n,m), snd (n,m)).
Proof.
reflexivity.
Qed.
Theorem surjective_pairing : forall (p : natprod),
p = (fst p, snd p).
Proof.
intros.
destruct p as (m, n).
reflexivity.
Qed.
Theorem snd_fst_is_swap : forall (p : natprod),
(snd p, fst p) = swap_pair p.
Proof.
intros.
destruct p as (m, n).
reflexivity.
Qed.
Theorem fst_swap_is_snd : forall (p : natprod),
fst (swap_pair p) = snd p.
Proof.
intros.
destruct p as (m, n).
reflexivity.
Qed.
Inductive natlist : Type :=
| nil : natlist
| cons : nat -> natlist -> natlist.
Definition l_123 := cons 1 (cons 2 (cons 3 nil)).
Notation "x :: l" := (cons x l) (at level 60, right associativity).
Notation "[ ]" := nil.
Notation "[ x , .. , y ]" := (cons x .. (cons y nil) ..).
Definition l_123' := 1 :: (2 :: (3 :: nil)).
Definition l_123'' := 1 :: 2 :: 3 :: nil.
Definition l_123''' := [1,2,3].
Notation "x + y" := (plus x y) (at level 50, left associativity).
Fixpoint repeat (n count: nat) : natlist :=
match count with
| 0 => nil
| S count' => n :: (repeat n count')
end.
Eval simpl in (repeat 1 4).
Fixpoint length (l:natlist) : nat :=
match l with
| nil => 0
| h :: t => S (length t)
end.
Fixpoint app(l1 l2:natlist) : natlist :=
match l1 with
| nil => l2
| h :: t => h :: (app t l2)
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment