eqcert.dk 7.23 KB
Newer Older
Raphael Cauderlier's avatar
Raphael Cauderlier committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 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 130 131 132 133 134 135 136 137 138 139 140 141 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 201 202
#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 ;)

def type := fol.type.
def term := fol.term.
def prop := fol.prop.
def proof := fol.proof.
def certificate := cert.certificate.

not_an_equality : tac.exc.
def match_equality : prop -> (A : type -> term A -> term A -> certificate) -> certificate -> certificate.

[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.

not_convertible : A : type -> term A -> term A -> tac.exc.

(; reflexivity proves goals of the form a = a ;)
def reflexivity : certificate :=
 cert.with_goal (G =>
            match_equality G (A : type => a : term A => b : term A =>
                             cert.try (cert.exact (eq.eq A a a) (eq.eq_refl A a))
                                 (__ => 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 =>
             match_equality G (A : type => a : term A => b : term A =>
                               cert.refine (eq.eq A b a)
                                      (eq.eq A a b)
                                      (eq.eq_symm A a b)
                                      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 ;)
def transitivity (A : type) (b : term A) (c1 : certificate) (c2 : certificate) : certificate :=
  cert.with_goal (G =>
             match_equality G (A' : type => a : term A' => c : term A' =>
                               cert.ifeq_type A A' (f =>
                               cert.refine2 (eq.eq A' a (f b))
                                       (eq.eq A' (f b) c)
                                       (eq.eq A' a c)
                                       (eq.eq_trans A' a (f b) c)
                                       c1
                                       c2)
                              (cert.raise trans_bad_type))
                              (cert.raise not_an_equality)).

(; 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
     (eq.eq A a b)
     (eq.eq B
        (f (fol.cons_term A a As as))
        (f (fol.cons_term A a As bs)))
     (eq.eq B
        (f (fol.cons_term A a As as))
        (f (fol.cons_term A b As bs)))
     (Hab : proof (eq.eq A a b) =>
      Hf : proof (eq.eq B
                    (f (fol.cons_term A a As as))
                    (f (fol.cons_term A a As bs))) =>
      Hab (b => eq.eq B
                  (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.

(; rewrite A a b c1 c2 proves G if c1 proves a = b and c2 proves G{b/a} ;)
def rewrite (A : type) (a : term A) (b : term A) (c1 : certificate) (c2 : certificate) : certificate
:=
  cert.with_goal (G =>
             cert.refine2 (eq.eq A a b)
                     (unif.subst_prop (unif.cons_subst A b a unif.empty_subst) G)
                     G
                       (Hab : proof (eq.eq A a b) =>
                        Hab (a =>
                             unif.subst_prop
                               (unif.cons_subst A b a unif.empty_subst) G))
                       c1
                       c2).