| 1 |
From elpi Require Import elpi. |
| 2 |
|
| 3 |
(** Intrinsically typed data type and semantics, from software foundations. |
| 4 |
|
| 5 |
We devise a command to fuzz the semantics by flipping some operators. |
| 6 |
We do it by locally checking that the fuzzing produces welltyped terms, |
| 7 |
reducing a bit the nondeterminism of the fuzzer. |
| 8 |
*) |
| 9 |
|
| 10 |
Inductive ty := B | N. |
| 11 |
|
| 12 |
Inductive Exp : ty -> Type := |
| 13 |
| NUM : nat -> Exp N |
| 14 |
| BOOL : bool -> Exp B |
| 15 |
| PLUS : Exp N -> Exp N -> Exp N |
| 16 |
| AND : Exp B -> Exp B -> Exp B |
| 17 |
| OR : Exp B -> Exp B-> Exp B |
| 18 |
| EQ : Exp N -> Exp N -> Exp B. |
| 19 |
|
| 20 |
Inductive Val : ty -> Set := |
| 21 |
| iNv : nat -> Val N |
| 22 |
| iBv : bool -> Val B. |
| 23 |
|
| 24 |
Inductive eval : forall {T: ty}, Exp T -> Val T -> Prop := |
| 25 |
| E_Num n : |
| 26 |
eval (NUM n) (iNv n) |
| 27 |
| E_Bool b : |
| 28 |
eval (BOOL b) (iBv b) |
| 29 |
| E_Plus e1 e2 n1 n2 : |
| 30 |
eval e1 (iNv n1) -> |
| 31 |
eval e2 (iNv n2) -> |
| 32 |
eval (PLUS e1 e2) (iNv (n1 + n2)) |
| 33 |
| E_AND e1 e2 b1 b2 : |
| 34 |
eval e1 (iBv b1) -> |
| 35 |
eval e2 (iBv b2) -> |
| 36 |
eval (AND e1 e2) (iBv (b1 && b2)) |
| 37 |
| E_OR e1 e2 b1 b2 : |
| 38 |
eval e1 (iBv b1) -> |
| 39 |
eval e2 (iBv b2) -> |
| 40 |
eval (OR e1 e2) (iBv (b1 || b2)) |
| 41 |
| E_EQ e1 e2 n1 n2 : |
| 42 |
eval e1 (iNv n1) -> |
| 43 |
eval e2 (iNv n2) -> |
| 44 |
eval (EQ e1 e2) (iBv (Nat.eqb n1 n2)). |
| 45 |
|
| 46 |
Elpi Command fuzz. |
| 47 |
Elpi Accumulate lp:{{ |
| 48 |
|
| 49 |
pred fuzz i:term, o:term. |
| 50 |
|
| 51 |
% fuzzin rule: we look for a Coq term (?Op ?A ?B) and we turn it in (AND ?A ?B) |
| 52 |
% only if the new term is well typed. |
| 53 |
fuzz {{ lp:Op lp:A lp:B }} Fuzzed :- |
| 54 |
coq.say "DEBUG: attempt at fuzzing binary op:" Op, |
| 55 |
fuzz A A1, fuzz B B1, |
| 56 |
Fuzzed = {{ AND lp:A1 lp:B1 }}, |
| 57 |
coq.typecheck Fuzzed _ ok, % we don't care about the type, only that it is ok |
| 58 |
coq.say "DEBUG: fuzzed!". |
| 59 |
|
| 60 |
% rule for the dependent function space |
| 61 |
fuzz (prod N S T) (prod N S1 T1) :- |
| 62 |
fuzz S S1, |
| 63 |
% we load the context with types for x and y, as well as the fact that |
| 64 |
% we fuzz x to y |
| 65 |
pi x y\ decl x N S ==> decl y N S1 ==> fuzz x y ==> fuzz (T x) (T1 y). |
| 66 |
|
| 67 |
% rule for application |
| 68 |
fuzz (app L) (app L1) :- std.map L fuzz L1. |
| 69 |
|
| 70 |
% rule for global constants |
| 71 |
fuzz (global X) (global X). |
| 72 |
|
| 73 |
% TODO: we should have clauses for all other type formers... |
| 74 |
|
| 75 |
pred rename-constructors i:constructor, o:pair constructor string. |
| 76 |
rename-constructors C (pr C C1) :- |
| 77 |
coq.gref->id (indc C) S, |
| 78 |
C1 is S ^ "1". |
| 79 |
|
| 80 |
main [str IN, str OUT ] :- |
| 81 |
% locate the inductive |
| 82 |
coq.locate IN (indt I), |
| 83 |
% fetch all its data, in particulat the types of the constructors |
| 84 |
coq.env.indt I B NP NPU A KN KT, |
| 85 |
% fuzz all constructor types |
| 86 |
std.map KT fuzz KT1, |
| 87 |
% we rename them, otherwise Coq complains the names are already used |
| 88 |
std.map KN rename-constructors KN1, |
| 89 |
% declare the new inductive |
| 90 |
coq.build-indt-decl (coq.indt-spec I OUT B A KN1 KT1) NP NPU Decl, |
| 91 |
coq.env.add-indt Decl _. |
| 92 |
|
| 93 |
}}. |
| 94 |
|
| 95 |
|
| 96 |
Elpi fuzz eval eval1. |
| 97 |
|
| 98 |
(* let's print our new, broken, semantics ;-) *) |
| 99 |
Print eval1. |