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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
From mathcomp Require Import ssreflect ssrbool eqtype.
Require Import Arith List String Lia.
From QuickChick Require Import QuickChick.
Import ListNotations.
(* Types *)
Inductive type : Type :=
| N : type
| Arrow : type -> type -> type.
Derive (Arbitrary, Show, EnumSized) for type.
Instance dec_type (t1 t2 : type) : Dec (t1 = t2).
Proof. dec_eq. Defined.
(* Terms *)
Definition var := nat.
Inductive term : Type :=
| Const : nat -> term
| Id : var -> term
| App : term -> term -> term
| Abs : type -> term -> term.
(* Environments *)
Definition env := list type.
Inductive bind : env -> nat -> type -> Prop :=
| BindNow : forall t G, bind (t :: G) 0 t
| BindLater : forall t t' x G,
bind G x t -> bind (t' :: G) (S x) t.
(* Generate variables of a specific type in an env. *)
Derive ArbitrarySizedSuchThat for (fun x => bind G x t).
(* Get the type of a given variable in an env. *)
Derive EnumSizedSuchThat for (fun t => bind G x t).
(* Check whether a variable has a given type in an env. *)
Derive DecOpt for (bind G e t).
(* Typing *)
Inductive typing (G : env) : term -> type -> Prop :=
| TId :
forall x t,
bind G x t ->
typing G (Id x) t
| TConst :
forall n,
typing G (Const n) N
| TAbs :
forall e t1 t2,
typing (t1 :: G) e t2 ->
typing G (Abs t1 e) (Arrow t1 t2)
| TApp :
forall e1 e2 t1 t2,
typing G e2 t1 ->
typing G e1 (Arrow t1 t2) ->
typing G (App e1 e2) t2.
Fixpoint typeOf G e : option type :=
match e with
| Id x => nth_error G x
| Const n => Some N
| Abs t e' =>
match typeOf (t::G) e' with
| Some t' => Some (Arrow t t')
| None => None
end
| App e1 e2 =>
match typeOf G e1, typeOf G e2 with
| Some (Arrow t1 t2), Some t1' =>
if t1 = t1'? then Some t2 else None
| _, _ => None
end
end.
(* Generate terms of a specific type in an env. *)
Derive ArbitrarySizedSuchThat for (fun e => typing G e t).
Derive EnumSizedSuchThat for (fun t => typing G e t).
(* Check whether a variable has a given type in an env. *)
Derive DecOpt for (typing G e t).
(* Small step CBV semantics *)
Inductive value : term -> Prop :=
| VConst : forall n, value (Const n)
| VAbs : forall t e, value (Abs t e).
Derive DecOpt for (value e).
Definition is_value (e : term) : bool :=
match e with
| Const _ | Abs _ _ => true
| _ => false
end.
Fixpoint subst (y : var) (e1 : term) (e2 : term) : term :=
match e2 with
| Const n => Const n
| Id x =>
if eq_nat_dec x y then e1 else e2
| App e e' =>
App (subst y e1 e) (subst y e1 e')
| Abs t e =>
Abs t (subst (S y) e1 e)
end.
Fixpoint step (e : term) : option term :=
match e with
| Const _ | Id _ => None | Abs _ x => None
| App (Abs t e1) e2 =>
if is_value e2 then Some (subst 0 e2 e1)
else
match step e2 with
| Some e2' => Some (App (Abs t e1) e2')
| None => None
end
| App e1 e2 =>
match step e1 with
| Some e1' => Some (App e1' e2)
| None => None
end
end.
Eval compute in (step (App (Abs N (Id 0)) (Const 42))).
Eval compute in (step (App (Abs N (Abs N (Id 0))) (Const 42))).
Eval compute in (subst 0 (Const 42) (Abs N (Id 0))).
(* Printing *)
Open Scope string.
Fixpoint show_type (tau : type) :=
match tau with
| N => "N"
| Arrow tau1 tau2 =>
"(Arrow " ++ show_type tau1 ++ " -> " ++ show_type tau2 ++ ")"
end.
Instance showType : Show type := { show := show_type }.
Fixpoint show_term (e : term) :=
match e with
| Const n => "(Const " ++ show n ++ ")"
| Id x => "(Id " ++ show x ++ ")"
| App e1 e2 => "(App " ++ show_term e1 ++ " " ++ show_term e2 ++ ")"
| Abs t e => "(Abs " ++ show t ++ " " ++ show_term e ++ ")"
end.
Close Scope string.
Instance showTerm : Show term := { show := show_term }.
Instance dec_eq_opt_type : Dec_Eq (option type).
Proof. dec_eq. Defined.
Definition preservation (e : term) (t: type) : Checker :=
match step e with
| Some e' => checker ((typeOf nil e' = Some t)?)
| None => checker true
end.
Definition preservation' (e : term) (t: type) : Checker :=
match step e with
| Some e' => typing nil e' t ?? 10
| None => checker true
end.
Definition preservation_prop (c : term -> type -> Checker) :=
forAll (@arbitrary type _) (fun t =>
forAllMaybe ((genST (fun e => typing nil e t))) (fun e =>
c e t)).
Extract Constant defNumTests => "20000".
QuickChick (preservation_prop preservation).
QuickChick (preservation_prop preservation').
|