fact
stringlengths 14
11.5k
| type
stringclasses 22
values | library
stringclasses 7
values | imports
listlengths 0
7
| filename
stringclasses 136
values | symbolic_name
stringlengths 1
36
| docstring
stringclasses 1
value |
|---|---|---|---|---|---|---|
ioE : Type -> Type := | Input : ioE (list nat) (** Ask for a list of [nat] from the environment. *) | Output : list nat -> ioE unit (** Send a list of [nat]. *) . (** Events are wrapped as ITrees using [ITree.trigger], and composed using monadic operations [bind] and [ret]. *) (** Read some input, and echo it back, appending [1] to it. *)
|
Inductive
|
examples
|
[
"From ITree Require Import\n Simple."
] |
examples/IntroductionSolutions.v
|
ioE
| |
write_one : itree ioE unit := xs <- ITree.trigger Input;; ITree.trigger (Output (xs ++ [1])). (** We can _interpret_ interaction trees by giving semantics to their events individually, as a _handler_: a function of type [forall R, ioE R -> M R] for some monad [M]. *) (** The handler [handle_io] below responds to every [Input] event with [[0]], and appends any [Output] to a global log. Here the target monad is [M := stateT (list nat) (itree void1)], - [stateT] is the state monad transformer, that type unfolds to [M R := list nat -> itree void1 (list nat * R)]; - [void1] is the empty event (so the resulting ITree can trigger no event). *) Compute Monads.stateT (list nat) (itree void1) unit. Print void1.
|
Definition
|
examples
|
[
"From ITree Require Import\n Simple."
] |
examples/IntroductionSolutions.v
|
write_one
| |
handle_io : forall R, ioE R -> Monads.stateT (list nat) (itree void1) R := fun R e log => match e with | Input => ret (log, [0]) | Output o => ret (log ++ o, tt) end. (** [interp] lifts any handler into an _interpreter_, of type [forall R, itree ioE R -> M R]. *)
|
Definition
|
examples
|
[
"From ITree Require Import\n Simple."
] |
examples/IntroductionSolutions.v
|
handle_io
| |
interp_io : forall R, itree ioE R -> itree void1 (list nat * R) := fun R t => Monads.run_stateT (interp handle_io t) []. (** We can now interpret [write_one]. *)
|
Definition
|
examples
|
[
"From ITree Require Import\n Simple."
] |
examples/IntroductionSolutions.v
|
interp_io
| |
interpreted_write_one : itree void1 (list nat * unit) := interp_io _ write_one. (** Intuitively, [interp_io] replaces every [ITree.trigger] in the definition of [write_one] with [handle_io]: [[ interpreted_write_one = xs <- handle_io _ Input;; handle_io _ (Output (xs ++ [1])) ]] We can prove such a lemma in a more restricted setting, where [handle_io] targets some monad of the form [itree F], rather than [T (itree F)] (above, [T := stateT (list nat)]). (The library is currently missing some theory about the monads we can instantiate [interp] with.) The proof of [interp_write_one] will require us to rewrite an expression under a binder---i.e., on the right side of a [;;]. The [rewrite] tactic will fail in this situation; instead, we can use [setoid_rewrite], which works under binders. *)
|
Definition
|
examples
|
[
"From ITree Require Import\n Simple."
] |
examples/IntroductionSolutions.v
|
interpreted_write_one
| |
interp_write_one F (handle_io : forall R, ioE R -> itree F R) : interp handle_io write_one ≈ (xs <- handle_io _ Input;; handle_io _ (Output (xs ++ [1]))). Proof. unfold write_one. (* Use lemmas from [ITree.Simple] ([theories/Simple.v]). *) (* ADMITTED *) rewrite interp_bind. rewrite interp_trigger. setoid_rewrite interp_trigger. reflexivity. Qed. (* /ADMITTED *) (** An [itree void1] is a computation which can either return a value, or loop infinitely. Since Coq is total, [interpreted_write_one] will not be reduced naively. Instead, we can force computation with fuel, using [burn]: *) Compute (burn 100 interpreted_write_one). (** * General recursion with interaction trees *) (** One application of [interp] is to define and reason about recursive functions as ITrees. In this section, we will demonstrate: - [rec] - equational reasoning using [≈] ([\approx]) *) (** In this file, we won't use external events, so we will use this empty event type [void1]. *)
|
Lemma
|
examples
|
[
"From ITree Require Import\n Simple."
] |
examples/IntroductionSolutions.v
|
interp_write_one
| |
E := void1. (** ** Factorial *) (** This is the Coq specification -- the usual mathematical definition. *)
|
Definition
|
examples
|
[
"From ITree Require Import\n Simple."
] |
examples/IntroductionSolutions.v
|
E
| |
factorial_spec (n : nat) : nat := match n with | 0 => 1 | S m => n * factorial_spec m end. (** The generic [rec] interface of the library's [Interp] module can be used to define a single recursive function. The more general [mrec] (from which [rec] is defined) allows multiple, mutually recursive definitions. The argument of [rec] is an interaction tree with an event type [callE A B] to represent "recursive calls", with input [A] and output [B]. The function [call : A -> itree _ B] can be used to make such calls. In this case, since [factorial : nat -> nat], we use [callE nat nat]. *)
|
Fixpoint
|
examples
|
[
"From ITree Require Import\n Simple."
] |
examples/IntroductionSolutions.v
|
factorial_spec
| |
fact_body (x : nat) : itree (callE nat nat +' E) nat := match x with | 0 => Ret 1 | S m => y <- call m ;; (* Recursively compute [y := m!] *) Ret (x * y) end. (** The factorial function itself is defined as an ITree by "tying the knot" using [rec]. *)
|
Definition
|
examples
|
[
"From ITree Require Import\n Simple."
] |
examples/IntroductionSolutions.v
|
fact_body
| |
factorial (n : nat) : itree E nat := rec fact_body n. (** *** Evaluation *) (** Contrary to definitions by [Fixpoint], there are no restrictions on the arguments of recursive calls: [rec] may thus produce diverging computations, and Coq will not naively reduce [factorial 5]. We can force the computation with fuel, e.g., using [burn]... *) Compute (burn 100 (factorial 5)). (** ... or with tactics, such as [tau_steps], which removes all taus from the left-hand side of an [≈] equation. *)
|
Definition
|
examples
|
[
"From ITree Require Import\n Simple."
] |
examples/IntroductionSolutions.v
|
factorial
| |
fact_5 : factorial 5 ≈ Ret 120. Proof. tau_steps. reflexivity. Qed. (** *** Alternative notation *) (** An equivalent definition with a [rec-fix] notation looking like [fix], where the recursive call can be given a more specific name. *)
|
Example
|
examples
|
[
"From ITree Require Import\n Simple."
] |
examples/IntroductionSolutions.v
|
fact_5
| |
factorial' : nat -> itree E nat := rec-fix fact x := match x with | 0 => Ret 1 | S m => y <- fact m ;; Ret (x * y) end. (** These two definitions are definitionally equal. *)
|
Definition
|
examples
|
[
"From ITree Require Import\n Simple."
] |
examples/IntroductionSolutions.v
|
factorial'
| |
factorial_same : factorial = factorial'. Proof. reflexivity. Qed. (** *** Correctness *) (** [rec] is actually a special version of [interp], which replaces every [call] in [fact_body] with [factorial] itself. *)
|
Lemma
|
examples
|
[
"From ITree Require Import\n Simple."
] |
examples/IntroductionSolutions.v
|
factorial_same
| |
unfold_factorial : forall x, factorial x ≈ match x with | 0 => Ret 1 | S m => y <- factorial m ;; Ret (x * y) end. Proof. intros x. unfold factorial. (* ADMITTED *) rewrite rec_as_interp; unfold fact_body at 2. destruct x. - rewrite interp_ret. reflexivity. - rewrite interp_bind. rewrite interp_recursive_call. setoid_rewrite interp_ret. reflexivity. Qed. (* /ADMITTED *) (** We can prove that the ITrees version [factorial] is "equivalent" to the [factorial_spec] version. The proof goes by induction on [n] and uses only rewriting -- no coinduction necessary. Here, we detail each step of rewriting to illustrate the use of the equational theory, which is mostly applications of the monad laws and the properties of [rec]. In this proof, we do all of the rewriting steps explicitly. *)
|
Lemma
|
examples
|
[
"From ITree Require Import\n Simple."
] |
examples/IntroductionSolutions.v
|
unfold_factorial
| |
factorial_correct : forall n, factorial n ≈ Ret (factorial_spec n). Proof. intros n. (* ADMITTED *) induction n as [ | n' IH ]. - (* n = 0 *) rewrite unfold_factorial. reflexivity. - (* n = S n' *) rewrite unfold_factorial. rewrite IH. (* Induction hypothesis *) rewrite bind_ret. simpl. reflexivity. Qed. (* /ADMITTED *) (* HIDE *) (** The tactics [tau_steps] and [autorewrite with itree] offer a little automation to simplify monadic expressions. *)
|
Lemma
|
examples
|
[
"From ITree Require Import\n Simple."
] |
examples/IntroductionSolutions.v
|
factorial_correct
| |
factorial_correct' : forall n, factorial n ≈ Ret (factorial_spec n). Proof. intros n. unfold factorial. induction n as [ | n' IH ]. - (* n = 0 *) tau_steps. (* Just compute away. *) reflexivity. - (* n = S n' *) rewrite rec_as_interp. unfold fact_body at 2. autorewrite with itree. rewrite IH. (* Induction hypothesis *) autorewrite with itree. reflexivity. Qed. (* /HIDE *) (** ** Fibonacci *) (** Carry out the analogous proof of correctness for the Fibonacci function, whose naturally recursive coq definition is given below. *)
|
Lemma
|
examples
|
[
"From ITree Require Import\n Simple."
] |
examples/IntroductionSolutions.v
|
factorial_correct'
| |
fib_spec (n : nat) : nat := match n with | 0 => 0 | S n' => match n' with | 0 => 1 | S n'' => fib_spec n'' + fib_spec n' end end.
|
Fixpoint
|
examples
|
[
"From ITree Require Import\n Simple."
] |
examples/IntroductionSolutions.v
|
fib_spec
| |
fib_body : nat -> itree (callE nat nat +' E) nat (* ADMITDEF *) := fun n => match n with | 0 => Ret 0 | S n' => match n' with | 0 => Ret 1 | S n'' => y1 <- call n'' ;; y2 <- call n' ;; Ret (y1 + y2) end end. (* /ADMITDEF *)
|
Definition
|
examples
|
[
"From ITree Require Import\n Simple."
] |
examples/IntroductionSolutions.v
|
fib_body
| |
fib n : itree E nat := rec fib_body n.
|
Definition
|
examples
|
[
"From ITree Require Import\n Simple."
] |
examples/IntroductionSolutions.v
|
fib
| |
fib_3_6 : mapT fib [4;5;6] ≈ Ret [3; 5; 8]. Proof. (* Use [tau_steps] to compute. *) (* ADMITTED *) tau_steps. reflexivity. Qed. (* /ADMITTED *) (** Since fib uses two recursive calls, we need to strengthen the induction hypothesis. One way to do that is to prove the property for all [m <= n]. You may find the following two lemmas useful at the start of each case. [[ Nat.le_0_r : forall n : nat, n <= 0 <-> n = 0 Nat.le_succ_r : forall n m : nat, n <= S m <-> n <= m \/ n = S m ]] *)
|
Example
|
examples
|
[
"From ITree Require Import\n Simple."
] |
examples/IntroductionSolutions.v
|
fib_3_6
| |
fib_correct_aux : forall n m, m <= n -> fib m ≈ Ret (fib_spec m). Proof. intros n. unfold fib. induction n as [ | n' IH ]; intros. - (* n = 0 *) apply Nat.le_0_r in H. subst m. (* ADMIT *) rewrite rec_as_interp. simpl. rewrite interp_ret. (* alternatively, [tau_steps], or [autorewrite with itree] *) reflexivity. (* /ADMIT *) - (* n = S n' *) apply Nat.le_succ_r in H. (* ADMIT *) destruct H. + apply IH. auto. + rewrite rec_as_interp. subst m. simpl. destruct n' as [ | n'' ]. * rewrite interp_ret. reflexivity. * autorewrite with itree. rewrite IH. 2: lia. autorewrite with itree. rewrite IH. 2: lia. autorewrite with itree. reflexivity. (* /ADMIT *) (* ADMITTED *) Qed. (* /ADMITTED. *) (** The final correctness result follows. *)
|
Lemma
|
examples
|
[
"From ITree Require Import\n Simple."
] |
examples/IntroductionSolutions.v
|
fib_correct_aux
| |
fib_correct : forall n, fib n ≈ Ret (fib_spec n). Proof. (* ADMITTED *) intros n. eapply fib_correct_aux. reflexivity. Qed. (* /ADMITTED *) (** ** Logarithm *) (** An example of a function which is not structurally recursive. [log_ b n]: logarithm of [n] in base [b]. Specification: [log_ b (b ^ y) ≈ Ret y] when [1 < b]. (Note that this only constrains a very small subset of inputs, and in fact our solution diverges for some of them.) *)
|
Lemma
|
examples
|
[
"From ITree Require Import\n Simple."
] |
examples/IntroductionSolutions.v
|
fib_correct
| |
log (b : nat) : nat -> itree E nat (* ADMITDEF *) := rec-fix log_b n := if n <=? 1 then Ret O else y <- log_b (n / b) ;; Ret (S y). (* /ADMITDEF *)
|
Definition
|
examples
|
[
"From ITree Require Import\n Simple."
] |
examples/IntroductionSolutions.v
|
log
| |
log_2_64 : log 2 (2 ^ 6) ≈ Ret 6. Proof. (* ADMITTED *) tau_steps. reflexivity. Qed. (* /ADMITTED *) (** These lemmas take care of the boring arithmetic. *)
|
Example
|
examples
|
[
"From ITree Require Import\n Simple."
] |
examples/IntroductionSolutions.v
|
log_2_64
| |
log_correct_helper : forall b y, 1 < b -> (b * b ^ y <=? 1) = false. Proof. intros. apply Nat.leb_gt. apply Nat.lt_1_mul_pos; auto. apply Nat.neq_0_lt_0. intro. apply (Nat.pow_nonzero b y); lia. Qed.
|
Lemma
|
examples
|
[
"From ITree Require Import\n Simple."
] |
examples/IntroductionSolutions.v
|
log_correct_helper
| |
log_correct_helper2 : forall b y, 1 < b -> (b * b ^ y / b) = (b ^ y). Proof. intros; rewrite Nat.mul_comm, Nat.div_mul; lia. Qed.
|
Lemma
|
examples
|
[
"From ITree Require Import\n Simple."
] |
examples/IntroductionSolutions.v
|
log_correct_helper2
| |
log_correct : forall b y, 1 < b -> log b (b ^ y) ≈ Ret y. Proof. intros b y H. (* ADMITTED *) unfold log, rec_fix. induction y. - rewrite rec_as_interp; cbn. autorewrite with itree. reflexivity. - rewrite rec_as_interp; cbn. (* (b * b ^ y <=? 1) = false *) rewrite log_correct_helper by auto. autorewrite with itree. (* (b * b ^ y / b) = (b ^ y)*) rewrite log_correct_helper2 by auto. rewrite IHy. autorewrite with itree. reflexivity. Qed. (* /ADMITTED *)
|
Lemma
|
examples
|
[
"From ITree Require Import\n Simple."
] |
examples/IntroductionSolutions.v
|
log_correct
| |
stateE (S:Type) : Type -> Type := | Get : stateE S S | Put : S -> stateE S unit. (* We can define an interpretation of [stateE] events into itrees with no events as follows. Note that we split out the "node functor", which is parameterized by the interpreter on the recursive calls, separating it from the CoFixpoint. This structure mirrors the way that we define predicates below. The ITrees library defines more elaborate versions of these interpreters that work with multiple events types. *)
|
Variant
|
examples
|
[
"From Coq Require Import\n Morphisms.",
"From Paco Require Import paco.",
"From ExtLib Require Import\n Monads.",
"From Paco Require Import paco."
] |
examples/ITreePredicatesExample.v
|
stateE
| |
stateT (S:Type) (M:Type -> Type) (R:Type) := S -> M (S * R)%type.
|
Definition
|
examples
|
[
"From Coq Require Import\n Morphisms.",
"From Paco Require Import paco.",
"From ExtLib Require Import\n Monads.",
"From Paco Require Import paco."
] |
examples/ITreePredicatesExample.v
|
stateT
| |
interpret_stateF (S:Type) {R} (rec : itree (stateE S) R -> stateT S (itree void1) R) (t : itree (stateE S) R) : stateT S (itree void1) R := fun s => match observe t with | RetF r => ret (s, r) | TauF t => Tau (rec t s) | VisF Get k => Tau (rec (k s) s) | VisF (Put s') k => Tau (rec (k tt) s') end. CoFixpoint interpret_state {S R:Type} (t:itree (stateE S) R) : stateT S (itree void1) R := interpret_stateF interpret_state t.
|
Definition
|
examples
|
[
"From Coq Require Import\n Morphisms.",
"From Paco Require Import paco.",
"From ExtLib Require Import\n Monads.",
"From Paco Require Import paco."
] |
examples/ITreePredicatesExample.v
|
interpret_stateF
| |
unfold_interpret_state : forall {S R} (t : itree (stateE S) R) (s:S), observe (interpret_state t s) = observe (interpret_stateF interpret_state t s). Proof. reflexivity. Qed. (* Rewriting ---------------------------------------------------------------- *) (* To enable rewriting under the [interpret_state] function, we need to show that it respects ≅. In practice, this means instantiating the typeclass [Proper] so that the setoid rewrite tactics recognize that interpret_state is such a morphism. These proofs are pretty straightforward, but there are a few gotchas: - We use [red] to unfold the definitions of Proper and respectful. - We typically want to use [rewrite itree_eta] to change [t] into [(observe t)] but we don't want to do that before introducing the coinductive hypothesis because the extra "observes" get in the way of later using the CIH after we've made some progress in the proof. - We need to use UIP (via the [ITree.Axioms.ddestruction] tactic, which itself uses the stdlib's [dependent destruction]) to get equality of the projected components of the VisF constructor. *)
|
Lemma
|
examples
|
[
"From Coq Require Import\n Morphisms.",
"From Paco Require Import paco.",
"From ExtLib Require Import\n Monads.",
"From Paco Require Import paco."
] |
examples/ITreePredicatesExample.v
|
unfold_interpret_state
| |
proper_interpret_state {S R} : Proper ((@eq_itree (stateE S) R _ eq) ==> (@eq S) ==> (@eq_itree void1 (S * R) _ eq)) interpret_state. Proof. ginit. gcofix CIH. intros x y H0 x2 y0 H1. rewrite (itree_eta (interpret_state x x2)). rewrite (itree_eta (interpret_state y y0)). rewrite !unfold_interpret_state. subst. punfold H0. repeat red in H0. unfold interpret_stateF. destruct (observe x); inv H0; try discriminate; pclearbot; simpl; try (gstep; constructor; eauto with paco; fail). ddestruction. destruct e; gstep; econstructor; eauto with paco itree. Qed.
|
Instance
|
examples
|
[
"From Coq Require Import\n Morphisms.",
"From Paco Require Import paco.",
"From ExtLib Require Import\n Monads.",
"From Paco Require Import paco."
] |
examples/ITreePredicatesExample.v
|
proper_interpret_state
| |
NoGetsF {S R} (rec : itree (stateE S) R -> Prop) : itreeF (stateE S) R (itree (stateE S) R) -> Prop := | isRet : forall (r:R), NoGetsF rec (RetF r) | isTau : forall t, rec t -> NoGetsF rec (TauF t) | isPut : forall (k : unit -> itree (stateE S) R), forall (s:S), rec (k tt) -> NoGetsF rec (VisF (Put s) k). Global Hint Constructors NoGetsF : core. (* Next, we lift the [NoGetsF] predicate through the [itree] [go] constructor to obtain a predicate transformer on [itree stateE R]. This is achieved just by asking composing NoGetsF with [observe]. *) (* SAZ: n.b. for some reason, we have adopted the [Foo_] notational convention for the predicate transformer whose greatest fixpoint is [Foo]. Is this good? SAZ: Actually, I see that we're inconsistent between [eq_itree] and [eutt] with these naming conventions. *)
|
Variant
|
examples
|
[
"From Coq Require Import\n Morphisms.",
"From Paco Require Import paco.",
"From ExtLib Require Import\n Monads.",
"From Paco Require Import paco."
] |
examples/ITreePredicatesExample.v
|
NoGetsF
| |
NoGets_ {S R} (rec : itree (stateE S) R -> Prop) (t : itree (stateE S) R) : Prop := NoGetsF rec (observe t). (* Next, we need to prove that [NoGets_] is a monotone function on relations, which means that paco can take its greatest fixpoint. Monotonicity of [NoGets_] depends on monotonicity of [NoGetsF]. Fortunately, paco provides the tactic [pmonauto] which almost always discharges these proofs. It also provides the definitions monotone1, monotone2, etc. for monotonicity at different arities of relations. *) (* SAZ: Arguably, the LE fact should be defined via some named property. In the Coq Relationclasses library, there is a definition of subrelation, which is the binary version of this. It might be more uniform to have subrelation1, subrelation2, subrelation3, etc. for different arities. SAZ: It's a bit of a wart that, due to NoGetsF transforming predicates on [itree (stateE S) R] to predicates on [itreeF (stateE S) (itree (stateE S) R)] that it isn't an instance of monotone1. *)
|
Definition
|
examples
|
[
"From Coq Require Import\n Morphisms.",
"From Paco Require Import paco.",
"From ExtLib Require Import\n Monads.",
"From Paco Require Import paco."
] |
examples/ITreePredicatesExample.v
|
NoGets_
| |
monotone_NoGetsF : forall {S R} t (r r' : itree (stateE S) R -> Prop) (IN: NoGetsF r t) (LE: forall y, r y -> r' y), NoGetsF r' t. Proof. pmonauto. Qed. (* SAZ: we need to do a couple of reductions to expose the structure of the lemma so that pmonauto can work. Note that [cbn] and [simple] don't work here because they don't unfold the definitions. *)
|
Lemma
|
examples
|
[
"From Coq Require Import\n Morphisms.",
"From Paco Require Import paco.",
"From ExtLib Require Import\n Monads.",
"From Paco Require Import paco."
] |
examples/ITreePredicatesExample.v
|
monotone_NoGetsF
| |
monotone_NoGets_ : forall {S R}, monotone1 (@NoGets_ S R). Proof. do 2 red. pmonauto. Qed. Global Hint Resolve monotone_NoGets_ : paco. (* Finally, we can define the [NoGets] predicate by simply applying paco1 starting from bot1 (the least prediate). We would use paco2 and bot2 for a binary relation, paco3 and bot3 for ternary, etc. *)
|
Lemma
|
examples
|
[
"From Coq Require Import\n Morphisms.",
"From Paco Require Import paco.",
"From ExtLib Require Import\n Monads.",
"From Paco Require Import paco."
] |
examples/ITreePredicatesExample.v
|
monotone_NoGets_
| |
NoGets {S R} : itree (stateE S) R -> Prop := paco1 NoGets_ bot1. (* Using a coinductive predicate -------------------------------------------- *) (* Now that we have defined [NoGets], we can use that predicate to do a coinductive proof. Intuitively, if we interpret an itree of type [itree (stateE S) R] that satisfies the [NoGets] predicate, it does not matter what initial state it runs in. To state this correctly, we have to "project away" the final state component that is produced by the monad, since the two final states might differ. Some notes about this proof: - In general, rewriting up to ≅ or ≈ can take place only under the "up to" paco context. This means that to do the [rewrite (itree_eta ...)] steps, we have to have done [pupto2_init] first. *)
|
Definition
|
examples
|
[
"From Coq Require Import\n Morphisms.",
"From Paco Require Import paco.",
"From ExtLib Require Import\n Monads.",
"From Paco Require Import paco."
] |
examples/ITreePredicatesExample.v
|
NoGets
| |
state_independent : forall {S R} (t:itree (stateE S) R) (H: NoGets t), forall s s', ('(s,x) <- interpret_state t s ;; ret x) ≅ ('(s,x) <- interpret_state t s' ;; ret x). Proof. intros S R. ginit. gcofix CIH. intros t H0 s s'. rewrite (itree_eta (interpret_state t s)). rewrite (itree_eta (interpret_state t s')). rewrite !unfold_interpret_state. unfold interpret_stateF. punfold H0. repeat red in H0. destruct (observe t); cbn. - rewrite !bind_ret_l. gstep. econstructor. eauto. - rewrite !bind_tau. gstep. econstructor. gbase. eapply CIH. inversion H0. subst. pclearbot. assumption. - destruct e; cbn. + (* e is Get, which is ruled out by the NoGets predicate *) inversion H0. + rewrite !bind_tau. gstep. econstructor. gbase. eapply CIH. inversion H0. ddestruction. pclearbot. assumption. Qed. (* More or less the same proof also works for any continuation [k] that ignores the state. This proof illustrates the use of paco2_mon -- monotonicity means that if we assume that [k (s, x) ≅ k (s', x))] then [k (s, x)] is related to [k (s', x)] at any "later" step of the cofixpoint. e.g. in the proof below we need them related at [r]. *)
|
Lemma
|
examples
|
[
"From Coq Require Import\n Morphisms.",
"From Paco Require Import paco.",
"From ExtLib Require Import\n Monads.",
"From Paco Require Import paco."
] |
examples/ITreePredicatesExample.v
|
state_independent
| |
state_independent_k : forall {S R U} (t:itree (stateE S) R) (H: NoGets t) (k: (S * R) -> itree void1 U) (INV: forall s s' x, k (s, x) ≅ k (s', x)), forall s s', (sx <- interpret_state t s ;; (k sx)) ≅ (sx <- interpret_state t s' ;; (k sx)). Proof. intros S R U. ginit. gcofix CIH. intros t H0 k INV s s'. rewrite (itree_eta (interpret_state t s)). rewrite (itree_eta (interpret_state t s')). rewrite !unfold_interpret_state. unfold interpret_stateF. punfold H0. repeat red in H0. destruct (observe t); cbn. - rewrite !bind_ret_l. gfinal. right. eapply paco2_mon_bot; eauto with paco. apply INV. - rewrite !bind_tau. gstep. econstructor. gbase. eapply CIH; auto. inversion H0. subst. pclearbot. assumption. - destruct e; cbn. + (* e is Get, which is ruled out by the NoGets predicate *) inversion H0. + rewrite !bind_tau. gstep. econstructor. gbase. eapply CIH; auto. inversion H0. ddestruction. pclearbot. assumption. Qed.
|
Lemma
|
examples
|
[
"From Coq Require Import\n Morphisms.",
"From Paco Require Import paco.",
"From ExtLib Require Import\n Monads.",
"From Paco Require Import paco."
] |
examples/ITreePredicatesExample.v
|
state_independent_k
| |
state_independent' : forall {S R} (t:itree (stateE S) R) (H: NoGets t), forall s s', ('(s,x) <- interpret_state t s ;; ret x) ≅ ('(s,x) <- interpret_state t s' ;; ret x). Proof. intros S R t H s s'. eapply state_independent_k; eauto. intros. reflexivity. Qed.
|
Theorem
|
examples
|
[
"From Coq Require Import\n Morphisms.",
"From Paco Require Import paco.",
"From ExtLib Require Import\n Monads.",
"From Paco Require Import paco."
] |
examples/ITreePredicatesExample.v
|
state_independent'
| |
term : Type := | Var : nat -> term (* DeBruijn indexed *) | App : term -> term -> term | Lam : term -> term .
|
Inductive
|
examples
|
[
"From Coq Require Import Arith.",
"From ExtLib.Structures Require Import\n Monad."
] |
examples/LC.v
|
term
| |
headvar : Type := | VVar : nat -> headvar | VApp : headvar -> value -> headvar with value : Type := | VHead : headvar -> value | VLam : term -> value .
|
Inductive
|
examples
|
[
"From Coq Require Import Arith.",
"From ExtLib.Structures Require Import\n Monad."
] |
examples/LC.v
|
headvar
| |
to_term (v : value) : term := match v with | VHead hv => hv_to_term hv | VLam t => Lam t end with hv_to_term (hv : headvar) : term := match hv with | VVar n => Var n | VApp hv v => App (hv_to_term hv) (to_term v) end.
|
Fixpoint
|
examples
|
[
"From Coq Require Import Arith.",
"From ExtLib.Structures Require Import\n Monad."
] |
examples/LC.v
|
to_term
| |
shift (n m : nat) (s : term) := match s with | Var p => if p <? m then Var (n + p) else Var p | App t1 t2 => App (shift n m t1) (shift n m t2) | Lam t => Lam (shift n (S m) t) end.
|
Fixpoint
|
examples
|
[
"From Coq Require Import Arith.",
"From ExtLib.Structures Require Import\n Monad."
] |
examples/LC.v
|
shift
| |
subst (n : nat) (s t : term) := match t with | Var m => if m <? n then Var m else if m =? n then shift n O s else Var (pred m) | App t1 t2 => App (subst n s t1) (subst n s t2) | Lam t => Lam (subst (S n) s t) end. (* big-step call-by-value *)
|
Fixpoint
|
examples
|
[
"From Coq Require Import Arith.",
"From ExtLib.Structures Require Import\n Monad."
] |
examples/LC.v
|
subst
| |
big_step : term -> itree void1 value := rec (fun t => match t with | Var n => ret (VHead (VVar n)) | App t1 t2 => t2' <- trigger (Call t2);; t1' <- trigger (Call t1);; match t1' with | VHead hv => ret (VHead (VApp hv t2')) | VLam t1'' => trigger (Call (subst O (to_term t2') t1'')) end | Lam t => ret (VLam t) end).
|
Definition
|
examples
|
[
"From Coq Require Import Arith.",
"From ExtLib.Structures Require Import\n Monad."
] |
examples/LC.v
|
big_step
| |
printE : Type -> Type := Print : string -> printE unit. (* A thread that loops, printing [s] forever. *)
|
Variant
|
examples
|
[
"From Coq Require Import\n String."
] |
examples/MultiThreadedPrinting.v
|
printE
| |
thread {E} `{printE -< E} (s:string) : itree E unit := ITree.forever (trigger (Print s)). (* Run three threads. *)
|
Definition
|
examples
|
[
"From Coq Require Import\n String."
] |
examples/MultiThreadedPrinting.v
|
thread
| |
main_thread : itree (spawnE printE +' printE) unit := spawn (thread "Thread 1") ;; spawn (thread "Thread 2") ;; spawn (thread "Thread 3").
|
Definition
|
examples
|
[
"From Coq Require Import\n String."
] |
examples/MultiThreadedPrinting.v
|
main_thread
| |
scheduled_thread : itree printE unit := run_spawn main_thread.
|
Definition
|
examples
|
[
"From Coq Require Import\n String."
] |
examples/MultiThreadedPrinting.v
|
scheduled_thread
| |
com : Type := | loop : com -> com (* Nondeterministically, continue or stop. *) | choose : com -> com -> com | skip : com | seq : com -> com -> com . Declare Scope com_scope. Infix ";;" := seq (at level 61, right associativity) : com_scope. Delimit Scope com_scope with com. Open Scope com_scope.
|
Inductive
|
examples
|
[
"From Coq Require Import\n Relations.",
"From Paco Require Import paco."
] |
examples/Nimp.v
|
com
| |
one_loop : com := loop skip.
|
Example
|
examples
|
[
"From Coq Require Import\n Relations.",
"From Paco Require Import paco."
] |
examples/Nimp.v
|
one_loop
| |
two_loops : com := loop (loop skip).
|
Example
|
examples
|
[
"From Coq Require Import\n Relations.",
"From Paco Require Import paco."
] |
examples/Nimp.v
|
two_loops
| |
loop_choose : com := loop (choose skip skip).
|
Example
|
examples
|
[
"From Coq Require Import\n Relations.",
"From Paco Require Import paco."
] |
examples/Nimp.v
|
loop_choose
| |
choose_loop : com := choose (loop skip) skip. (* Unlabeled small-step *)
|
Example
|
examples
|
[
"From Coq Require Import\n Relations.",
"From Paco Require Import paco."
] |
examples/Nimp.v
|
choose_loop
| |
step : relation com := | step_loop_stop c : loop c --> skip | step_loop_go c : loop c --> (c ;; loop c) | step_choose_l c1 c2 : choose c1 c2 --> c1 | step_choose_r c1 c2 : choose c1 c2 --> c2 | step_seq_go c1 c1' c2 : c1 --> c2 -> (c1 ;; c2) --> (c1' ;; c2) | step_seq_next c2 : (skip ;; c2) --> c2 where "x --> y" := (step x y).
|
Inductive
|
examples
|
[
"From Coq Require Import\n Relations.",
"From Paco Require Import paco."
] |
examples/Nimp.v
|
step
| |
infinite_steps (c : com) : Type := | more c' : step c c' -> infinite_steps c' -> infinite_steps c.
|
CoInductive
|
examples
|
[
"From Coq Require Import\n Relations.",
"From Paco Require Import paco."
] |
examples/Nimp.v
|
infinite_steps
| |
infinite_simple_loop : infinite_steps one_loop. Proof. cofix self. eapply more. { eapply step_loop_go. } eapply more. { eapply step_seq_next. } apply self. Qed.
|
Lemma
|
examples
|
[
"From Coq Require Import\n Relations.",
"From Paco Require Import paco."
] |
examples/Nimp.v
|
infinite_simple_loop
| |
label := tau | bit (b : bool).
|
Variant
|
examples
|
[
"From Coq Require Import\n Relations.",
"From Paco Require Import paco."
] |
examples/Nimp.v
|
label
| |
step : label -> relation com := | step_loop_stop c : loop c ! true --> skip | step_loop_go c : loop c ! false --> (c ;; loop c) | step_choose_l c1 c2 : choose c1 c2 ! true --> c1 | step_choose_r c1 c2 : choose c1 c2 ! false --> c2 | step_seq_go b c1 c1' c2 : c1 ? b --> c2 -> (c1 ;; c2) ? b --> (c1' ;; c2) | step_seq_next c2 : (skip ;; c2) --> c2 where "x --> y" := (step tau x y) and "x ! b --> y" := (step (bit b) x y) and "x ? b --> y" := (step b x y).
|
Inductive
|
examples
|
[
"From Coq Require Import\n Relations.",
"From Paco Require Import paco."
] |
examples/Nimp.v
|
step
| |
infinite_steps (c : com) : Type := | more b c' : step b c c' -> infinite_steps c' -> infinite_steps c.
|
CoInductive
|
examples
|
[
"From Coq Require Import\n Relations.",
"From Paco Require Import paco."
] |
examples/Nimp.v
|
infinite_steps
| |
infinite_simple_loop : infinite_steps one_loop. Proof. cofix self. eapply more. { eapply step_loop_go. } eapply more. { eapply step_seq_next. } apply self. Qed.
|
Lemma
|
examples
|
[
"From Coq Require Import\n Relations.",
"From Paco Require Import paco."
] |
examples/Nimp.v
|
infinite_simple_loop
| |
nd : Type -> Prop := | Or : nd bool.
|
Variant
|
examples
|
[
"From Coq Require Import\n Relations.",
"From Paco Require Import paco."
] |
examples/Nimp.v
|
nd
| |
or {R : Type} (t1 t2 : itree nd R) : itree nd R := Vis Or (fun b : bool => if b then t1 else t2). (* Flip a coin *)
|
Definition
|
examples
|
[
"From Coq Require Import\n Relations.",
"From Paco Require Import paco."
] |
examples/Nimp.v
|
or
| |
choice {E} `{nd -< E} : itree E bool := trigger Or.
|
Definition
|
examples
|
[
"From Coq Require Import\n Relations.",
"From Paco Require Import paco."
] |
examples/Nimp.v
|
choice
| |
eval_def : com -> itree (callE _ _ +' nd) unit := (fun (c : com) => match c with | loop c => (b <- choice;; if b : bool then Ret tt else (trigger (Call c);; trigger (Call (loop c))))%itree | choose c1 c2 => (b <- choice;; if b : bool then trigger (Call c1) else trigger (Call c2))%itree | (t1 ;; t2)%com => (trigger (Call t1);; trigger (Call t2))%itree | skip => Ret tt end ).
|
Definition
|
examples
|
[
"From Coq Require Import\n Relations.",
"From Paco Require Import paco."
] |
examples/Nimp.v
|
eval_def
| |
eval : com -> itree nd unit := rec eval_def. (* [itree] semantics of [one_loop]. *)
|
Definition
|
examples
|
[
"From Coq Require Import\n Relations.",
"From Paco Require Import paco."
] |
examples/Nimp.v
|
eval
| |
one_loop_tree : itree nd unit := rec (fun _ : unit => (* note: [or] is not allowed under [mfix]. *) b <- choice;; if b : bool then Ret tt else trigger (Call tt))%itree tt. Import Coq.Classes.Morphisms.
|
Definition
|
examples
|
[
"From Coq Require Import\n Relations.",
"From Paco Require Import paco."
] |
examples/Nimp.v
|
one_loop_tree
| |
eval_skip : rec eval_def skip ≈ Ret tt. Proof. rewrite rec_as_interp. cbn. rewrite interp_ret. reflexivity. Qed. (* SAZ: the [~] notation for eutt wasn't working here. *)
|
Lemma
|
examples
|
[
"From Coq Require Import\n Relations.",
"From Paco Require Import paco."
] |
examples/Nimp.v
|
eval_skip
| |
eval_one_loop : eval one_loop ≈ one_loop_tree. Proof. einit. ecofix CIH. edrop. setoid_rewrite rec_as_interp. setoid_rewrite interp_bind. setoid_rewrite interp_vis. setoid_rewrite tau_eutt. setoid_rewrite interp_ret. setoid_rewrite bind_bind. setoid_rewrite bind_ret_l. setoid_rewrite bind_vis. evis. intros. setoid_rewrite bind_ret_l. destruct v. - setoid_rewrite interp_ret. apply reflexivity. - setoid_rewrite interp_bind. setoid_rewrite interp_recursive_call. setoid_rewrite eval_skip. setoid_rewrite bind_ret_l. eauto with paco. Qed.
|
Lemma
|
examples
|
[
"From Coq Require Import\n Relations.",
"From Paco Require Import paco."
] |
examples/Nimp.v
|
eval_one_loop
| |
inputE : Type -> Prop := | Input : inputE nat. (* Effectful programs *)
|
Variant
|
examples
|
[
"From ITree Require Import\n ITree ITreeFacts."
] |
examples/ReadmeExample.v
|
inputE
| |
echo : itree inputE nat := x <- trigger Input ;; Ret x. (* Effect handlers *)
|
Definition
|
examples
|
[
"From ITree Require Import\n ITree ITreeFacts."
] |
examples/ReadmeExample.v
|
echo
| |
handler {E} (n : nat) : inputE ~> itree E := fun _ e => match e with | Input => Ret n end. (* Interpreters *)
|
Definition
|
examples
|
[
"From ITree Require Import\n ITree ITreeFacts."
] |
examples/ReadmeExample.v
|
handler
| |
echoed (n : nat) : itree void1 nat := interp (handler n) echo. (* Equational reasoning *)
|
Definition
|
examples
|
[
"From ITree Require Import\n ITree ITreeFacts."
] |
examples/ReadmeExample.v
|
echoed
| |
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.
|
Theorem
|
examples
|
[
"From ITree Require Import\n ITree ITreeFacts."
] |
examples/ReadmeExample.v
|
echoed_id
| |
typ := | Base | Arr (s:typ) (t:typ). Variable V : typ -> Type. (* PHOAS variables *)
|
Inductive
|
examples
|
[] |
examples/STLC.v
|
typ
| |
tm : typ -> Type := | Lit (n:nat) : tm Base | Var : forall (t:typ), V t -> tm t | App : forall t1 t2 (m1 : tm (Arr t1 t2)) (m2 : tm t1), tm t2 | Lam : forall t1 t2 (body : V t1 -> tm t2), tm (Arr t1 t2) | Opr : forall (m1 : tm Base) (m2 : tm Base), tm Base .
|
Inductive
|
examples
|
[] |
examples/STLC.v
|
tm
| |
open_tm (G : list typ) (u:typ) : Type := match G with | [] => tm u | t::ts => V t -> (open_tm ts u) end.
|
Fixpoint
|
examples
|
[] |
examples/STLC.v
|
open_tm
| |
Term (G : list typ) (u:typ) := forall (V : typ -> Type), open_tm V G u. Arguments Lit {V}. Arguments Var {V t}. Arguments App {V t1 t2}. Arguments Lam {V t1 t2}. Arguments Opr {V}.
|
Definition
|
examples
|
[] |
examples/STLC.v
|
Term
| |
denote_typ E (t:typ) : Type := match t with | Base => nat | Arr s t => (denote_typ E s) -> itree E (denote_typ E t) end.
|
Fixpoint
|
examples
|
[] |
examples/STLC.v
|
denote_typ
| |
denotation_tm_typ E (V:typ -> Type) (G : list typ) (u:typ) := match G with | [] => itree E (V u) | t::ts => (V t) -> denotation_tm_typ E V ts u end.
|
Fixpoint
|
examples
|
[] |
examples/STLC.v
|
denotation_tm_typ
| |
denote_closed_term {E} {u:typ} (m : tm (denote_typ E) u) : itree E (denote_typ E u) := match m with | Lit n => Ret n | Var x => Ret x | App m1 m2 => f <- (denote_closed_term m1) ;; x <- (denote_closed_term m2) ;; ans <- f x ;; ret ans | Lam body => ret (fun x => denote_closed_term (body x)) | Opr m1 m2 => x <- (denote_closed_term m1) ;; y <- (denote_closed_term m2) ;; Ret (x + y) end.
|
Fixpoint
|
examples
|
[] |
examples/STLC.v
|
denote_closed_term
| |
Fixpoint denote_rec (V:typ -> Type) E (base : forall u (m : tm V u), itree E (V u)) (G: list typ) (u:typ) (m : open_tm V G u) : denotation_tm_typ E V G u := match G with | [] => base u _ | t::ts => fun (x : V t) => denote_rec V E base ts u _ end.
|
Program
|
examples
|
[] |
examples/STLC.v
|
Fixpoint
| |
Obligation . simpl in m. exact m. Defined.
|
Next
|
examples
|
[] |
examples/STLC.v
|
Obligation
| |
Obligation . unfold Term in m. simpl in m. apply m in x. exact x. Defined.
|
Next
|
examples
|
[] |
examples/STLC.v
|
Obligation
| |
Definition denote E (G : list typ) (u:typ) (m : Term G u) : denotation_tm_typ E (denote_typ E) G u := denote_rec (denote_typ E) E (@denote_closed_term E) G u _.
|
Program
|
examples
|
[] |
examples/STLC.v
|
Definition
| |
Obligation . unfold Term in m. specialize (m (denote_typ E)). exact m. Defined.
|
Next
|
examples
|
[] |
examples/STLC.v
|
Obligation
| |
id_tm : Term [] (Arr Base Base) := fun V => Lam (fun x => Var x).
|
Definition
|
examples
|
[] |
examples/STLC.v
|
id_tm
| |
example : Term [] Base := fun V => App (id_tm V) (Lit 3).
|
Definition
|
examples
|
[] |
examples/STLC.v
|
example
| |
example_equiv E : (denote E [] Base example) ≈ Ret 3. Proof. cbn. repeat rewrite bind_ret_l. reflexivity. Qed.
|
Lemma
|
examples
|
[] |
examples/STLC.v
|
example_equiv
| |
twice : Term [] (Arr (Arr Base Base) (Arr Base Base)) := fun V => Lam (fun f => Lam (fun x => App (Var f) (App (Var f) (Var x)))).
|
Definition
|
examples
|
[] |
examples/STLC.v
|
twice
| |
example2 : Term [] Base := fun V => App (App (twice V) (id_tm V)) (Lit 3).
|
Definition
|
examples
|
[] |
examples/STLC.v
|
example2
| |
big_example_equiv E : (denote E [] Base example2) ≈ Ret 3. Proof. cbn. repeat rewrite bind_ret_l. reflexivity. Qed.
|
Lemma
|
examples
|
[] |
examples/STLC.v
|
big_example_equiv
| |
add_2_tm : Term [] (Arr Base Base) := fun V => Lam (fun x => (Opr (Var x) (Lit 2))).
|
Definition
|
examples
|
[] |
examples/STLC.v
|
add_2_tm
| |
example3 : Term [] Base := fun V => App (App (twice V) (add_2_tm V)) (Lit 3).
|
Definition
|
examples
|
[] |
examples/STLC.v
|
example3
| |
big_example2_equiv E : (denote E [] Base example3) ≈ Ret 7. Proof. cbn. repeat rewrite bind_ret_l. reflexivity. Qed.
|
Lemma
|
examples
|
[] |
examples/STLC.v
|
big_example2_equiv
| |
iforest (E: Type -> Type) (X: Type): Type := itree E X -> Prop. (** TODO: There may be a generalization to a kind of monad transformer [PropT M X := M X -> Prop]. *) (** [iforest] are expected to be compatible with the [eutt] relation in the following sense: *)
|
Definition
|
extra
|
[
"From Paco Require Import paco.",
"From ExtLib Require Import\n Structures."
] |
extra/IForest.v
|
iforest
| |
eutt_closed := (Proper (eutt eq ==> iff)) (only parsing). #[global] Polymorphic Instance Eq1_iforest {E} : Eq1 (iforest E) := fun a PA PA' => (forall x y, x ≈ y -> (PA x <-> PA' y)) /\ eutt_closed PA /\ eutt_closed PA'. #[global] Instance Functor_iforest {E} : Functor (iforest E) := {| fmap := fun A B f PA b => exists (a: itree E A), PA a /\ b = fmap f a |}.
|
Notation
|
extra
|
[
"From Paco Require Import paco.",
"From ExtLib Require Import\n Structures."
] |
extra/IForest.v
|
eutt_closed
| |
subtree {E} {A B} (ta : itree E A) (tb : itree E B) : Prop := exists (k : A -> itree E B), tb ≈ bind ta k. (* Definition 5.1 *)
|
Definition
|
extra
|
[
"From Paco Require Import paco.",
"From ExtLib Require Import\n Structures."
] |
extra/IForest.v
|
subtree
| |
bind_iforest {E} : forall A B, iforest E A -> (A -> iforest E B) -> iforest E B := fun A B (specA : iforest E A) (K: A -> iforest E B) (tb: itree E B) => exists (ta: itree E A) (k: A -> itree E B), specA ta /\ tb ≈ bind ta k /\ (forall a, Leaf a ta -> K a (k a)).
|
Definition
|
extra
|
[
"From Paco Require Import paco.",
"From ExtLib Require Import\n Structures."
] |
extra/IForest.v
|
bind_iforest
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.