Proving f (f bool) = bool

前端 未结 4 1238
悲&欢浪女
悲&欢浪女 2020-12-30 07:01

How can I in coq, prove that a function f that accepts a bool true|false and returns a bool true|false (shown below), when applied twi

相关标签:
4条回答
  • 2020-12-30 07:18

    Thanks for wonderful assignment! Such a lovely theorem!

    This is the proof using C-zar declarative proof style for Coq. It is a much longer than imperative ones (altrough it might be such because of my too low skill).

    Theorem bool_cases : forall a, a = true \/ a = false.
    proof.
        let a:bool.
        per cases on a.
        suppose it is false.
            thus thesis.
        suppose it is true.
            thus thesis.
        end cases.
    end proof. Qed.
    
    Goal forall (b:bool), f (f (f b)) = f b.
    proof.
        let b:bool.
        per cases on b.
    
        suppose it is false.
            per cases of (f false = false \/ f false = true) by bool_cases.
            suppose (f false = false).
                hence (f (f (f false)) = f false).
            suppose H:(f false = true).
                per cases of (f true = false \/ f true = true) by bool_cases.
                suppose (f true = false).
                    hence (f (f (f false)) = f false) by H.
                suppose (f true = true).
                    hence (f (f (f false)) = f false) by H.
                end cases.
            end cases.
    
        suppose it is true.
            per cases of (f true = false \/ f true = true) by bool_cases.
            suppose H:(f true = false).
                per cases of (f false = false \/ f false = true) by bool_cases.
                suppose (f false = false).
                    hence (f (f (f true)) = f true) by H.
                suppose (f false = true).
                    hence (f (f (f true)) = f true) by H.
                end cases.
            suppose (f true = true).
                hence (f (f (f true)) = f true).
            end cases.
    
    end cases.
    end proof. Qed.
    
    0 讨论(0)
  • 2020-12-30 07:22
    Goal forall (f:bool -> bool) (b:bool), f (f (f b)) = f b.
    Proof.
    intros.
    remember (f true) as ft.
    remember (f false) as ff.
    destruct ff ; destruct ft ; destruct b ; 
        try rewrite <- Heqft ; try rewrite <- Heqff ; 
        try rewrite <- Heqft ; try rewrite <- Heqff ; auto.
    Qed.
    
    0 讨论(0)
  • 2020-12-30 07:27

    A tad shorter proof:

    Require Import Sumbool.
    
    Goal forall (f : bool -> bool) (b:bool), f (f (f b)) = f b.
    Proof.
      destruct b;                             (* case analysis on [b] *)
        destruct (sumbool_of_bool (f true));  (* case analysis on [f true] *)
        destruct (sumbool_of_bool (f false)); (* case analysis on [f false] *)
        congruence.                           (* equational reasoning *)
    Qed.
    
    0 讨论(0)
  • 2020-12-30 07:31

    In SSReflect:

    Require Import ssreflect.
    
    Goal forall (f:bool -> bool) (b:bool), f (f (f b)) = f b.
    Proof.
    move=> f.
    by case et:(f true); case ef:(f false); case; rewrite ?et ?ef // !et ?ef.
    Qed.
    
    0 讨论(0)
提交回复
热议问题