forked from DeepSpec/InteractionTrees
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadmeExample.v
41 lines (34 loc) · 1.16 KB
/
ReadmeExample.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
(* Example demonstrating the core features of the library. *)
From ITree Require Import
ITree ITreeFacts.
Import ITreeNotations.
(* Custom effects *)
Variant inputE : Type -> Type :=
| Input : inputE nat.
(* Effectful programs *)
Definition echo : itree inputE nat
:= x <- trigger Input ;;
Ret x.
(* Effect handlers *)
Definition handler {E} (n : nat)
: inputE ~> itree E
:= fun _ e => match e with
| Input => Ret n
end.
(* Interpreters *)
Definition echoed (n : nat)
: itree void1 nat
:= interp (handler n) echo.
(* Equational reasoning *)
Theorem echoed_id : forall n, echoed n ≈ Ret n.
Proof.
intros n. (* echoed n *)
unfold echoed, echo. (* ≈ interp (handler n) (x <- trigger Input ;; Ret x) *)
rewrite interp_bind. (* ≈ x <- interp (handler n) Input ;; interp (handler n) (Ret x) *)
rewrite interp_trigger.
(* ≈ x <- handler n _ Input ;; interp (handler n) (Ret x) *)
cbn. (* ≈ x <- Ret n ;; interp (handler n) (Ret x) *)
rewrite bind_ret_l. (* ≈ interp (handler n) (Ret n) *)
rewrite interp_ret. (* ≈ Ret n *)
reflexivity.
Qed.