]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/library/demo/realisability.ma
Preparing for 0.5.9 release.
[helm.git] / helm / software / matita / library / demo / realisability.ma
1 (**************************************************************************)
2 (*       ___                                                                  *)
3 (*      ||M||                                                             *)
4 (*      ||A||       A project by Andrea Asperti                           *)
5 (*      ||T||                                                             *)
6 (*      ||I||       Developers:                                           *)
7 (*      ||T||       A.Asperti, C.Sacerdoti Coen,                          *)
8 (*      ||A||       E.Tassi, S.Zacchiroli                                 *)
9 (*      \   /                                                             *)
10 (*       \ /        This file is distributed under the terms of the       *)
11 (*        v         GNU Lesser General Public License Version 2.1         *)
12 (*                                                                        *)
13 (**************************************************************************)
14
15 include "logic/connectives.ma".
16 include "datatypes/constructors.ma".
17
18 (* The following is a stranslation in Matita of the initial part of
19    Erik Palmgren, ``Internalizing Modified Realizability in Constructive Type
20    Theory'', Logical Methods in Computer Science, Vol 1 (2:2), 2005, pp. 1--7
21    
22    The original Agda file realisability.agda can be found at
23    
24    http://www.math.uu.se/~palmgren/modif/realisability.agda
25 *)
26
27 inductive SP : Type ≝
28    abs: SP
29  | atom: ∀P:Prop.SP
30  | sand: SP → SP → SP
31  | sor: SP → SP → SP
32  | simp: SP → SP → SP
33  | forall: ∀A:Type. (A → SP) → SP
34  | exist: ∀A:Type. (A → SP) → SP.
35
36 let rec Prop_OF_SP F ≝
37  match F with
38   [ abs ⇒ False
39   | atom P ⇒ P
40   | sand A B ⇒ Prop_OF_SP A ∧ Prop_OF_SP B
41   | sor A B ⇒ Prop_OF_SP A ∨ Prop_OF_SP B
42   | simp A B ⇒ Prop_OF_SP A → Prop_OF_SP B
43   | forall A F ⇒ ∀x:A.Prop_OF_SP (F x)
44   | exist A F ⇒ ∃x:A.Prop_OF_SP (F x)
45   ].
46
47 inductive sigma (A:Type) (P:A → Type) : Type ≝
48  sigma_intro: ∀x:A. P x → sigma A P.
49
50 definition pi1 ≝
51  λA,P.λs:sigma A P.
52   match s with
53    [ sigma_intro a _ ⇒ a].
54
55 definition pi2 ≝
56  λA,P.λs:sigma A P.
57   match s return λs.P (pi1 ? ? s) with
58    [ sigma_intro _ p ⇒ p].
59
60 notation "hvbox(\Sigma ident i opt (: ty) break . p)"
61   right associative with precedence 20
62 for @{ 'sigma ${default
63   @{\lambda ${ident i} : $ty. $p}
64   @{\lambda ${ident i} . $p}}}.
65
66 interpretation "Sigma" 'sigma \eta.x = (sigma ? x).
67
68 let rec type_OF_SP F ≝
69  match F return λF.Type with
70   [ abs ⇒ unit
71   | atom P ⇒ unit
72   | sand A B ⇒ (type_OF_SP A) × (type_OF_SP B)
73   | sor A B ⇒ type_OF_SP A + type_OF_SP B
74   | simp A B ⇒ type_OF_SP A → type_OF_SP B
75   | forall A F ⇒ Πx:A.type_OF_SP (F x)
76   | exist A F ⇒ Σx:A.type_OF_SP (F x)
77   ].
78
79 let rec modr F : type_OF_SP F → Prop ≝
80  match F return λF.type_OF_SP F → Prop with
81   [ abs ⇒ λr.False
82   | atom P ⇒ λr.P
83   | sand A B ⇒ λr.modr A (\fst r) ∧ modr B (\snd r)
84   | sor A B ⇒
85      λr.
86       match r with
87        [ inl a ⇒ modr A a
88        | inr b ⇒ modr B b
89        ]
90   | simp A B ⇒
91      λr.
92       (Prop_OF_SP A → Prop_OF_SP B) ∧
93       ∀a:type_OF_SP A. modr A a → modr B (r a)
94   | forall A F ⇒
95      λr:Πx:A.type_OF_SP (F x).∀a:A. modr (F a) (r a)
96   | exist A F ⇒
97      λr.
98       modr (F (pi1 ? ? r)) (pi2 ? ? r)
99   ].
100
101 theorem correct: ∀F:SP.∀r:type_OF_SP F.modr F r → Prop_OF_SP F.
102  intro;
103  elim F; simplify;
104   [1,2: apply H
105   | split; simplify in r H2; 
106      [apply H;
107        [ apply (\fst r)
108        | apply (proj1 ? ? H2)
109        ]
110      | apply H1;simplify in r H2;
111        [ apply (\snd r)
112        | apply (proj2 ? ? H2)
113        ]
114      ]
115   | change in r with (type_OF_SP s + type_OF_SP s1);
116     elim r in H2 ⊢ %; simplify in H2;
117      [ left; apply H; assumption
118      | right; apply H1; assumption
119      ]
120   | simplify in H2;
121     apply (proj1 ? ? H2)
122   | simplify in H1;
123     intro;
124     apply H;
125     [2: apply H1
126     | skip
127     ]
128   | simplify in r;
129     elim r in H1 ⊢ %;
130     apply (ex_intro ? ? a);
131     apply H;
132     assumption
133   ]
134 qed.
135
136 definition realized ≝
137  λF:SP.∃r:type_OF_SP F.modr F r.
138
139 theorem correct2: ∀F:SP. realized F → Prop_OF_SP F.
140  intros;
141  elim H;
142  apply correct;
143  assumption.
144 qed.
145
146 theorem extraction:
147  ∀A,B:Type.∀P: A → B → SP.
148   realized (forall A (λa:A. exist B (λb:B. P a b))) →
149    ∀a:A.∃b:B.Prop_OF_SP (P a b).
150  intros;
151  apply (correct2 (exist ? (λb:B. P a b)));
152  simplify in H; elim H; clear H;
153  simplify;
154  apply (ex_intro ? ? (a1 a));
155  apply H1.
156 qed.
157
158 lemma true_impl_realized:
159  ∀A,B:Prop. (A → B) → realized (simp (atom A) (atom B)).
160  intros;
161  simplify;
162  apply (ex_intro ? ? (λu.u));
163  split;
164   [ assumption
165   | intro; assumption
166   ]
167 qed.
168
169 (******** rules for first order logic **********************)
170
171 lemma elim_abs: ∀P:Prop. realized (simp abs (atom P)).
172  intro;
173  simplify;
174  apply (ex_intro ? ? (λu.u));
175  split;
176   [ intro; cases H
177   | intros; cases H
178   ]
179 qed.
180
181 lemma id_axiom: ∀F:SP. realized (simp F F).
182  intros;
183  simplify;
184  apply (ex_intro ? ? (λu.u));
185  split;
186   [ intro; assumption
187   | intros; assumption
188   ]
189 qed.
190
191 lemma cut:
192  ∀F1,F2,F3:SP.
193   realized (simp F1 F2) → realized (simp F2 F3) → realized (simp F1 F3).
194  intros;
195  elim H; clear H;
196  elim H1; clear H1;
197  simplify in a a1;
198  apply (ex_intro ? ? (λx.a1 (a x)));
199  simplify;
200  simplify in H2 H;
201  elim H2; clear H2;
202  elim H; clear H;
203  split;
204   [ intro; apply (H2 (H1 H))
205   | intros; apply (H4 ? (H3 ? H))
206   ]
207 qed.
208
209 lemma and_i:
210  ∀F1,F2,F3:SP.
211   realized (simp F1 F2) → realized (simp F1 F3) → realized (simp F1 (sand F2 F3)).
212  intros;
213  elim H; clear H;
214  elim H1; clear H1;
215  simplify in a a1 ⊢ %;
216  apply (ex_intro ? ? (λu.〈a u, a1 u〉));
217  simplify in H2; cases H2; clear H2;
218  simplify in H; cases H; clear H;
219  split;
220   [ intro; split; [ apply (H1 H) | apply (H2 H) ] 
221   | intros;
222     split;
223      [ simplify; apply H3; assumption
224      | simplify; apply H4; assumption
225      ]
226   ]
227 qed.
228
229 (* Many more rules and examples missing, but trivial. *)