Dataset Viewer
Auto-converted to Parquet Duplicate
fact
stringlengths
23
2.8k
type
stringclasses
15 values
library
stringclasses
3 values
imports
listlengths
1
5
filename
stringclasses
23 values
symbolic_name
stringlengths
1
55
docstring
stringclasses
1 value
door : Type := left | right.
Inductive
examples
[ "From Coq Require Import Arith.", "From FreeSpec.Core Require Import Core CoreFacts." ]
examples/airlock.v
door
door_eq_dec (d d' : door) : { d = d' } + { ~ d = d' } := ltac:(decide equality).
Definition
examples
[ "From Coq Require Import Arith.", "From FreeSpec.Core Require Import Core CoreFacts." ]
examples/airlock.v
door_eq_dec
DOORS : interface := | IsOpen : door -> DOORS bool | Toggle : door -> DOORS unit. Generalizable All Variables.
Inductive
examples
[ "From Coq Require Import Arith.", "From FreeSpec.Core Require Import Core CoreFacts." ]
examples/airlock.v
DOORS
is_open `{Provide ix DOORS} (d : door) : impure ix bool := request (IsOpen d).
Definition
examples
[ "From Coq Require Import Arith.", "From FreeSpec.Core Require Import Core CoreFacts." ]
examples/airlock.v
is_open
toggle `{Provide ix DOORS} (d : door) : impure ix unit := request (Toggle d).
Definition
examples
[ "From Coq Require Import Arith.", "From FreeSpec.Core Require Import Core CoreFacts." ]
examples/airlock.v
toggle
open_door `{Provide ix DOORS} (d : door) : impure ix unit := let* open := is_open d in when (negb open) (toggle d).
Definition
examples
[ "From Coq Require Import Arith.", "From FreeSpec.Core Require Import Core CoreFacts." ]
examples/airlock.v
open_door
close_door `{Provide ix DOORS} (d : door) : impure ix unit := let* open := is_open d in when open (toggle d). (** ** Controller *)
Definition
examples
[ "From Coq Require Import Arith.", "From FreeSpec.Core Require Import Core CoreFacts." ]
examples/airlock.v
close_door
CONTROLLER : interface := | Tick : CONTROLLER unit | RequestOpen (d : door) : CONTROLLER unit.
Inductive
examples
[ "From Coq Require Import Arith.", "From FreeSpec.Core Require Import Core CoreFacts." ]
examples/airlock.v
CONTROLLER
tick `{Provide ix CONTROLLER} : impure ix unit := request Tick.
Definition
examples
[ "From Coq Require Import Arith.", "From FreeSpec.Core Require Import Core CoreFacts." ]
examples/airlock.v
tick
request_open `{Provide ix CONTROLLER} (d : door) : impure ix unit := request (RequestOpen d).
Definition
examples
[ "From Coq Require Import Arith.", "From FreeSpec.Core Require Import Core CoreFacts." ]
examples/airlock.v
request_open
co (d : door) : door := match d with | left => right | right => left end.
Definition
examples
[ "From Coq Require Import Arith.", "From FreeSpec.Core Require Import Core CoreFacts." ]
examples/airlock.v
co
controller `{Provide ix DOORS, Provide ix (STORE nat)} : component CONTROLLER ix := fun _ op => match op with | Tick => let* cpt := get in when (15 <? cpt) begin close_door left;; close_door right;; put 0 end | RequestOpen d => close_door (co d);; open_door d;; put 0 end. (** * Verifying the Airlock Controller *) (** ** Doors Specification *) (** *** Witness States *)
Definition
examples
[ "From Coq Require Import Arith.", "From FreeSpec.Core Require Import Core CoreFacts." ]
examples/airlock.v
controller
Ω : Type := bool * bool.
Definition
examples
[ "From Coq Require Import Arith.", "From FreeSpec.Core Require Import Core CoreFacts." ]
examples/airlock.v
Ω
sel (d : door) : Ω -> bool := match d with | left => fst | right => snd end.
Definition
examples
[ "From Coq Require Import Arith.", "From FreeSpec.Core Require Import Core CoreFacts." ]
examples/airlock.v
sel
tog (d : door) (ω : Ω) : Ω := match d with | left => (negb (fst ω), snd ω) | right => (fst ω, negb (snd ω)) end.
Definition
examples
[ "From Coq Require Import Arith.", "From FreeSpec.Core Require Import Core CoreFacts." ]
examples/airlock.v
tog
tog_equ_1 (d : door) (ω : Ω) : sel d (tog d ω) = negb (sel d ω). Proof. destruct d; reflexivity. Qed.
Lemma
examples
[ "From Coq Require Import Arith.", "From FreeSpec.Core Require Import Core CoreFacts." ]
examples/airlock.v
tog_equ_1
tog_equ_2 (d : door) (ω : Ω) : sel (co d) (tog d ω) = sel (co d) ω. Proof. destruct d; reflexivity. Qed. (** From now on, we will reason about [tog] using [tog_equ_1] and [tog_equ_2]. FreeSpec tactics rely heavily on [cbn] to simplify certain terms, so we use the <<simpl never>> options of the [Arguments] vernacular command to prevent [cbn] from unfolding [tog]. This pattern is common in FreeSpec. Later in this example, we will use this trick to prevent [cbn] to unfold impure computations covered by intermediary theorems. *) #[local] Opaque tog.
Lemma
examples
[ "From Coq Require Import Arith.", "From FreeSpec.Core Require Import Core CoreFacts." ]
examples/airlock.v
tog_equ_2
step (ω : Ω) (a : Type) (e : DOORS a) (x : a) := match e with | Toggle d => tog d ω | _ => ω end. (** *** Requirements *)
Definition
examples
[ "From Coq Require Import Arith.", "From FreeSpec.Core Require Import Core CoreFacts." ]
examples/airlock.v
step
doors_o_caller : Ω -> forall (a : Type), DOORS a -> Prop := (** - Given the door [d] of o system [ω], it is always possible to ask for the state of [d]. *) | req_is_open (d : door) (ω : Ω) : doors_o_caller ω bool (IsOpen d) (** - Given the door [d] of o system [ω], if [d] is closed, then the second door [co d] has to be closed too for a request to toggle [d] to be valid. *) | req_toggle (d : door) (ω : Ω) (H : sel d ω = false -> sel (co d) ω = false) : doors_o_caller ω unit (Toggle d). #[global] Hint Constructors doors_o_caller : airlock. (** *** Promises *)
Inductive
examples
[ "From Coq Require Import Arith.", "From FreeSpec.Core Require Import Core CoreFacts." ]
examples/airlock.v
doors_o_caller
doors_o_callee : Ω -> forall (a : Type), DOORS a -> a -> Prop := (** - When a system in a state [ω] reports the state of the door [d], it shall reflect the true state of [d]. *) | doors_o_callee_is_open (d : door) (ω : Ω) (x : bool) (equ : sel d ω = x) : doors_o_callee ω bool (IsOpen d) x (** - There is no particular doors_o_calleeises on the result [x] of a request for [ω] to close the door [d]. *) | doors_o_callee_toggle (d : door) (ω : Ω) (x : unit) : doors_o_callee ω unit (Toggle d) x. #[global] Hint Constructors doors_o_callee : airlock.
Inductive
examples
[ "From Coq Require Import Arith.", "From FreeSpec.Core Require Import Core CoreFacts." ]
examples/airlock.v
doors_o_callee
doors_contract : contract DOORS Ω := make_contract step doors_o_caller doors_o_callee. (** ** Intermediary Lemmas *) (** Closing a door [d] in any system [ω] is always a respectful operation. *)
Definition
examples
[ "From Coq Require Import Arith.", "From FreeSpec.Core Require Import Core CoreFacts." ]
examples/airlock.v
doors_contract
close_door_respectful `{Provide ix DOORS} (ω : Ω) (d : door) : pre (to_hoare doors_contract (close_door d)) ω. Proof. (* We use the [prove_program] tactics to erase the program monad *) prove impure with airlock; subst; constructor. (* This leaves us with one goal to prove: [sel d ω = false -> sel (co d) ω = false] Yet, thanks to our call to [IsOpen d], we can predict that [sel d ω = true] *) inversion o_caller0; ssubst. now rewrite H3. Qed. #[global] Hint Resolve close_door_respectful : airlock.
Lemma
examples
[ "From Coq Require Import Arith.", "From FreeSpec.Core Require Import Core CoreFacts." ]
examples/airlock.v
close_door_respectful
open_door_respectful `{Provide ix DOORS} (ω : Ω) (d : door) (safe : sel (co d) ω = false) : pre (to_hoare doors_contract (open_door (ix := ix) d)) ω. Proof. prove impure; repeat constructor; subst. inversion o_caller0; ssubst. now rewrite safe. Qed. #[global] Hint Resolve open_door_respectful : airlock.
Lemma
examples
[ "From Coq Require Import Arith.", "From FreeSpec.Core Require Import Core CoreFacts." ]
examples/airlock.v
open_door_respectful
close_door_run `{Provide ix DOORS} (ω : Ω) (d : door) (ω' : Ω) (x : unit) (run : post (to_hoare doors_contract (close_door d)) ω x ω') : sel d ω' = false. Proof. unroll_post run. + rewrite tog_equ_1. inversion H1; ssubst. now rewrite H5. + now inversion H1; ssubst. Qed. #[global] Hint Resolve close_door_run : airlock. #[local] Opaque close_door. #[local] Opaque open_door. #[local] Opaque Nat.ltb.
Lemma
examples
[ "From Coq Require Import Arith.", "From FreeSpec.Core Require Import Core CoreFacts." ]
examples/airlock.v
close_door_run
one_door_safe_all_doors_safe (ω : Ω) (d : door) (safe : sel d ω = false \/ sel (co d) ω = false) : forall (d' : door), sel d' ω = false \/ sel (co d') ω = false. Proof. intros d'. destruct d; destruct d'; auto. + cbn -[sel]. now rewrite or_comm. + cbn -[sel]. fold (co right). now rewrite or_comm. Qed. (** The objective of this lemma is to prove that, if either the right door or the left door is closed, then after any respectful run of a computation [p] that interacts with doors, this fact remains true. *) #[local] Opaque sel.
Remark
examples
[ "From Coq Require Import Arith.", "From FreeSpec.Core Require Import Core CoreFacts." ]
examples/airlock.v
one_door_safe_all_doors_safe
respectful_run_inv `{Provide ix DOORS} {a} (p : impure ix a) (ω : Ω) (safe : sel left ω = false \/ sel right ω = false) (x : a) (ω' : Ω) (hpre : pre (to_hoare doors_contract p) ω) (hpost : post (to_hoare doors_contract p) ω x ω') : sel left ω' = false \/ sel right ω' = false. (** We reason by induction on the impure computation [p]: - Either [p] is a local, pure computation; in such a case, the doors state does not change, hence the proof is trivial. - Or [p] consists in a request to the doors interface, and a continuation whose domain satisfies the theorem, i.e. it preserves the invariant that either the left or the right door is closed. Due to this hypothesis, we only have to prove that the first request made by [p] does not break the invariant. We consider two cases. - Either the computation asks for the state of a given door ([IsOpen]), then again the doors state does not change and the proof is trivial. - Or the computation wants to toggle a door [d]. We know by hypothesis that either [d] is closed or [d] is open (thanks to the [one_door_safe_all_doors_safe] result and the [safe] hypothesis). Again, we consider both cases. - If [d] is closed —and therefore will be opened—, then because we consider a respectful run, [co d] is necessarily closed too (it is a requirements of [door_contract]). Once [d] is opened, [co d] is still closed. - Otherwise, [co d] is closed, which means once [d] is toggled (no matter its initial state), then [co d] is still closed. That is, we prove that, when [p] toggles [d], [co d] is necessarily closed after the request has been handled. Because there is at least one door closed ([co d]), we can conclude that either the right or the left door is closed thanks to [one_door_safe_all_doors_safe]. *) Proof. fold (co left) in *. revert ω hpre hpost safe. induction p; intros ω hpre run safe. + now unroll_post run. + unroll_post run. assert (hpost : post (interface_to_hoare doors_contract β e) ω x0 ω0). { split; [apply H2|now rewrite H3]. } apply H1 with (ω:=ω0) (β:=x0); auto; [now apply hpre|]. cbn in *. unfold gen_caller_obligation, gen_callee_obligation, gen_witness_update in *. destruct (proj_p e) as [e'|]. ++ destruct hpost as [o_callee equω]. destruct e' as [d|d]. +++ rewrite H3. apply safe. +++ apply one_door_safe_all_doors_safe with (d := d); apply one_door_safe_all_doors_safe with (d' := d) in safe; subst. destruct hpre as [hbefore hafter]. inversion hbefore; ssubst. cbn. destruct safe as [safe|safe]. all: right; rewrite tog_equ_2; auto. ++ destruct hpost as [_ equω]. subst. exact safe. Qed. (** ** Main Theorem *)
Lemma
examples
[ "From Coq Require Import Arith.", "From FreeSpec.Core Require Import Core CoreFacts." ]
examples/airlock.v
respectful_run_inv
controller_correct `{StrictProvide2 ix DOORS (STORE nat)} : correct_component controller (no_contract CONTROLLER) doors_contract (fun _ ω => sel left ω = false \/ sel right ω = false). Proof. intros ωc ωd pred a e req. assert (hpre : pre (to_hoare doors_contract (controller a e)) ωd) by (destruct e; prove impure with airlock). split; auto. intros x ωj' run. cbn. split. + auto with freespec. + apply respectful_run_inv in run; auto. Qed.
Lemma
examples
[ "From Coq Require Import Arith.", "From FreeSpec.Core Require Import Core CoreFacts." ]
examples/airlock.v
controller_correct
with_heap `{Monad m, MonadRefs m} : m string := let* ptr := make_ref 2 in assign ptr 3;; let* x := deref ptr in if Nat.eqb x 2 then pure "yes!" else pure "no!". (* Coq projects the [with_heap] polymorphic definition directly into [impure], thanks to its typeclass inference algorithm. *)
Definition
examples
[ "From FreeSpec.Core Require Import Core Extraction.", "From FreeSpec.FFI Require Import FFI Refs.", "From FreeSpec.Exec Require Import Exec.", "From Coq Require Import String." ]
examples/heap.v
with_heap
with_heap_impure `{Provide ix REFS} : impure ix string := with_heap. Exec with_heap_impure.
Definition
examples
[ "From FreeSpec.Core Require Import Core Extraction.", "From FreeSpec.FFI Require Import FFI Refs.", "From FreeSpec.Exec Require Import Exec.", "From Coq Require Import String." ]
examples/heap.v
with_heap_impure
MEMORY : interface := | ReadFrom (addr : address) : MEMORY cell | WriteTo (addr : address) (value : cell) : MEMORY unit.
Inductive
examples
[ "From Coq Require Import ZArith.", "From FreeSpec Require Import Core CoreFacts." ]
examples/smram.v
MEMORY
DRAM : interface := | MakeDRAM {a : Type} (e : MEMORY a) : DRAM a.
Inductive
examples
[ "From Coq Require Import ZArith.", "From FreeSpec Require Import Core CoreFacts." ]
examples/smram.v
DRAM
dram_read_from `{Provide ix DRAM} (addr : address) : impure ix cell := request (MakeDRAM (ReadFrom addr)).
Definition
examples
[ "From Coq Require Import ZArith.", "From FreeSpec Require Import Core CoreFacts." ]
examples/smram.v
dram_read_from
dram_write_to `{Provide ix DRAM} (addr : address) (val : cell) : impure ix unit := request (MakeDRAM (WriteTo addr val)).
Definition
examples
[ "From Coq Require Import ZArith.", "From FreeSpec Require Import Core CoreFacts." ]
examples/smram.v
dram_write_to
VGA : interface := | MakeVGA {a : Type} (e : MEMORY a) : VGA a.
Inductive
examples
[ "From Coq Require Import ZArith.", "From FreeSpec Require Import Core CoreFacts." ]
examples/smram.v
VGA
vga_read_from `{Provide ix VGA} (addr : address) : impure ix cell := request (MakeVGA (ReadFrom addr)).
Definition
examples
[ "From Coq Require Import ZArith.", "From FreeSpec Require Import Core CoreFacts." ]
examples/smram.v
vga_read_from
vga_write_to `{Provide ix VGA} (addr : address) (val : cell) : impure ix unit := request (MakeVGA (WriteTo addr val)). (** ** Memory Controller *)
Definition
examples
[ "From Coq Require Import ZArith.", "From FreeSpec Require Import Core CoreFacts." ]
examples/smram.v
vga_write_to
smiact := smiact_set | smiact_unset.
Inductive
examples
[ "From Coq Require Import ZArith.", "From FreeSpec Require Import Core CoreFacts." ]
examples/smram.v
smiact
MEMORY_CONTROLLER : interface := | Read (pin : smiact) (addr : address) : MEMORY_CONTROLLER cell | Write (pin : smiact) (addr : address) (value : cell) : MEMORY_CONTROLLER unit.
Inductive
examples
[ "From Coq Require Import ZArith.", "From FreeSpec Require Import Core CoreFacts." ]
examples/smram.v
MEMORY_CONTROLLER
unwrap_sumbool {A B} (x : { A } + { B }) : bool := if x then true else false.
Definition
examples
[ "From Coq Require Import ZArith.", "From FreeSpec Require Import Core CoreFacts." ]
examples/smram.v
unwrap_sumbool
unwrap_sumbool : sumbool >-> bool.
Coercion
examples
[ "From Coq Require Import ZArith.", "From FreeSpec Require Import Core CoreFacts." ]
examples/smram.v
unwrap_sumbool
dispatch {a} `{Provide3 ix (STORE bool) DRAM VGA} (addr : address) (unpriv : address -> impure ix a) (priv : address -> impure ix a) : impure ix a := let* reg := get in if (andb reg (in_smram addr)) then unpriv addr else priv addr.
Definition
examples
[ "From Coq Require Import ZArith.", "From FreeSpec Require Import Core CoreFacts." ]
examples/smram.v
dispatch
memory_controller `{Provide3 ix (STORE bool) DRAM VGA} : component MEMORY_CONTROLLER ix := fun _ op => match op with (** When SMIACT is set, the CPU is in SMM. According to its specification, the Memory Controller can simply forward the memory access to the DRAM. *) | Read smiact_set addr => dram_read_from addr | Write smiact_set addr val => dram_write_to addr val (** On the contrary, when the SMIACT is not set, the CPU is not in SMM. As a consequence, the memory controller implements a dedicated access control mechanism. If the requested address belongs to the SMRAM and if the SMRAM lock has been set, then the memory access is forwarded to the VGA. Otherwise, it is forwarded to the DRAM by default. This is specified in the [dispatch] function. *) | Read smiact_unset addr => dispatch addr vga_read_from dram_read_from | Write smiact_unset addr val => dispatch addr (fun x => vga_write_to x val) (fun x => dram_write_to x val) end. (** * Verifying our Subsystem *) (** ** Memory View *)
Definition
examples
[ "From Coq Require Import ZArith.", "From FreeSpec Require Import Core CoreFacts." ]
examples/smram.v
memory_controller
memory_view : Type := address -> cell.
Definition
examples
[ "From Coq Require Import ZArith.", "From FreeSpec Require Import Core CoreFacts." ]
examples/smram.v
memory_view
update_memory_view_address (ω : memory_view) (addr : address) (content : cell) : memory_view := fun (addr' : address) => if address_eq_dec addr addr' then content else ω addr'. (** ** Specification *) (** *** DRAM Controller Specification *)
Definition
examples
[ "From Coq Require Import ZArith.", "From FreeSpec Require Import Core CoreFacts." ]
examples/smram.v
update_memory_view_address
update_dram_view (ω : memory_view) (a : Type) (p : DRAM a) (_ : a) : memory_view := match p with | MakeDRAM (WriteTo a v) => update_memory_view_address ω a v | _ => ω end.
Definition
examples
[ "From Coq Require Import ZArith.", "From FreeSpec Require Import Core CoreFacts." ]
examples/smram.v
update_dram_view
dram_o_callee (ω : memory_view) : forall (a : Type), DRAM a -> a -> Prop := | read_in_smram (a : address) (v : cell) (prom : in_smram a = true -> v = ω a) : dram_o_callee ω cell (MakeDRAM (ReadFrom a)) (ω a) | write (a : address) (v : cell) (r : unit) : dram_o_callee ω unit (MakeDRAM (WriteTo a v)) r.
Inductive
examples
[ "From Coq Require Import ZArith.", "From FreeSpec Require Import Core CoreFacts." ]
examples/smram.v
dram_o_callee
dram_specs : contract DRAM memory_view := {| witness_update := update_dram_view ; caller_obligation := no_caller_obligation ; callee_obligation := dram_o_callee |}. (** *** Memory Controller Specification *)
Definition
examples
[ "From Coq Require Import ZArith.", "From FreeSpec Require Import Core CoreFacts." ]
examples/smram.v
dram_specs
update_memory_controller_view (ω : memory_view) (a : Type) (p : MEMORY_CONTROLLER a) (_ : a) : memory_view := match p with | Write smiact_set a v => update_memory_view_address ω a v | _ => ω end.
Definition
examples
[ "From Coq Require Import ZArith.", "From FreeSpec Require Import Core CoreFacts." ]
examples/smram.v
update_memory_controller_view
memory_controller_o_callee (ω : memory_view) : forall (a : Type) (p : MEMORY_CONTROLLER a) (x : a), Prop := | memory_controller_read_o_callee (pin : smiact) (addr : address) (content : cell) (prom : pin = smiact_set -> in_smram addr = true -> ω addr = content) : memory_controller_o_callee ω cell (Read pin addr) content | memory_controller_write_o_callee (pin : smiact) (addr : address) (content : cell) (b : unit) : memory_controller_o_callee ω unit (Write pin addr content) b.
Inductive
examples
[ "From Coq Require Import ZArith.", "From FreeSpec Require Import Core CoreFacts." ]
examples/smram.v
memory_controller_o_callee
mc_specs : contract MEMORY_CONTROLLER memory_view := {| witness_update := update_memory_controller_view ; caller_obligation := no_caller_obligation ; callee_obligation := memory_controller_o_callee |}. (** ** Main Theorem *)
Definition
examples
[ "From Coq Require Import ZArith.", "From FreeSpec Require Import Core CoreFacts." ]
examples/smram.v
mc_specs
smram_pred (ωmc : memory_view) (ωmem : memory_view * bool) : Prop := snd ωmem = true /\ forall (a : address), in_smram a = true -> ωmc a = (fst ωmem) a.
Definition
examples
[ "From Coq Require Import ZArith.", "From FreeSpec Require Import Core CoreFacts." ]
examples/smram.v
smram_pred
memory_controller_respectful `{StrictProvide3 ix (STORE bool) VGA DRAM} (a : Type) (op : MEMORY_CONTROLLER a) (ω : memory_view) : pre (to_hoare (dram_specs * store_specs bool) (memory_controller a op)) (ω, true). Proof. destruct op; destruct pin; prove impure. Qed. #[local] Open Scope semantics_scope. #[local] Open Scope contract_scope.
Lemma
examples
[ "From Coq Require Import ZArith.", "From FreeSpec Require Import Core CoreFacts." ]
examples/smram.v
memory_controller_respectful
simpl_tuple := match goal with | H: (_, _) = (_, _) |- _ => inversion H; subst; clear H end.
Ltac
examples
[ "From Coq Require Import ZArith.", "From FreeSpec Require Import Core CoreFacts." ]
examples/smram.v
simpl_tuple
memory_controller_correct `{StrictProvide3 ix VGA (STORE bool) DRAM} (ω : memory_view) (sem : semantics ix) (comp : compliant_semantics (dram_specs * store_specs bool) (ω, true) sem) : compliant_semantics mc_specs ω (derive_semantics memory_controller sem). Proof. apply correct_component_derives_compliant_semantics with (pred := smram_pred) (cj := dram_specs * store_specs bool) (ωj := (ω, true)). + intros ωmc [ωdram b] [b_true pred] a e _. cbn in b_true. rewrite b_true in *; clear b_true. split. ++ apply memory_controller_respectful. ++ intros x [ωdram' st'] respectful. split. +++ destruct e; unroll_post respectful; repeat simpl_tuple; constructor. all: intros ?equ hin. all: lazymatch goal with | H: dram_o_callee _ _ (MakeDRAM (ReadFrom _)) _ |- _ => apply pred in hin; rewrite hin; inversion H; now ssubst | _ => discriminate end. +++ destruct e; unroll_post respectful; repeat simpl_tuple; split. all: try reflexivity. all: intros addr' hin; (now apply pred) || cbn. all: unfold update_memory_view_address; destruct address_eq_dec. all: try reflexivity. all: try now apply pred. rewrite <- (in_smram_morphism addr _ a) in hin. inversion H8; ssubst. cbn in equ. rewrite equ in hin. discriminate. + now constructor. + exact comp. Qed.
Theorem
examples
[ "From Coq Require Import ZArith.", "From FreeSpec Require Import Core CoreFacts." ]
examples/smram.v
memory_controller_correct
COUNTER : interface := | Get : COUNTER nat | Inc : COUNTER unit | Dec : COUNTER unit.
Inductive
tests
[ "From FreeSpec.Core Require Import CoreFacts.", "From Coq Require Import Lia." ]
tests/core_tactics.v
COUNTER
counter_get `{Provide ix COUNTER} : impure ix nat := request Get.
Definition
tests
[ "From FreeSpec.Core Require Import CoreFacts.", "From Coq Require Import Lia." ]
tests/core_tactics.v
counter_get
counter_inc `{Provide ix COUNTER} : impure ix unit := request Inc.
Definition
tests
[ "From FreeSpec.Core Require Import CoreFacts.", "From Coq Require Import Lia." ]
tests/core_tactics.v
counter_inc
counter_dec `{Provide ix COUNTER} : impure ix unit := request Dec.
Definition
tests
[ "From FreeSpec.Core Require Import CoreFacts.", "From Coq Require Import Lia." ]
tests/core_tactics.v
counter_dec
repeat {m : Type -> Type} `{Monad m} {a} (n : nat) (c : m a) : m unit := match n with | O => pure tt | S n => (c >>= fun _ => repeat n c) end.
Fixpoint
tests
[ "From FreeSpec.Core Require Import CoreFacts.", "From Coq Require Import Lia." ]
tests/core_tactics.v
repeat
update_counter (x : nat) : forall (a : Type), COUNTER a -> a -> nat := fun (a : Type) (p : COUNTER a) (_ : a) => match p with | Inc => S x | Dec => Nat.pred x | _ => x end.
Definition
tests
[ "From FreeSpec.Core Require Import CoreFacts.", "From Coq Require Import Lia." ]
tests/core_tactics.v
update_counter
counter_o_caller (x : nat) : forall (a : Type), COUNTER a -> Prop := fun (a : Type) (p : COUNTER a) => match p with | Dec => 0 < x | _ => True end.
Definition
tests
[ "From FreeSpec.Core Require Import CoreFacts.", "From Coq Require Import Lia." ]
tests/core_tactics.v
counter_o_caller
counter_o_callee (x : nat) : forall (a : Type), COUNTER a -> a -> Prop := fun (a : Type) (p : COUNTER a) (r : a) => match p, r with | Get, r => r = x | _, _ => True end.
Definition
tests
[ "From FreeSpec.Core Require Import CoreFacts.", "From Coq Require Import Lia." ]
tests/core_tactics.v
counter_o_callee
counter_specs : contract COUNTER nat := {| witness_update := update_counter ; caller_obligation := counter_o_caller ; callee_obligation := counter_o_callee |}.
Definition
tests
[ "From FreeSpec.Core Require Import CoreFacts.", "From Coq Require Import Lia." ]
tests/core_tactics.v
counter_specs
dec_then_inc `{Provide ix COUNTER} (x y : nat) : impure ix nat := repeat x counter_dec;; repeat y counter_inc;; counter_get.
Definition
tests
[ "From FreeSpec.Core Require Import CoreFacts.", "From Coq Require Import Lia." ]
tests/core_tactics.v
dec_then_inc
dec_then_inc_respectful `{Provide ix COUNTER} (cpt x y : nat) (init_cpt : x < cpt) : pre (to_hoare counter_specs $ dec_then_inc x y) cpt. Proof. prove impure. + revert x cpt init_cpt. induction x; intros cpt init_cpt; prove impure. ++ cbn. transitivity (S x); auto. apply PeanoNat.Nat.lt_0_succ. ++ apply IHx. now apply Lt.lt_pred. + clear init_cpt hpost cpt. revert ω; induction y; intros cpt; prove impure. Qed.
Theorem
tests
[ "From FreeSpec.Core Require Import CoreFacts.", "From Coq Require Import Lia." ]
tests/core_tactics.v
dec_then_inc_respectful
repeat_dec_cpt_output `(run : post (to_hoare counter_specs $ repeat x counter_dec) cpt r cpt') (init_cpt : x < cpt) : cpt' = cpt - x. Proof. revert init_cpt run; revert cpt; induction x; intros cpt init_cpt run. + unroll_post run. now rewrite PeanoNat.Nat.sub_0_r. + unroll_post run. apply IHx in run; [| lia]. subst. lia. Qed. #[local] Hint Resolve repeat_dec_cpt_output : counter.
Lemma
tests
[ "From FreeSpec.Core Require Import CoreFacts.", "From Coq Require Import Lia." ]
tests/core_tactics.v
repeat_dec_cpt_output
repeat_inc_cpt_output `(run : post (to_hoare counter_specs $ repeat x counter_inc) cpt r cpt') : cpt' = cpt + x. Proof. revert run; revert cpt; induction x; intros cpt run. + unroll_post run. now rewrite PeanoNat.Nat.add_0_r. + unroll_post run. apply IHx in run. lia. Qed. #[local] Hint Resolve repeat_inc_cpt_output : counter.
Lemma
tests
[ "From FreeSpec.Core Require Import CoreFacts.", "From Coq Require Import Lia." ]
tests/core_tactics.v
repeat_inc_cpt_output
get_cpt_output (cpt x cpt' : nat) (run : post (to_hoare counter_specs $ counter_get) cpt x cpt') : cpt' = cpt. Proof. now unroll_post run. Qed. #[local] Hint Resolve get_cpt_output : counter. #[local] Opaque counter_get.
Lemma
tests
[ "From FreeSpec.Core Require Import CoreFacts.", "From Coq Require Import Lia." ]
tests/core_tactics.v
get_cpt_output
dec_then_inc_cpt_output (cpt x y cpt' r : nat) (init_cpt : x < cpt) (run : post (to_hoare counter_specs $ dec_then_inc x y) cpt r cpt') : cpt' = cpt - x + y. Proof. unroll_post run. apply repeat_dec_cpt_output in run0; [| exact init_cpt ]. apply repeat_inc_cpt_output in run. apply get_cpt_output in run2. lia. Qed. #[local] Transparent counter_get.
Theorem
tests
[ "From FreeSpec.Core Require Import CoreFacts.", "From Coq Require Import Lia." ]
tests/core_tactics.v
dec_then_inc_cpt_output
dec_then_inc_output (cpt x y cpt' r : nat) (init_cpt : x < cpt) (run : post (to_hoare counter_specs $ dec_then_inc x y) cpt r cpt') : cpt' = r. Proof. now unroll_post run. Qed.
Theorem
tests
[ "From FreeSpec.Core Require Import CoreFacts.", "From Coq Require Import Lia." ]
tests/core_tactics.v
dec_then_inc_output
enum {a b ix} (p : a -> impure ix b) (l : list a) {measure (length l)} : impure ix unit := match l with | nil => pure tt | cons x rst => p x;; enum p rst end. Fail Timeout 1 Exec (enum eval [true; true; false]). (** We have provided an attribute for [Exec] which slightly changes the behavior of the command (see the documentation of [FreeSpec.Exec.Exec]). Note that this is not a silver bullet, as some computations may behave just fine with the default behavior, but on the contrary take forever to compute with this option enabled. *) #[reduce(nf)] Exec (enum eval [true; true; false]).
Fixpoint
tests
[ "From FreeSpec.Exec Require Import Exec Eval.", "From Coq.Program Require Import Wf.", "From Coq Require Import List." ]
tests/program_fixpoint.v
enum
p1 : forall `{Provide ix}, impure ix nat.
Axiom
tests
[ "From FreeSpec.Core Require Import Core." ]
tests/provide_notation.v
p1
p2 : forall `{Provide2 ix i3 i1}, impure ix nat.
Axiom
tests
[ "From FreeSpec.Core Require Import Core." ]
tests/provide_notation.v
p2
p `{Provide4 ix i1 i4 i3 i2} : impure ix nat := p1;; p2.
Definition
tests
[ "From FreeSpec.Core Require Import Core." ]
tests/provide_notation.v
p
provide_notation_test_1 {a} `{StrictProvide3 ix i1 i2 i3} (p : i2 a) : ix a := inj_p p.
Definition
tests
[ "From FreeSpec.Core Require Import Core." ]
tests/provide_notation.v
provide_notation_test_1
provide_notation_test_2 `{StrictProvide3 ix i1 i2 i3} : StrictProvide2 ix i2 i3. Proof. typeclasses eauto. Qed.
Lemma
tests
[ "From FreeSpec.Core Require Import Core." ]
tests/provide_notation.v
provide_notation_test_2
provide_notation_test_3 {a} (i1 i2 i3 : interface) (p : i2 a) : (iplus (iplus i1 i2) i3) a := inj_p p.
Definition
tests
[ "From FreeSpec.Core Require Import Core." ]
tests/provide_notation.v
provide_notation_test_3
provide_notation_test_4 (i1 i2 i3 : interface) : Provide (i1 + (i2 + i3)) i2. Proof. typeclasses eauto. Qed.
Lemma
tests
[ "From FreeSpec.Core Require Import Core." ]
tests/provide_notation.v
provide_notation_test_4
provide_notation_test_5 (i1 i2 i3 : interface) : StrictProvide2 (i1 + i2 + i3) i2 i1. Proof. typeclasses eauto. Qed.
Lemma
tests
[ "From FreeSpec.Core Require Import Core." ]
tests/provide_notation.v
provide_notation_test_5
component (i j : interface) : Type := forall (α : Type), i α -> impure j α. (** The similarity between FreeSpec components and operational semantics may be confusing at first. The main difference between the two concepts is simple: operational semantics are self-contained terms which can, alone, be used to interpret impure computations of a given interface. Components, on the other hand, are not self-contained. Without an operational semantics for [j], we cannot use a component [c : component i j] to interpret an impure computation using [i]. Given an initial semantics for [j], we can however derive an operational semantics for [i] from a component [c]. *) (** * Semantics Derivation *) CoFixpoint derive_semantics {i j} (c : component i j) (sem : semantics j) : semantics i := mk_semantics (fun a p => let (res, next) := runState (to_state $ c a p) sem in (res, derive_semantics c next)). (** So, [semprod] on the one hand allows for composing operational semantics horizontally, and [derive_semantics] allows for composing components vertically. Using these two operators, we can model a complete system in a hierarchical and modular manner, by defining each of its components independently, then composing them together with [semprod] and [derive_semantics]. *)
Definition
theories
[ "From ExtLib Require Import StateMonad.", "From FreeSpec.Core Require Export Interface Semantics Impure." ]
theories/Core/Component.v
component
bootstrap {i} (c : component i iempty) : semantics i := derive_semantics c iempty_semantics. (** * In-place Primitives Handling *) (** The function [with_component] allows for locally providing an additional interface [j] within an impure computation of type [impure ix a]. The primitives of [j] will be handled by impure computations, i.e., a component. of type [c : compoment j ix s]. *) #[local]
Definition
theories
[ "From ExtLib Require Import StateMonad.", "From FreeSpec.Core Require Export Interface Semantics Impure." ]
theories/Core/Component.v
bootstrap
with_component_aux {ix j α} (c : component j ix) (p : impure (ix + j) α) : impure ix α := match p with | local x => local x | request_then (in_right e) f => c _ e >>= fun res => with_component_aux c (f res) | request_then (in_left e) f => request_then e (fun x => with_component_aux c (f x)) end.
Fixpoint
theories
[ "From ExtLib Require Import StateMonad.", "From FreeSpec.Core Require Export Interface Semantics Impure." ]
theories/Core/Component.v
with_component_aux
with_component {ix j α} (initializer : impure ix unit) (c : component j ix) (finalizer : impure ix unit) (p : impure (ix + j) α) : impure ix α := initializer;; let* res := with_component_aux c p in finalizer;; pure res.
Definition
theories
[ "From ExtLib Require Import StateMonad.", "From FreeSpec.Core Require Export Interface Semantics Impure." ]
theories/Core/Component.v
with_component
derive_semantics_equ `(c : component i j) (sem : semantics j) : (derive_semantics c sem === derive_semantics' c sem)%semantics. Proof. revert sem. cofix aux; intros sem. constructor; intros a e; cbn; unfold derive_semantics; unfold eval_effect; unfold exec_effect; unfold run_effect; unfold evalState; unfold execState; destruct runState. + reflexivity. + apply aux. Qed. (** ** Component Correctness *) (** We consider a component [c : component i j s], meaning [c] exposes an interface [i], uses an interface [j], and carries an internal state of type [s]. << c : component i j s i +---------------------+ j | | | | +------>| | st : s |----->| | | | | +---------------------+ >> We say [c] is correct wrt. [ci] (a contract for [i]) and [cj] (a contract for [i]) iff given primitives which satisfies the caller obligation of [ci], [c] will - use respectful impure computations wrt. [cj] - compute results which satisfy [ci] callee obligations, assuming it can rely on a semantics of [j] which complies with [cj] The two witness states [ωi : Ωi] (for [speci]) and [ωj : Ωj] (for [specj]), and [st : s] the internal state of [c] remained implicit in the previous sentence. In practice, we associate them together by means of a dedicated predicate [pred]. It is expected that [pred] is an invariant throughout [c]'s life, meaning that as long as [c] computes result for legitimate effects (wrt. [speci] effects, the updated values of [ωi], [ωj] and [st] continue to satisfy [pred]. *)
Remark
theories
[ "From ExtLib Require Import StateMonad.", "From FreeSpec.Core Require Import Contract HoareFacts SemanticsFacts InstrumentFacts.", "From FreeSpec Require Export Component." ]
theories/Core/ComponentFacts.v
derive_semantics_equ
correct_component `{MayProvide jx j} `(c : component i jx) `(ci : contract i Ωi) `(cj : contract j Ωj) (pred : Ωi -> Ωj -> Prop) : Prop := forall (ωi : Ωi) (ωj : Ωj) (init : pred ωi ωj) `(e : i α) (o_caller : caller_obligation ci ωi e), pre (to_hoare cj $ c α e) ωj /\ forall (x : α) (ωj' : Ωj) (run : post (to_hoare cj $ c α e) ωj x ωj'), callee_obligation ci ωi e x /\ pred (witness_update ci ωi e x) ωj'. (** Once we have proven [c] is correct wrt. to [speci] and [specj] with [pred] acting as an invariant throughout [c] life, we show we can derive a semantics from [c] with an initial state [st] which complies to [speci] in accordance to [ωi] if we use a semantics of [j] which complies to [specj] in accordance to [ωj], where [pred ωi st ωj] is satisfied. *)
Definition
theories
[ "From ExtLib Require Import StateMonad.", "From FreeSpec.Core Require Import Contract HoareFacts SemanticsFacts InstrumentFacts.", "From FreeSpec Require Export Component." ]
theories/Core/ComponentFacts.v
correct_component
correct_component_derives_compliant_semantics `{MayProvide jx j} `(c : component i jx) `(ci : contract i Ωi) `(cj : contract j Ωj) (pred : Ωi -> Ωj -> Prop) (correct : correct_component c ci cj pred) (ωi : Ωi) (ωj : Ωj) (inv : pred ωi ωj) (sem : semantics jx) (comp : compliant_semantics cj ωj sem) : compliant_semantics ci ωi (derive_semantics c sem). Proof. rewrite derive_semantics_equ. revert ωi ωj inv sem comp. cofix correct_component_derives_compliant_semantics. intros ωi ωj inv sem comp. unfold correct_component in correct. specialize (correct ωi ωj inv). constructor; intros a e req; specialize (correct a e req); destruct correct as [trust run]. + eapply run. cbn. ++ rewrite instrument_to_state_eval_morphism with (c0 := cj) (ω := ωj). now apply instrument_satisfies_hoare. + eapply correct_component_derives_compliant_semantics. ++ apply run. cbn. erewrite instrument_to_state_eval_morphism. apply instrument_satisfies_hoare. +++ exact comp. +++ exact trust. ++ erewrite instrument_to_state_exec_morphism. apply instrument_preserves_compliance. +++ exact comp. +++ apply trust. Qed.
Lemma
theories
[ "From ExtLib Require Import StateMonad.", "From FreeSpec.Core Require Import Contract HoareFacts SemanticsFacts InstrumentFacts.", "From FreeSpec Require Export Component." ]
theories/Core/ComponentFacts.v
correct_component_derives_compliant_semantics
contract (i : interface) (Ω : Type) : Type := make_contract { witness_update (ω : Ω) : forall (α : Type), i α -> α -> Ω ; caller_obligation (ω : Ω) : forall (α : Type), i α -> Prop ; callee_obligation (ω : Ω) : forall (α : Type), i α -> α -> Prop }. Declare Scope contract_scope. Bind Scope contract_scope with contract. Arguments make_contract [i Ω] (_ _ _). Arguments witness_update [i Ω] (c ω) [α] (_ _). Arguments caller_obligation [i Ω] (c ω) [α] (_). Arguments callee_obligation [i Ω] (c ω) [α] (_ _). (** The most simple contract we can define is the one that requires anything both for the impure computations which uses the primitives of a given interface, and for the operational semantics which compute results for these primitives. *)
Record
theories
[ "From Coq Require Import Setoid Morphisms.", "From ExtLib Require Import StateMonad MonadState MonadTrans.", "From FreeSpec.Core Require Import Interface Impure Semantics Component." ]
theories/Core/Contract.v
contract
const_witness {i} := fun (u : unit) (α : Type) (e : i α) (x : α) => u.
Definition
theories
[ "From Coq Require Import Setoid Morphisms.", "From ExtLib Require Import StateMonad MonadState MonadTrans.", "From FreeSpec.Core Require Import Interface Impure Semantics Component." ]
theories/Core/Contract.v
const_witness
no_caller_obligation {i Ω} (ω : Ω) (α : Type) (e : i α) : Prop := | mk_no_caller_obligation : no_caller_obligation ω α e. #[global] Hint Constructors no_caller_obligation : freespec.
Inductive
theories
[ "From Coq Require Import Setoid Morphisms.", "From ExtLib Require Import StateMonad MonadState MonadTrans.", "From FreeSpec.Core Require Import Interface Impure Semantics Component." ]
theories/Core/Contract.v
no_caller_obligation
no_callee_obligation {i Ω} (ω : Ω) (α : Type) (e : i α) (x : α) : Prop := | mk_no_callee_obligation : no_callee_obligation ω α e x. #[global] Hint Constructors no_callee_obligation : freespec.
Inductive
theories
[ "From Coq Require Import Setoid Morphisms.", "From ExtLib Require Import StateMonad MonadState MonadTrans.", "From FreeSpec.Core Require Import Interface Impure Semantics Component." ]
theories/Core/Contract.v
no_callee_obligation
no_contract (i : interface) : contract i unit := {| witness_update := const_witness ; caller_obligation := no_caller_obligation ; callee_obligation := no_callee_obligation |}. (** A similar —and as simple— contract is the one that forbids the use of a given interface. *)
Definition
theories
[ "From Coq Require Import Setoid Morphisms.", "From ExtLib Require Import StateMonad MonadState MonadTrans.", "From FreeSpec.Core Require Import Interface Impure Semantics Component." ]
theories/Core/Contract.v
no_contract
do_no_use {i Ω} (ω : Ω) (α : Type) (e : i α) : Prop := False.
Definition
theories
[ "From Coq Require Import Setoid Morphisms.", "From ExtLib Require Import StateMonad MonadState MonadTrans.", "From FreeSpec.Core Require Import Interface Impure Semantics Component." ]
theories/Core/Contract.v
do_no_use
forbid_specs (i : interface) : contract i unit := {| witness_update := const_witness ; caller_obligation := do_no_use ; callee_obligation := no_callee_obligation |}. (** * Contract Equivalence *)
Definition
theories
[ "From Coq Require Import Setoid Morphisms.", "From ExtLib Require Import StateMonad MonadState MonadTrans.", "From FreeSpec.Core Require Import Interface Impure Semantics Component." ]
theories/Core/Contract.v
forbid_specs
contract_caller_equ `(c1 : contract i Ω1) `(c2 : contract i Ω2) (f : Ω1 -> Ω2) : Prop := forall ω1 a (p : i a), caller_obligation c1 ω1 p <-> caller_obligation c2 (f ω1) p.
Definition
theories
[ "From Coq Require Import Setoid Morphisms.", "From ExtLib Require Import StateMonad MonadState MonadTrans.", "From FreeSpec.Core Require Import Interface Impure Semantics Component." ]
theories/Core/Contract.v
contract_caller_equ
contract_callee_equ `(c1 : contract i Ω1) `(c2 : contract i Ω2) (f : Ω1 -> Ω2) : Prop := forall ω1 a (p : i a) x, callee_obligation c1 ω1 p x <-> callee_obligation c2 (f ω1) p x.
Definition
theories
[ "From Coq Require Import Setoid Morphisms.", "From ExtLib Require Import StateMonad MonadState MonadTrans.", "From FreeSpec.Core Require Import Interface Impure Semantics Component." ]
theories/Core/Contract.v
contract_callee_equ
contract_witness_equ `(c1 : contract i Ω1) `(c2 : contract i Ω2) (f : Ω1 -> Ω2) : Prop := forall ω1 a (p : i a) x, f (witness_update c1 ω1 p x) = witness_update c2 (f ω1) p x.
Definition
theories
[ "From Coq Require Import Setoid Morphisms.", "From ExtLib Require Import StateMonad MonadState MonadTrans.", "From FreeSpec.Core Require Import Interface Impure Semantics Component." ]
theories/Core/Contract.v
contract_witness_equ
contract_equ `(c1 : contract i Ω1) `(c2 : contract i Ω2) : Type := | mk_contract_equ (f : Ω1 -> Ω2) (g : Ω2 -> Ω1) (iso1 : forall x, f (g x) = x) (iso2 : forall x, g (f x) = x) (caller_equ : contract_caller_equ c1 c2 f) (callee_equ : contract_callee_equ c1 c2 f) (witness_equ : contract_witness_equ c1 c2 f) : contract_equ c1 c2.
Inductive
theories
[ "From Coq Require Import Setoid Morphisms.", "From ExtLib Require Import StateMonad MonadState MonadTrans.", "From FreeSpec.Core Require Import Interface Impure Semantics Component." ]
theories/Core/Contract.v
contract_equ
contract_iso_lr `(c1 : contract i Ω1) `(c2 : contract i Ω2) (equ : contract_equ c1 c2) (ω1 : Ω1) : Ω2 := match equ with | @mk_contract_equ _ _ _ _ _ f _ _ _ _ _ _ => f ω1 end.
Definition
theories
[ "From Coq Require Import Setoid Morphisms.", "From ExtLib Require Import StateMonad MonadState MonadTrans.", "From FreeSpec.Core Require Import Interface Impure Semantics Component." ]
theories/Core/Contract.v
contract_iso_lr
contract_iso_rl `(c1 : contract i Ω1) `(c2 : contract i Ω2) (equ : contract_equ c1 c2) (ω2 : Ω2) : Ω1 := match equ with | @mk_contract_equ _ _ _ _ _ _ g _ _ _ _ _ => g ω2 end. Arguments contract_iso_lr {i Ω1 c1 Ω2 c2} (equ ω1). Arguments contract_iso_rl {i Ω1 c1 Ω2 c2} (equ ω2).
Definition
theories
[ "From Coq Require Import Setoid Morphisms.", "From ExtLib Require Import StateMonad MonadState MonadTrans.", "From FreeSpec.Core Require Import Interface Impure Semantics Component." ]
theories/Core/Contract.v
contract_iso_rl
contract_equ_refl `(c : contract i Ω) : contract_equ c c. Proof. apply mk_contract_equ with (f:=fun x => x) (g:=fun x => x); auto. + now intros ω α p. + now intros ω α p x. + now intros ω α p x. Defined.
Lemma
theories
[ "From Coq Require Import Setoid Morphisms.", "From ExtLib Require Import StateMonad MonadState MonadTrans.", "From FreeSpec.Core Require Import Interface Impure Semantics Component." ]
theories/Core/Contract.v
contract_equ_refl
End of preview. Expand in Data Studio

Coq-FreeSpec

Structured dataset from FreeSpec — Modular verification of effectful programs.

279 declarations extracted from Coq source files.

Applications

  • Training language models on formal proofs
  • Fine-tuning theorem provers
  • Retrieval-augmented generation for proof assistants
  • Learning proof embeddings and representations

Source

Schema

Column Type Description
fact string Declaration body
type string Lemma, Definition, Theorem, etc.
library string Source module
imports list Required imports
filename string Source file path
symbolic_name string Identifier
Downloads last month
23

Collection including phanerozoic/Coq-FreeSpec