Debian Codegraph @

tactics/hipattern.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 open Pp
12 open CErrors
13 open Util
14 open Names
15 open Constr
16 open Termops
17 open EConstr
18 open Inductiveops
19 open Constr_matching
20 open Rocqlib
21 open Declarations
22 open Context.Rel.Declaration
23
24 module RelDecl = Context.Rel.Declaration
25
26 (* I implemented the following functions which test whether a term t
27 is an inductive but non-recursive type, a general conjunction, a
28 general disjunction, or a type with no constructors.
29
30 They are more general than matching with or_term, and_term, etc,
31 since they do not depend on the name of the type. Hence, they
32 also work on ad-hoc disjunctions introduced by the user.
33
34 -- Eduardo (6/8/97). *)
35
36 type 'a matching_function = Environ.env -> Evd.evar_map -> EConstr.constr -> 'a option
37
38 type testing_function = Environ.env -> Evd.evar_map -> EConstr.constr -> bool
39
40 let mkmeta n = Nameops.make_ident "X" (Some n)
41 let meta1 = mkmeta 1
42 let meta2 = mkmeta 2
43
44 let match_with_non_recursive_type env sigma t =
45 match EConstr.kind sigma t with
46 | App _ ->
47 let (hdapp,args) = decompose_app_list sigma t in
48 (match EConstr.kind sigma hdapp with
49 | Ind (ind,u) ->
50 if (Environ.lookup_mind (fst ind) env).mind_finite == CoFinite then
51 Some (hdapp,args)
52 else
53 None
54 | _ -> None)
55 | _ -> None
56
57 let is_non_recursive_type env sigma t = Option.has_some (match_with_non_recursive_type env sigma t)
58
59 (* Test dependencies *)
60
61 (* NB: we consider also the let-in case in the following function,
62 since they may appear in types of inductive constructors (see #2629) *)
63
64 let rec has_nodep_prod_after n env sigma c =
65 match EConstr.kind sigma c with
66 | Prod (_,_,b) | LetIn (_,_,_,b) ->
67 ( n>0 || Vars.noccurn sigma 1 b)
68 && (has_nodep_prod_after (n-1) env sigma b)
69 | _ -> true
70
71 let has_nodep_prod env sigma c = has_nodep_prod_after 0 env sigma c
72
73 (* A general conjunctive type is a non-recursive with-no-indices inductive
74 type with only one constructor and no dependencies between argument;
75 it is strict if it has the form
76 "Inductive I A1 ... An := C (_:A1) ... (_:An)" *)
77
78 (* style: None = record; Some false = conjunction; Some true = strict conj *)
79
80 let is_strict_conjunction = function
81 | Some true -> true
82 | _ -> false
83
84 let is_lax_conjunction = function
85 | Some false -> true
86 | _ -> false
87
88 (* whd_beta normalize the types of arguments in a product *)
89 let rec whd_beta_prod env sigma c = match EConstr.kind sigma c with
90 | Prod (n,t,c) -> mkProd (n,Reductionops.whd_beta env sigma t,whd_beta_prod env sigma c)
91 | LetIn (n,d,t,c) -> mkLetIn (n,d,t,whd_beta_prod env sigma c)
92 | _ -> c
93
94 let match_with_one_constructor env sigma style onlybinary allow_rec t =
95 let (hdapp,args) = decompose_app_list sigma t in
96 let res = match EConstr.kind sigma hdapp with
97 | Ind ind ->
98 let (mib,mip) = Inductive.lookup_mind_specif env (fst ind) in
99 if Int.equal (Array.length mip.mind_consnames) 1
100 && (allow_rec || not (mis_is_recursive mip))
101 && (Int.equal mip.mind_nrealargs 0)
102 then
103 if is_strict_conjunction style (* strict conjunction *) then
104 let (ctx, _) = mip.mind_nf_lc.(0) in
105 let ctx = List.skipn (Context.Rel.length mib.mind_params_ctxt) (List.rev ctx) in
106 if
107 (* Constructor has a type of the form
108 c : forall (a_0 ... a_n : Type) (x_0 : A_0) ... (x_n : A_n). T **)
109 List.for_all
110 (fun decl -> let c = RelDecl.get_type decl in
111 is_local_assum decl &&
112 Constr.isRel c &&
113 Int.equal (Constr.destRel c) mib.mind_nparams) ctx
114 then
115 Some (hdapp,args)
116 else None
117 else
118 let ctx, cty = mip.mind_nf_lc.(0) in
119 let cty = EConstr.of_constr (Term.it_mkProd_or_LetIn cty ctx) in
120 let ctyp = whd_beta_prod env sigma
121 (Termops.prod_applist_decls sigma (Context.Rel.length mib.mind_params_ctxt) cty args) in
122 let cargs = List.map RelDecl.get_type (EConstr.prod_decls sigma ctyp) in
123 if not (is_lax_conjunction style) || has_nodep_prod env sigma ctyp then
124 (* Record or non strict conjunction *)
125 Some (hdapp,List.rev cargs)
126 else
127 None
128 else
129 None
130 | _ -> None in
131 match res with
132 | Some (hdapp, args) when not onlybinary -> res
133 | Some (hdapp, [_; _]) -> res
134 | _ -> None
135
136 let match_with_conjunction ?(strict=false) ?(onlybinary=false) env sigma t =
137 match_with_one_constructor env sigma (Some strict) onlybinary false t
138
139 let match_with_record env sigma t =
140 match_with_one_constructor env sigma None false false t
141
142 let is_conjunction ?(strict=false) ?(onlybinary=false) env sigma t =
143 Option.has_some (match_with_conjunction env sigma ~strict ~onlybinary t)
144
145 let is_record env sigma t =
146 Option.has_some (match_with_record env sigma t)
147
148 let match_with_tuple env sigma t =
149 let t = match_with_one_constructor env sigma None false true t in
150 Option.map (fun (hd,l) ->
151 let ind, _ = destInd sigma hd in
152 let (mib,mip) = Inductive.lookup_mind_specif env ind in
153 let isrec = mis_is_recursive mip in
154 (hd,l,isrec)) t
155
156 let is_tuple env sigma t =
157 Option.has_some (match_with_tuple env sigma t)
158
159 (* A general disjunction type is a non-recursive with-no-indices inductive
160 type with of which all constructors have a single argument;
161 it is strict if it has the form
162 "Inductive I A1 ... An := C1 (_:A1) | ... | Cn : (_:An)" *)
163
164 let test_strict_disjunction (mib, mip) =
165 let n = List.length mib.mind_params_ctxt in
166 let check i (ctx, _) = match List.skipn n (List.rev ctx) with
167 | [LocalAssum (_, c)] -> Constr.isRel c && Int.equal (Constr.destRel c) (n - i)
168 | _ -> false
169 in
170 Array.for_all_i check 0 mip.mind_nf_lc
171
172 let match_with_disjunction ?(strict=false) ?(onlybinary=false) env sigma t =
173 let (hdapp,args) = decompose_app_list sigma t in
174 let res = match EConstr.kind sigma hdapp with
175 | Ind (ind,u) ->
176 let car = constructors_nrealargs env ind in
177 let (mib,mip) = Inductive.lookup_mind_specif env ind in
178 if Array.for_all (fun ar -> Int.equal ar 1) car
179 && not (mis_is_recursive mip)
180 && (Int.equal mip.mind_nrealargs 0)
181 then
182 if strict then
183 if test_strict_disjunction (mib, mip) then
184 Some (hdapp,args)
185 else
186 None
187 else
188 let map (ctx, cty) =
189 let ar = EConstr.of_constr (Term.it_mkProd_or_LetIn cty ctx) in
190 pi2 (destProd sigma (prod_applist sigma ar args))
191 in
192 let cargs = Array.map map mip.mind_nf_lc in
193 Some (hdapp,Array.to_list cargs)
194 else
195 None
196 | _ -> None in
197 match res with
198 | Some (hdapp,args) when not onlybinary -> res
199 | Some (hdapp,[_; _]) -> res
200 | _ -> None
201
202 let is_disjunction ?(strict=false) ?(onlybinary=false) env sigma t =
203 Option.has_some (match_with_disjunction ~strict ~onlybinary env sigma t)
204
205 (* An empty type is an inductive type, possible with indices, that has no
206 constructors *)
207
208 let match_with_empty_type env sigma t =
209 let (hdapp,args) = decompose_app sigma t in
210 match EConstr.kind sigma hdapp with
211 | Ind (ind, _) ->
212 let (mib,mip) = Inductive.lookup_mind_specif env ind in
213 let nconstr = Array.length mip.mind_consnames in
214 if Int.equal nconstr 0 then Some hdapp else None
215 | _ -> None
216
217 let is_empty_type env sigma t = Option.has_some (match_with_empty_type env sigma t)
218
219 (* This filters inductive types with one constructor with no arguments;
220 Parameters and indices are allowed *)
221
222 let match_with_unit_or_eq_type env sigma t =
223 let (hdapp,args) = decompose_app sigma t in
224 match EConstr.kind sigma hdapp with
225 | Ind (ind , _) ->
226 let (mib,mip) = Inductive.lookup_mind_specif env ind in
227 let nconstr = Array.length mip.mind_consnames in
228 if Int.equal nconstr 1 && Int.equal mip.mind_consnrealargs.(0) 0 then
229 Some hdapp
230 else
231 None
232 | _ -> None
233
234 let is_unit_or_eq_type env sigma t = Option.has_some (match_with_unit_or_eq_type env sigma t)
235
236 (* A unit type is an inductive type with no indices but possibly
237 (useless) parameters, and that has no arguments in its unique
238 constructor *)
239
240 let is_unit_type env sigma t =
241 match match_with_conjunction env sigma t with
242 | Some (_,[]) -> true
243 | _ -> false
244
245 (* Checks if a given term is an application of an
246 inductive binary relation R, so that R has only one constructor
247 establishing its reflexivity. *)
248
249 type equation_kind =
250 | MonomorphicLeibnizEq of constr * constr
251 | PolymorphicLeibnizEq of constr * constr * constr
252 | HeterogenousEq of constr * constr * constr * constr
253
254 exception NoEquationFound
255
256 open Pattern
257
258 let mkPRel n = PRel n
259 let mkPApp f args = PApp (f, Array.of_list args)
260 let mkPHole = PMeta None
261 let mkPProd id c1 c2 = PProd (Name (Id.of_string id), c1, c2)
262 let mkPArrow c1 c2 = PProd (Anonymous, c1, c2)
263 let mkPPatVar id = PMeta (Some (Id.of_string id))
264 let mkPRef r = PRef (lib_ref r)
265 let mkPAppRef r args = mkPApp (mkPRef r) args
266
267 (** forall x : _, _ x x *)
268 let rocq_refl_leibniz1_pattern =
269 mkPProd "x" mkPHole (mkPApp mkPHole [mkPRel 1; mkPRel 1])
270
271 (** forall A:_, forall x:A, _ A x x *)
272 let rocq_refl_leibniz2_pattern =
273 mkPProd "A" mkPHole (mkPProd "x" (mkPRel 1)
274 (mkPApp mkPHole [mkPRel 2; mkPRel 1; mkPRel 1]))
275
276 (** forall A:_, forall x:A, _ A x A x *)
277 let rocq_refl_jm_pattern =
278 mkPProd "A" mkPHole (mkPProd "x" (mkPRel 1)
279 (mkPApp mkPHole [mkPRel 2; mkPRel 1; mkPRel 2; mkPRel 1]))
280
281 let match_with_equation env sigma t =
282 if not (isApp sigma t) then raise NoEquationFound;
283 let (hdapp,args) = destApp sigma t in
284 match EConstr.kind sigma hdapp with
285 | Ind (ind,u) ->
286 (try
287 let gr = GlobRef.IndRef ind in
288 if Rocqlib.check_ref "core.eq.type" gr then
289 Some (build_rocq_eq_data()),hdapp,
290 PolymorphicLeibnizEq(args.(0),args.(1),args.(2))
291 else if Rocqlib.check_ref "core.identity.type" gr then
292 Some (build_rocq_identity_data()),hdapp,
293 PolymorphicLeibnizEq(args.(0),args.(1),args.(2))
294 else if Rocqlib.check_ref "core.JMeq.type" gr then
295 Some (build_rocq_jmeq_data()),hdapp,
296 HeterogenousEq(args.(0),args.(1),args.(2),args.(3))
297 else
298 let (mib,mip) = Inductive.lookup_mind_specif env ind in
299 let constr_types = mip.mind_nf_lc in
300 let nconstr = Array.length mip.mind_consnames in
301 if Int.equal nconstr 1 then
302 let (ctx, cty) = constr_types.(0) in
303 let cty = EConstr.of_constr (Term.it_mkProd_or_LetIn cty ctx) in
304 if is_matching env sigma rocq_refl_leibniz1_pattern cty then
305 None, hdapp, MonomorphicLeibnizEq(args.(0),args.(1))
306 else if is_matching env sigma rocq_refl_leibniz2_pattern cty then
307 None, hdapp, PolymorphicLeibnizEq(args.(0),args.(1),args.(2))
308 else if is_matching env sigma rocq_refl_jm_pattern cty then
309 None, hdapp, HeterogenousEq(args.(0),args.(1),args.(2),args.(3))
310 else raise NoEquationFound
311 else raise NoEquationFound
312 with UserError _ ->
313 raise NoEquationFound)
314 | _ -> raise NoEquationFound
315
316 (* Note: An "equality type" is any type with a single argument-free
317 constructor: it captures eq, eq_dep, JMeq, eq_true, etc. but also
318 True/unit which is the degenerate equality type (isomorphic to ()=());
319 in particular, True/unit are provable by "reflexivity" *)
320
321 let is_inductive_equality env ind =
322 let (mib,mip) = Inductive.lookup_mind_specif env ind in
323 let nconstr = Array.length mip.mind_consnames in
324 Int.equal nconstr 1 && Int.equal (constructor_nrealargs env (ind,1)) 0
325
326 let match_with_equality_type env sigma t =
327 let (hdapp,args) = decompose_app_list sigma t in
328 match EConstr.kind sigma hdapp with
329 | Ind (ind,_) when is_inductive_equality env ind -> Some (hdapp,args)
330 | _ -> None
331
332 let is_equality_type env sigma t = Option.has_some (match_with_equality_type env sigma t)
333
334 (* Arrows/Implication/Negation *)
335
336 (** X1 -> X2 **)
337 let rocq_arrow_pattern = mkPArrow (mkPPatVar "X1") (mkPPatVar "X2")
338
339 let match_arrow_pattern env sigma t =
340 let result = matches env sigma rocq_arrow_pattern t in
341 match Id.Map.bindings result with
342 | [(m1,arg);(m2,mind)] ->
343 assert (Id.equal m1 meta1 && Id.equal m2 meta2); (arg, mind)
344 | _ -> anomaly (Pp.str "Incorrect pattern matching.")
345
346 let match_with_imp_term env sigma c =
347 match EConstr.kind sigma c with
348 | Prod (_,a,b) when Vars.noccurn sigma 1 b -> Some (a,b)
349 | _ -> None
350
351 let is_imp_term env sigma c = Option.has_some (match_with_imp_term env sigma c)
352
353 let match_with_nottype env sigma t =
354 try
355 let (arg,mind) = match_arrow_pattern env sigma t in
356 if is_empty_type env sigma mind then Some (mind,arg) else None
357 with PatternMatchingFailure -> None
358
359 let is_nottype env sigma t = Option.has_some (match_with_nottype env sigma t)
360
361 (* Forall *)
362
363 let match_with_forall_term env sigma c =
364 match EConstr.kind sigma c with
365 | Prod (nam,a,b) -> Some (nam,a,b)
366 | _ -> None
367
368 let is_forall_term env sigma c = Option.has_some (match_with_forall_term env sigma c)
369
370 let match_with_nodep_ind env sigma t =
371 let (hdapp,args) = decompose_app_list sigma t in
372 match EConstr.kind sigma hdapp with
373 | Ind (ind, _) ->
374 let (mib,mip) = Inductive.lookup_mind_specif env ind in
375 if Array.length (mib.mind_packets)>1 then None else
376 let nodep_constr (ctx, cty) =
377 let c = EConstr.of_constr (Term.it_mkProd_or_LetIn cty ctx) in
378 has_nodep_prod_after (Context.Rel.length mib.mind_params_ctxt) env sigma c in
379 if Array.for_all nodep_constr mip.mind_nf_lc then
380 let params=
381 if Int.equal mip.mind_nrealargs 0 then args else
382 fst (List.chop mib.mind_nparams args) in
383 Some (hdapp,params,mip.mind_nrealargs)
384 else
385 None
386 | _ -> None
387
388 let is_nodep_ind env sigma t = Option.has_some (match_with_nodep_ind env sigma t)
389
390 let match_with_sigma_type env sigma t =
391 let (hdapp,args) = decompose_app_list sigma t in
392 match EConstr.kind sigma hdapp with
393 | Ind (ind, _) ->
394 let (mib,mip) = Inductive.lookup_mind_specif env ind in
395 if Int.equal (Array.length (mib.mind_packets)) 1
396 && (Int.equal mip.mind_nrealargs 0)
397 && (Int.equal (Array.length mip.mind_consnames)1)
398 && has_nodep_prod_after (Context.Rel.length mib.mind_params_ctxt + 1) env sigma
399 (let (ctx, cty) = mip.mind_nf_lc.(0) in EConstr.of_constr (Term.it_mkProd_or_LetIn cty ctx))
400 then
401 (*allowing only 1 existential*)
402 Some (hdapp,args)
403 else
404 None
405 | _ -> None
406
407 let is_sigma_type env sigma t = Option.has_some (match_with_sigma_type env sigma t)
408
409 (*** Equality *)
410
411 let match_eq env sigma eqn (data, hetero) =
412 match data () with
413 | exception Rocqlib.NotFoundRef _ -> None
414 | data ->
415 let ref = data.Rocqlib.eq in
416 let kind = match EConstr.kind sigma eqn with
417 | App (c, [|t; x; y|]) ->
418 if not hetero && isRefX env sigma ref c then Some (PolymorphicLeibnizEq (t, x, y))
419 else None
420 | App (c, [|t; x; t'; x'|]) ->
421 if hetero && isRefX env sigma ref c then Some (HeterogenousEq (t, x, t', x'))
422 else None
423 | _ -> None
424 in
425 kind |> Option.map @@ fun kind -> data, kind
426
427 let equalities =
428 [build_rocq_eq_data, false;
429 build_rocq_jmeq_data, true;
430 build_rocq_identity_data, false]
431
432 let find_eq_data env sigma eqn = (* fails with PatternMatchingFailure *)
433 let d,k = match List.find_map (match_eq env sigma eqn) equalities with
434 | None -> raise PatternMatchingFailure
435 | Some v -> v
436 in
437 let hd,u = destInd sigma (fst (destApp sigma eqn)) in
438 d,u,k
439
440 let extract_eq_args env sigma = function
441 | MonomorphicLeibnizEq (e1,e2) ->
442 let t = Retyping.get_type_of env sigma e1 in (t,e1,e2)
443 | PolymorphicLeibnizEq (t,e1,e2) -> (t,e1,e2)
444 | HeterogenousEq (t1,e1,t2,e2) ->
445 if Reductionops.is_conv env sigma t1 t2 then (t1,e1,e2)
446 else raise PatternMatchingFailure
447
448 let find_eq_data_decompose env sigma eqn =
449 let (lbeq,u,eq_args) = find_eq_data env sigma eqn in
450 (lbeq,u,extract_eq_args env sigma eq_args)
451
452 let find_this_eq_data_decompose env sigma eqn =
453 let (lbeq,u,eq_args) =
454 try
455 find_eq_data env sigma eqn
456 with PatternMatchingFailure ->
457 user_err (str "No primitive equality found.") in
458 let eq_args =
459 try extract_eq_args env sigma eq_args
460 with PatternMatchingFailure ->
461 user_err Pp.(str "Don't know what to do with JMeq on arguments not of same type.") in
462 (lbeq,u,eq_args)
463
464 (*** Sigma-types *)
465
466 let match_sigma_data env sigma ex =
467 match EConstr.kind sigma ex with
468 | App (f, [| a; p; car; cdr |]) when is_lib_ref env sigma "core.sig.intro" f ->
469 build_sigma (), (snd (destConstruct sigma f), a, p, car, cdr)
470 | App (f, [| a; p; car; cdr |]) when is_lib_ref env sigma "core.sigT.intro" f ->
471 build_sigma_type (), (snd (destConstruct sigma f), a, p, car, cdr)
472 | _ -> raise PatternMatchingFailure
473
474 let find_sigma_data_decompose env ex = (* fails with PatternMatchingFailure *)
475 match_sigma_data env ex
476
477 (* Pattern "(sig ?1 ?2)" *)
478 let rocq_sig_pattern =
479 lazy (mkPAppRef "core.sig.type" [mkPPatVar "X1"; mkPPatVar "X2"])
480
481 let match_sigma env sigma t =
482 match Id.Map.bindings (matches env sigma (Lazy.force rocq_sig_pattern) t) with
483 | [(_,a); (_,p)] -> (a,p)
484 | _ -> anomaly (Pp.str "Unexpected pattern.")
485
486 let is_matching_sigma env sigma t = is_matching env sigma (Lazy.force rocq_sig_pattern) t
487
488 (*** Decidable equalities *)
489
490 (* The expected form of the goal for the tactic Decide Equality *)
491
492 (* Pattern "{<?1>x=y}+{~(<?1>x=y)}" *)
493 (* i.e. "(sumbool (eq ?1 x y) ~(eq ?1 x y))" *)
494
495 let rocq_eqdec ~sum ~rev =
496 lazy (
497 let eqn = mkPAppRef "core.eq.type" (List.map mkPPatVar ["X1"; "X2"; "X3"]) in
498 let args = [eqn; mkPAppRef "core.not.type" [eqn]] in
499 let args = if rev then List.rev args else args in
500 mkPAppRef sum args
501 )
502
503 let sumbool_type = "core.sumbool.type"
504 let or_type = "core.or.type"
505
506 (** [{ ?X2 = ?X3 :> ?X1 } + { ~ ?X2 = ?X3 :> ?X1 }] *)
507 let rocq_eqdec_inf_pattern = rocq_eqdec ~sum:sumbool_type ~rev:false
508
509 (** [{ ~ ?X2 = ?X3 :> ?X1 } + { ?X2 = ?X3 :> ?X1 }] *)
510 let rocq_eqdec_inf_rev_pattern = rocq_eqdec ~sum:sumbool_type ~rev:true
511
512 (** %rocq_or_ref (?X2 = ?X3 :> ?X1) (~ ?X2 = ?X3 :> ?X1) *)
513 let rocq_eqdec_pattern = rocq_eqdec ~sum:or_type ~rev:false
514
515 (** %rocq_or_ref (~ ?X2 = ?X3 :> ?X1) (?X2 = ?X3 :> ?X1) *)
516 let rocq_eqdec_rev_pattern = rocq_eqdec ~sum:or_type ~rev:true
517
518 let match_eqdec env sigma t =
519 let eqonleft,op,subst =
520 try true,sumbool_type,matches env sigma (Lazy.force rocq_eqdec_inf_pattern) t
521 with PatternMatchingFailure ->
522 try false,sumbool_type,matches env sigma (Lazy.force rocq_eqdec_inf_rev_pattern) t
523 with PatternMatchingFailure ->
524 try true,or_type,matches env sigma (Lazy.force rocq_eqdec_pattern) t
525 with PatternMatchingFailure ->
526 false,or_type,matches env sigma (Lazy.force rocq_eqdec_rev_pattern) t in
527 match Id.Map.bindings subst with
528 | [(_,typ);(_,c1);(_,c2)] ->
529 eqonleft, lib_ref op, c1, c2, typ
530 | _ -> anomaly (Pp.str "Unexpected pattern.")
531
532 (* Patterns "~ ?" and "? -> False" *)
533 let rocq_not_pattern = lazy (mkPAppRef "core.not.type" [mkPHole])
534 let rocq_imp_False_pattern = lazy (mkPArrow mkPHole (mkPRef "core.False.type"))
535
536 let is_matching_not env sigma t = is_matching env sigma (Lazy.force rocq_not_pattern) t
537 let is_matching_imp_False env sigma t = is_matching env sigma (Lazy.force rocq_imp_False_pattern) t
538
539 (* Remark: patterns that have references to the standard library must
540 be evaluated lazily (i.e. at the time they are used, not a the time
541 coqtop starts) *)
542
543 let is_homogeneous_relation ?loc env sigma rel =
544 let relty = Retyping.get_type_of env sigma rel in
545 let error () =
546 user_err ?loc
547 (Printer.pr_econstr_env env sigma rel ++ str " is not a homogeneous binary relation.")
548 in
549 let ctx, ar =
550 try Reductionops.whd_decompose_prod_n env sigma 2 relty
551 with Invalid_argument _ -> error () in
552 match ctx, EConstr.kind sigma ar with
553 | [(_,t); (_,u)], Sort s
554 when Sorts.is_prop (ESorts.kind sigma s) && Reductionops.is_conv env sigma t u -> t
555 | _, _ -> error ()