Debian Codegraph @

src/Util/ErrorT.v

raw
1 Require Import Crypto.Util.Notations.
2
3 Local Set Boolean Equality Schemes.
4 Local Set Decidable Equality Schemes.
5 Local Set Implicit Arguments.
6 Inductive ErrorT {ErrT T} :=
7 | Success (v : T)
8 | Error (msg : ErrT).
9
10 Global Arguments ErrorT : clear implicits.
11 Declare Scope error_scope.
12 Delimit Scope error_scope with error.
13 Bind Scope error_scope with ErrorT.
14
15 Definition invert_result {ErrT T} (v : ErrorT ErrT T)
16 := match v return match v with Success _ => T | _ => ErrT end with
17 | Success v => v
18 | Error msg => msg
19 end.
20
21 Definition bind {A B ErrT} (x : ErrorT ErrT A) (k : A -> ErrorT ErrT B) : ErrorT ErrT B
22 := match x with
23 | Success v => k v
24 | Error msg => Error msg
25 end.
26
27 Definition map2 {ErrT1 ErrT2 A B} (f : A -> B) (fe : ErrT1 -> ErrT2) (x : ErrorT ErrT1 A) : ErrorT ErrT2 B
28 := match x with
29 | Success v => Success (f v)
30 | Error e => Error (fe e)
31 end.
32
33 Definition map {ErrT A B} (f : A -> B) : ErrorT ErrT A -> ErrorT ErrT B
34 := map2 f id.
35
36 Definition map_error {ErrT1 ErrT2 A} (fe : ErrT1 -> ErrT2) : ErrorT ErrT1 A -> ErrorT ErrT2 A
37 := map2 id fe.
38
39 Definition error_bind {ErrT1 ErrT2 A} (x : ErrorT ErrT1 A) (k : ErrT1 -> ErrorT ErrT2 A) : ErrorT ErrT2 A
40 := match x with
41 | Success v => Success v
42 | Error msg => k msg
43 end.
44
45 Notation "x <- y ; f" := (bind y (fun x => f%error)) : error_scope.
46
47 (** ** Equality for [ErrorT] *)
48 Section ErrorT.
49 Local Notation ErrorT_code u v
50 := (match u, v with
51 | Success u', Success v'
52 | Error u', Error v'
53 => u' = v'
54 | Success _, _
55 | Error _, _
56 => False
57 end).
58
59 (** *** Equality of [ErrorT] is a [match] *)
60 Definition path_ErrorT {A B} (u v : ErrorT A B) (p : ErrorT_code u v)
61 : u = v.
62 Proof. destruct u, v; first [ apply f_equal | exfalso ]; exact p. Defined.
63
64 (** *** Equivalence of equality of [ErrorT] with [ErrorT_code] *)
65 Definition unpath_ErrorT {A B} {u v : ErrorT A B} (p : u = v)
66 : ErrorT_code u v.
67 Proof. subst v; destruct u; reflexivity. Defined.
68
69 Definition path_ErrorT_iff {A B}
70 (u v : @ErrorT A B)
71 : u = v <-> ErrorT_code u v.
72 Proof.
73 split; [ apply unpath_ErrorT | apply path_ErrorT ].
74 Defined.
75
76 (** *** Eta-expansion of [@eq (ErrorT _ _)] *)
77 Definition path_ErrorT_eta {A B} {u v : @ErrorT A B} (p : u = v)
78 : p = path_ErrorT u v (unpath_ErrorT p).
79 Proof. destruct u, p; reflexivity. Defined.
80
81 (** *** Induction principle for [@eq (ErrorT _ _)] *)
82 Definition path_ErrorT_rect {A B} {u v : @ErrorT A B} (P : u = v -> Type)
83 (f : forall p, P (path_ErrorT u v p))
84 : forall p, P p.
85 Proof. intro p; specialize (f (unpath_ErrorT p)); destruct u, p; exact f. Defined.
86 Definition path_ErrorT_rec {A B u v} (P : u = v :> @ErrorT A B -> Set) := path_ErrorT_rect P.
87 Definition path_ErrorT_ind {A B u v} (P : u = v :> @ErrorT A B -> Prop) := path_ErrorT_rec P.
88 End ErrorT.
89
90 (** ** Useful Tactics *)
91 (** *** [inversion_ErrorT] *)
92 Ltac induction_path_ErrorT H :=
93 induction H as [H] using path_ErrorT_rect;
94 try match type of H with
95 | False => exfalso; exact H
96 end.
97 Ltac inversion_ErrorT_step :=
98 match goal with
99 | [ H : Success _ = Success _ |- _ ]
100 => induction_path_ErrorT H
101 | [ H : Success _ = Error _ |- _ ]
102 => induction_path_ErrorT H
103 | [ H : Error _ = Success _ |- _ ]
104 => induction_path_ErrorT H
105 | [ H : Error _ = Error _ |- _ ]
106 => induction_path_ErrorT H
107 end.
108 Ltac inversion_ErrorT := repeat inversion_ErrorT_step.