Debian Codegraph @

src/Util/CPSNotations.v

raw
1 Require Import Crypto.Util.Notations.
2 Local Set Universe Polymorphism.
3
4 (** [x <- f ; C] encodes a call to function [f] with [C] as the
5 continuation. In [C], [x] refers to the output of [f]. *)
6
7 Declare Scope cps_scope.
8 Delimit Scope cps_scope with cps.
9
10 (* [cpscall] is a marker to get Coq to print code using this notation only when it was actually used *)
11 Definition cpscall {R} (f:forall{T}(continuation:R->T),T) {T} (continuation:R->T)
12 := @f T continuation.
13 Notation "x' <- v ; C" := (cpscall v%cps (fun x' => C%cps)) : cps_scope.
14
15 (** A value of type [~>R] accepts a continuation that takes an
16 argument of type [R]. It is meant to be used in [Definition] and
17 such; for example:
18 <<
19 Definition add_cps (a b:nat) : ~> nat.
20 >>
21 The return type of the continuation is not yet specified; every
22 [~> R] is universally quantified over the possible return types
23 of the continuations that it can be applied to.
24 *)
25 Notation "~> R" := (forall T (_:R->T), T) : type_scope.
26
27 (** The type [A ~> R] contains functions that takes an argument of
28 type [A] and pass a value of type [R] to the continuation. Functions
29 that take multiple arguments can be encoded as [A -> B ~> C] or [A
30 ~> B ~>C] -- the first form requires both arguments to be specified
31 before its output can be CPS-bound, the latter must be bound once it
32 is partially applied to one argument. *)
33 Notation "A ~> R" := (A -> ~>R) : type_scope.
34
35 (* [cpsreturn] is a marker to get Coq to print loop notations before a [return] *)
36 Definition cpsreturn {T} (x:T) := x.
37 (** [return x] passes [x] to the continuation implicit in the previous notations. *)
38 Notation "'return' x" := (cpsreturn (fun T (continuation:_->T) => continuation x)) : cps_scope.
39
40 Definition cpsbind {A B} (v:~> A) (f:A ~> B) : ~> B
41 := fun T k => (a <- v; fa <- f a; k fa)%cps.
42 Notation "x' <--- v ; C" := (cpsbind v%cps (fun x' => C%cps)) : cps_scope.
43
44 Definition cps_option_bind {A B} (v:~> option A) (f:A ~> option B) : ~> option B
45 := cpsbind v (fun x' T k => match x' with Some x' => f x' T k | None => k None end).
46 Notation "x' <-- v ; C" := (cps_option_bind v%cps (fun x' => C%cps)) : cps_scope.
47
48 Module CPSBindNotations.
49 Notation "x' <- v ; C" := (cpsbind v%cps (fun x' => C%cps)) : cps_scope.
50 End CPSBindNotations.