forked from MetaCoq/metacoq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathETyping.v
122 lines (97 loc) · 3.24 KB
/
ETyping.v
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
(* Distributed under the terms of the MIT license. *)
From Coq Require Import Bool String List Program BinPos Compare_dec Omega.
From MetaCoq.Template Require Import config utils AstUtils.
From MetaCoq.Erasure Require Import EAst EAstUtils EInduction ELiftSubst.
Require Import String.
Local Open Scope string_scope.
Set Asymmetric Patterns.
(** * Typing derivations
*WIP*
Inductive relations for reduction, conversion and typing of CIC terms.
*)
(** ** Environment lookup *)
Definition global_decl_ident d :=
match d with
| ConstantDecl id _ => id
| InductiveDecl id _ => id
end.
Fixpoint lookup_env (Σ : global_declarations) (id : ident) : option global_decl :=
match Σ with
| nil => None
| hd :: tl =>
if ident_eq id (global_decl_ident hd) then Some hd
else lookup_env tl id
end.
Definition declared_constant (Σ : global_declarations) (id : ident) decl : Prop :=
lookup_env Σ id = Some (ConstantDecl id decl).
Definition declared_minductive Σ mind decl :=
lookup_env Σ mind = Some (InductiveDecl mind decl).
Definition declared_inductive Σ mdecl ind decl :=
declared_minductive Σ (inductive_mind ind) mdecl /\
List.nth_error mdecl.(ind_bodies) (inductive_ind ind) = Some decl.
Definition declared_constructor Σ mdecl idecl cstr cdecl : Prop :=
declared_inductive Σ mdecl (fst cstr) idecl /\
List.nth_error idecl.(ind_ctors) (snd cstr) = Some cdecl.
Definition declared_projection Σ mdecl idecl (proj : projection) pdecl : Prop :=
declared_inductive Σ mdecl (fst (fst proj)) idecl /\
List.nth_error idecl.(ind_projs) (snd proj) = Some pdecl.
(** ** Reduction *)
(** *** Helper functions for reduction *)
Definition fix_subst (l : mfixpoint term) :=
let fix aux n :=
match n with
| 0 => []
| S n => tFix l n :: aux n
end
in aux (List.length l).
Definition unfold_fix (mfix : mfixpoint term) (idx : nat) :=
match List.nth_error mfix idx with
| Some d => Some (d.(rarg), subst0 (fix_subst mfix) d.(dbody))
| None => None
end.
Definition cofix_subst (l : mfixpoint term) :=
let fix aux n :=
match n with
| 0 => []
| S n => tCoFix l n :: aux n
end
in aux (List.length l).
Definition unfold_cofix (mfix : mfixpoint term) (idx : nat) :=
match List.nth_error mfix idx with
| Some d => Some (d.(rarg), subst0 (cofix_subst mfix) d.(dbody))
| None => None
end.
Definition is_constructor n ts :=
match List.nth_error ts n with
| Some a =>
let (f, a) := decompose_app a in
match f with
| tConstruct _ _ => true
| _ => false
end
| None => false
end.
Definition is_constructor_or_box n ts :=
match List.nth_error ts n with
| Some tBox => true
| Some a =>
let (f, a) := decompose_app a in
match f with
| tConstruct _ _ => true
| _ => false
end
| None => false
end.
Lemma fix_subst_length mfix : #|fix_subst mfix| = #|mfix|.
Proof.
unfold fix_subst. generalize (tFix mfix). intros.
induction mfix; simpl; auto.
Qed.
Lemma cofix_subst_length mfix : #|cofix_subst mfix| = #|mfix|.
Proof.
unfold cofix_subst. generalize (tCoFix mfix). intros.
induction mfix; simpl; auto.
Qed.
Definition tDummy := tVar "".
Definition iota_red npar c args brs :=
(mkApps (snd (List.nth c brs (0, tDummy))) (List.skipn npar args)).