Debian Codegraph @

test-suite/success/TCbacktrack.v

raw
1 (* Set Typeclasses Unique Instances *)
2 (** This lets typeclass search assume that instance heads are unique,
3 so if one matches no other need to be tried,
4 avoiding backtracking (even in unique solutions mode)
5 This is on a class-by-class basis.
6 *)
7
8 (* Non unique *)
9 Class B.
10 Class A.
11 Set Typeclasses Unique Instances.
12 (* Unique *)
13 Class D.
14 Class C (A : Type) := c : A.
15
16 #[export] Hint Mode C +.
17 Fail Definition test := c.
18
19 Unset Typeclasses Unique Instances.
20 #[export] Instance cN1 : B -> D -> C nat := fun _ _ => 0.
21 #[export] Instance cN2 : A -> D -> C nat := fun _ _ => 1.
22 #[export] Instance cB : B -> C bool := fun _ => true.
23
24 #[export] Instance Copt : forall A, C A -> C (option A) := fun A _ => None.
25
26 Set Typeclasses Debug.
27
28 Set Typeclasses Unique Solutions.
29 (** This forces typeclass resolution to fail if at least two solutions
30 exist to a given set of constraints. This is a global setting.
31 For constraints involving assumed unique instances, it will not fail
32 if two such instances could apply, however it will fail if two different
33 instances of a unique class could apply.
34 *)
35 Succeed Definition foo (d d' : D) (b b' : B) (a' a'' : A) := c : nat.
36 Definition foo (d d' : D) (b b' : B) (a' : A) := c : nat.
37
38 Succeed Definition foo' (b b' : B) := _ : B.
39 Unset Typeclasses Unique Solutions.
40 Definition foo' (b b' : B) := _ : B.
41
42 Set Typeclasses Unique Solutions.
43 Definition foo'' (d d' : D) := _ : D.
44
45 (** Cut backtracking *)
46 Module BacktrackGreenCut.
47 Unset Typeclasses Unique Solutions.
48 Class C (A : Type) := c : A.
49
50 Class D (A : Type) : Type := { c_of_d :: C A }.
51
52 #[export] Instance D1 : D unit.
53 Admitted.
54
55 #[export] Instance D2 : D unit.
56 Admitted.
57
58 (** Two instances of D unit, but when searching for [C unit], no
59 backtracking on the second instance should be needed except
60 in dependent cases. Check by adding an unresolvable constraint.
61 *)
62
63 Parameter f : D unit -> C bool -> True.
64 Fail Definition foo := f _ _.
65
66 Fail Definition foo' := let y := _ : D unit in let x := _ : C bool in f _ x.
67
68 Unset Typeclasses Strict Resolution.
69 Class Transitive (A : Type) := { trans : True }.
70 Class PreOrder (A : Type) := { preorder_trans :: Transitive A }.
71 Class PartialOrder (A : Type) := { partialorder_trans :: Transitive A }.
72 Class PartialOrder' (A : Type) := { partialorder_trans' :: Transitive A }.
73
74 #[export] Instance: PreOrder nat. Admitted.
75 #[export] Instance: PartialOrder nat. Admitted.
76
77 Class NoInst (A : Type) := {}.
78
79 Parameter foo : forall `{ T : Transitive nat } `{ NoInst (let x:=@trans _ T in nat) }, nat.
80
81 Fail Definition bar := foo.
82
83
84 End BacktrackGreenCut.