eqcert.dk 8.18 KB
Newer Older
Raphael Cauderlier's avatar
Raphael Cauderlier committed
1 2 3 4 5 6 7 8 9 10 11
#NAME eqcert.

(; Customization of Emacs mode for Dedukti: this file is not linear
   and requires a path extended with "../fol".

   (setq-local dedukti-check-options '("-nc" "-nl" "-I" "../fol"))
   (setq-local dedukti-compile-options '("-nc" "-e" "-nl" "-I" "../fol"))
;)

(; Certificates for manipulating equalities ;)

12
def type := fol.type.
Raphael Cauderlier's avatar
Raphael Cauderlier committed
13 14 15 16
def term := fol.term.
def prop := fol.prop.
def proof := fol.proof.
def certificate := cert.certificate.
17 18 19
def eq := eq.eq.
def imp := fol.imp.
def all := fol.all.
Raphael Cauderlier's avatar
Raphael Cauderlier committed
20 21

not_an_equality : tac.exc.
22
def match_equality : prop -> (A : type -> term A -> term A -> certificate) -> certificate -> certificate.
Raphael Cauderlier's avatar
Raphael Cauderlier committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

[c] match_equality fol.false            _ c --> c
[c] match_equality (fol.and _ _)        _ c --> c
[c] match_equality (fol.or _ _)         _ c --> c
[c] match_equality (fol.imp _ _)        _ c --> c
[c] match_equality (fol.all _ _)        _ c --> c
[c] match_equality (fol.ex _ _)         _ c --> c
[c,A,a,b]
    match_equality (fol.apply_pred
                     (eq.Eq A)
                     (fol.cons_term _ a
                       _ (fol.cons_term _ b
                       _ fol.nil_term)))
                   c _
      --> c A a b
[c] match_equality (fol.apply_pred _ _) _ c --> c.

40
not_convertible : A : type -> term A -> term A -> tac.exc.
Raphael Cauderlier's avatar
Raphael Cauderlier committed
41 42 43 44

(; reflexivity proves goals of the form a = a ;)
def reflexivity : certificate :=
 cert.with_goal (G =>
45
            match_equality G (A : type => a : term A => b : term A =>
46
                             cert.try (cert.exact (eq A a a) (fol.all_elim A (x => eq A x x) (eq.eq_refl A) a))
Raphael Cauderlier's avatar
Raphael Cauderlier committed
47 48 49 50 51 52 53
                                 (__ => cert.raise (not_convertible A a b))
                             )
                             (cert.raise not_an_equality)).

(; symmetry c proves a = b if c proves b = a ;)
def symmetry (c : certificate) : certificate :=
  cert.with_goal (G =>
54
             match_equality G (A : type => a : term A => b : term A =>
55 56 57 58 59
                               cert.refine (eq A b a)
                                      (eq A a b)
                                      (fol.imp_elim (eq A b a) (eq A a b)
                                        (fol.all_elim A (y => imp (eq A y a) (eq A a y))
                                          (fol.all_elim A (x => all A (y => imp (eq A y x) (eq A x y))) (eq.eq_symm A) a) b))
Raphael Cauderlier's avatar
Raphael Cauderlier committed
60 61 62 63 64 65
                                      c
                              )
                              (cert.raise not_an_equality)).

trans_bad_type : tac.exc.
(; transitivity A b c1 c2 proves a = c if c1 proves a = b and c2 proves b = c ;)
66
def transitivity (A : type) (b : term A) (c1 : certificate) (c2 : certificate) : certificate :=
Raphael Cauderlier's avatar
Raphael Cauderlier committed
67
  cert.with_goal (G =>
68
             match_equality G (A' : type => a : term A' => c : term A' =>
Raphael Cauderlier's avatar
Raphael Cauderlier committed
69
                               cert.ifeq_type A A' (f =>
70 71 72 73 74 75 76 77 78 79
                               cert.refine2 (eq A' a (f b))
                                       (eq A' (f b) c)
                                       (eq A' a c)
                                       (H1 => H2 =>
                                        fol.imp_elim (eq A' (f b) c) (eq A' a c)
                                          (fol.imp_elim (eq A' a (f b)) (imp (eq A' (f b) c) (eq A' a c))
                                            (fol.all_elim A' (z => imp (eq A' a (f b)) (imp (eq A' (f b) z) (eq A' a z)))
                                              (fol.all_elim A' (y => all A' (z => imp (eq A' a y) (imp (eq A' y z) (eq A' a z))))
                                                (fol.all_elim A' (x => all A' (y => all A' (z => imp (eq A' x y) (imp (eq A' y z) (eq A' x z)))))
                                                  (eq.eq_trans A') a) (f b)) c) H1) H2)
Raphael Cauderlier's avatar
Raphael Cauderlier committed
80 81 82 83 84
                                       c1
                                       c2)
                              (cert.raise trans_bad_type))
                              (cert.raise not_an_equality)).

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
(; f_equal f c .. cn proves f(a1, .., an) = f(b1, .., bn) if each ci proves ai = bi ;)

def match_f_equal_goal : prop ->
                         (f : fol.function ->
                          fol.terms (fol.fun_arity f) ->
                          fol.terms (fol.fun_arity f) ->
                          certificate) ->
                         certificate.
[A,f,as,bs,c] match_f_equal_goal (fol.apply_pred
                      (eq.Eq A)
                      (fol.cons_term _ (fol.apply_fun f as)
                       _ (fol.cons_term _ (fol.apply_fun f bs)
                       _ fol.nil_term)))
                      c
      --> c f as bs.

certificates : fol.types -> Type.
nil_cert : certificates fol.nil_type.
cons_cert : A : type -> As : fol.types -> certificate -> certificates As -> certificates (fol.cons_type A As).

def ifeq_certs : L : fol.types ->
                 L' : fol.types ->
                 certificates L ->
                 certificates L'.
[L,c] ifeq_certs L L c --> c.

(; f_equal_fun L B f [a1 .. an] [b1 .. bn] [c1 .. cn] proves
   f [a1 .. an] = f [b1 .. bn] if each ci proves ai = bi ;)
def f_equal_fun : L : fol.types ->
                  B : type ->
                  (fol.terms L -> term B) ->
                  fol.terms L ->
                  fol.terms L ->
                  certificates L -> certificate.
[] f_equal_fun _ _ _ fol.nil_term fol.nil_term nil_cert --> reflexivity
[B,f,A,a,As,as,b,bs,c,cs]
    f_equal_fun
      (fol.cons_type _ _)
      B
      f
      (fol.cons_term A a As as)
      (fol.cons_term _ b _ bs)
      (cons_cert _ _ c cs)
   -->
   cert.refine2
130 131
     (eq A a b)
     (eq B
132 133
        (f (fol.cons_term A a As as))
        (f (fol.cons_term A a As bs)))
134
     (eq B
135 136
        (f (fol.cons_term A a As as))
        (f (fol.cons_term A b As bs)))
137 138
     (Hab : proof (eq A a b) =>
      Hf : proof (eq B
139 140
                    (f (fol.cons_term A a As as))
                    (f (fol.cons_term A a As bs))) =>
141
      eq.eq_congr A a b Hab (b => eq B
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
                  (f (fol.cons_term A a As as))
                  (f (fol.cons_term A b As bs)))
          Hf)
     c
     (f_equal_fun As B (l => f (fol.cons_term A a As l)) as bs cs).

def f_equal_fun_on_goal (L : fol.types) (cs : certificates L) : certificate
 :=
   cert.with_goal (G => match_f_equal_goal G
                  (f => as => bs =>
                   f_equal_fun
                     (fol.fun_arity f)
                     (fol.fun_codomain f)
                     (fol.apply_fun f)
                     as bs (ifeq_certs L (fol.fun_arity f) cs)
                  )).

def f_equal_T : fol.types -> Type.
[] f_equal_T fol.nil_type --> certificate
[As] f_equal_T (fol.cons_type _ As) --> certificate -> f_equal_T As.

def append_type : fol.types -> fol.types -> fol.types.
[As] append_type As fol.nil_type --> As
[As,B,Bs]
    append_type As (fol.cons_type B Bs) -->
    append_type (fol.snoc_type As B) Bs.

def snoc_cert : L : fol.types ->
                A : type ->
                certificates L ->
                certificate ->
                certificates (fol.snoc_type L A).
[A,c] snoc_cert _ A nil_cert c --> cons_cert A fol.nil_type c nil_cert
[A,B,Bs,c,cs,ca]
    snoc_cert (fol.cons_type _ _) A (cons_cert B Bs c cs) ca
      -->
    cons_cert B (fol.snoc_type Bs A) c (snoc_cert Bs A cs ca).


def f_equal_fun_n : L' : fol.types ->
                    L : fol.types ->
                    certificates L ->
                    f_equal_T L'.
[L,B,f,as,bs,cs]
    f_equal_fun_n fol.nil_type L cs
      -->
    f_equal_fun_on_goal L cs
[A,As,L,B,f,as,bs,cs]
    f_equal_fun_n (fol.cons_type A As) L cs
      -->
    c : certificate =>
    f_equal_fun_n As (fol.snoc_type L A) (snoc_cert L A cs c).

def f_equal (f : fol.function) :=
  f_equal_fun_n
    (fol.fun_arity f)
    fol.nil_type
    nil_cert.

Raphael Cauderlier's avatar
Raphael Cauderlier committed
201
(; rewrite A a b c1 c2 proves G if c1 proves a = b and c2 proves G{b/a} ;)
202
def rewrite (A : type) (a : term A) (b : term A) (c1 : certificate) (c2 : certificate) : certificate
Raphael Cauderlier's avatar
Raphael Cauderlier committed
203 204
:=
  cert.with_goal (G =>
205
             cert.refine2 (eq A a b)
Raphael Cauderlier's avatar
Raphael Cauderlier committed
206 207
                     (unif.subst_prop (unif.cons_subst A b a unif.empty_subst) G)
                     G
208
                       (Hab : proof (eq A a b) =>
209
                        eq.eq_congr A a b Hab (a =>
Raphael Cauderlier's avatar
Raphael Cauderlier committed
210 211 212 213
                             unif.subst_prop
                               (unif.cons_subst A b a unif.empty_subst) G))
                       c1
                       c2).