Skip to content

Commit 9c6e812

Browse files
author
Basile
committed
Moving things around between Checker and AstUtils.
1 parent baa1e0b commit 9c6e812

File tree

2 files changed

+111
-29
lines changed

2 files changed

+111
-29
lines changed

template-rocq/theories/AstUtils.v

Lines changed: 97 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -716,14 +716,106 @@ Definition fold_termM {M} `{Monad M} {Acc} (f : Acc -> term -> M Acc) (acc : Acc
716716

717717

718718
(** * Traversal functions with a context*)
719+
720+
721+
722+
Definition fix_decls (l : mfixpoint term) :=
723+
let fix aux acc ds :=
724+
match ds with
725+
| nil => acc
726+
| d :: ds => aux (vass d.(dname) d.(dtype) :: acc) ds
727+
end
728+
in aux [] l.
729+
730+
Section Lookups.
731+
Context (Σ : global_env).
732+
733+
Definition polymorphic_constraints u :=
734+
match u with
735+
| Monomorphic_ctx => ConstraintSet.empty
736+
| Polymorphic_ctx ctx => (AUContext.repr ctx).2.2
737+
end.
738+
739+
Definition lookup_constant_type cst u :=
740+
match lookup_env Σ cst with
741+
| Some (ConstantDecl {| cst_type := ty; cst_universes := uctx |}) =>
742+
Some (subst_instance u ty)
743+
| _ => None
744+
end.
745+
746+
Definition lookup_constant_type_cstrs cst u :=
747+
match lookup_env Σ cst with
748+
| Some (ConstantDecl {| cst_type := ty; cst_universes := uctx |}) =>
749+
let cstrs := polymorphic_constraints uctx in
750+
Some (subst_instance u ty, subst_instance_cstrs u cstrs)
751+
| _ => None
752+
end.
753+
754+
Definition lookup_ind_decl ind i :=
755+
match lookup_env Σ ind with
756+
| Some (InductiveDecl mdecl) =>
757+
match nth_error mdecl.(ind_bodies) i with
758+
| Some body => Some (mdecl, body)
759+
| None => None
760+
end
761+
| _ => None
762+
end.
763+
764+
Definition lookup_ind_type ind i (u : list Level.t) :=
765+
match lookup_ind_decl ind i with
766+
|None => None
767+
|Some res =>
768+
Some (subst_instance u (snd res).(ind_type))
769+
end.
770+
771+
Definition lookup_ind_type_cstrs ind i (u : list Level.t) :=
772+
match lookup_ind_decl ind i with
773+
|None => None
774+
|Some res =>
775+
let '(mib, body) := res in
776+
let uctx := mib.(ind_universes) in
777+
let cstrs := polymorphic_constraints uctx in
778+
Some (subst_instance u body.(ind_type), subst_instance_cstrs u cstrs)
779+
end.
780+
781+
Definition lookup_constructor_decl ind i k :=
782+
match lookup_ind_decl ind i with
783+
|None => None
784+
|Some res =>
785+
let '(mib, body) := res in
786+
match nth_error body.(ind_ctors) k with
787+
| Some cdecl => Some (mib, cdecl)
788+
| None => None
789+
end
790+
end.
791+
792+
Definition lookup_constructor_type ind i k u :=
793+
match lookup_constructor_decl ind i k with
794+
|None => None
795+
|Some res =>
796+
let '(mib, cdecl) := res in
797+
Some (subst0 (inds ind u mib.(ind_bodies)) (subst_instance u cdecl.(cstr_type)))
798+
end.
799+
800+
Definition lookup_constructor_type_cstrs ind i k u :=
801+
match lookup_constructor_decl ind i k with
802+
|None => None
803+
|Some res =>
804+
let '(mib, cdecl) := res in
805+
let cstrs := polymorphic_constraints mib.(ind_universes) in
806+
Some (subst0 (inds ind u mib.(ind_bodies)) (subst_instance u cdecl.(cstr_type)),
807+
subst_instance_cstrs u cstrs)
808+
end.
809+
End Lookups.
810+
719811
Definition rebuild_case_predicate_ctx_with_context (Σ : global_env) ind (p : predicate term) : context :=
720812
match lookup_ind_decl Σ (inductive_mind ind) (inductive_ind ind) with
721-
| TypeError _ => []
722-
| Checked (mib, oib) => case_predicate_context ind mib oib p
813+
| None => []
814+
| Some (mib, oib) => case_predicate_context ind mib oib p
723815
end.
724816

725817
Definition map_context_with_context (f : context -> term -> term) (c : context) Γ : context :=
726-
fold_left (fun acc decl => map_decl (f (Γ ,,, acc)) decl :: acc) (List.rev c) [].
818+
fold_left (fun acc decl => map_decl (f (Γ ,,, acc)) decl :: acc) (List.rev c) [].
727819

728820
Definition map_predicate_with_context (Σ : global_env) (f : context -> term -> term) Γ ind (p : predicate term) :=
729821
let pctx := rebuild_case_predicate_ctx_with_context Σ ind p in
@@ -735,8 +827,8 @@ Definition map_predicate_with_context (Σ : global_env) (f : context -> term ->
735827

736828
Definition rebuild_case_branch_ctx_with_context Σ ind i p br :=
737829
match lookup_constructor_decl Σ (inductive_mind ind) (inductive_ind ind) i with
738-
| TypeError _ => []
739-
| Checked (mib, cdecl) => case_branch_context ind mib cdecl p br
830+
| None => []
831+
| Some (mib, cdecl) => case_branch_context ind mib cdecl p br
740832
end.
741833

742834
Definition map_case_branch_with_context Σ ind i (f : context -> term -> term) Γ p br :=

template-rocq/theories/Checker.v

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ Inductive type_error :=
5050
| NotEnoughFuel (n : nat)
5151
| NotSupported (s : string).
5252

53+
54+
5355
Definition string_of_type_error (e : type_error) : string :=
5456
match e with
5557
| UnboundRel n => "Unboound rel " ^ string_of_nat n
@@ -106,27 +108,23 @@ Section Lookups.
106108
end.
107109

108110
Definition lookup_constant_type cst u :=
109-
match lookup_env Σ cst with
110-
| Some (ConstantDecl {| cst_type := ty; cst_universes := uctx |}) =>
111-
ret (subst_instance u ty)
111+
match lookup_constant_type Σ cst u with
112+
| Some res =>
113+
ret res
112114
| _ => raise (UndeclaredConstant cst)
113115
end.
114116

115117
Definition lookup_constant_type_cstrs cst u :=
116-
match lookup_env Σ cst with
117-
| Some (ConstantDecl {| cst_type := ty; cst_universes := uctx |}) =>
118-
let cstrs := polymorphic_constraints uctx in
119-
ret (subst_instance u ty, subst_instance_cstrs u cstrs)
120-
| _ => raise (UndeclaredConstant cst)
118+
match lookup_constant_type_cstrs Σ cst u with
119+
| Some res =>
120+
ret res
121+
| _ => raise (UndeclaredConstant cst)
121122
end.
122123

123124
Definition lookup_ind_decl ind i :=
124-
match lookup_env Σ ind with
125-
| Some (InductiveDecl mdecl) =>
126-
match nth_error mdecl.(ind_bodies) i with
127-
| Some body => ret (mdecl, body)
128-
| None => raise (UndeclaredInductive (mkInd ind i))
129-
end
125+
match lookup_ind_decl Σ ind i with
126+
| Some res =>
127+
ret res
130128
| _ => raise (UndeclaredInductive (mkInd ind i))
131129
end.
132130

@@ -262,21 +260,13 @@ Section Reduce.
262260
res <- reduce_stack Γ n c [] ;;
263261
ret (zip res).
264262

265-
Definition fix_decls (l : mfixpoint term) :=
266-
let fix aux acc ds :=
267-
match ds with
268-
| nil => acc
269-
| d :: ds => aux (vass d.(dname) d.(dtype) :: acc) ds
270-
end
271-
in aux [] l.
272-
273263
Fixpoint reduce_opt Γ n c :=
274264
match n with
275265
| 0 => None
276266
| S n =>
277267
match reduce_stack_term Γ n c with
278268
| Some c' =>
279-
Some (map_constr_with_binders
269+
Some (map_term_with_context Σ
280270
(fun Γ t => match reduce_opt Γ n t with
281271
| Some t => t
282272
| None => t end) Γ c')
@@ -717,7 +707,7 @@ Section Typecheck.
717707
(** TODO check branches *)
718708
let '(ind, u, args) := indargs in
719709
if eq_inductive ind ci.(ci_ind) then
720-
let pctx := rebuild_case_predicate_ctx Σ ind p in
710+
let pctx := rebuild_case_predicate_ctx_with_context Σ ind p in
721711
let ptm := it_mkLambda_or_LetIn pctx p.(preturn) in
722712
ret (tApp ptm (List.skipn ci.(ci_npar) args ++ [c]))
723713
else

0 commit comments

Comments
 (0)