Skip to content

Commit

Permalink
Merge pull request #758 from proux01/param2-register
Browse files Browse the repository at this point in the history
Add derive.param2.register
  • Loading branch information
gares authored Feb 2, 2025
2 parents cd0e0eb + 71daf5f commit eea1606
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
28 changes: 28 additions & 0 deletions apps/derive/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,34 @@ Inductive is_nat : nat -> Type :=
| is_S : forall n : nat, is_nat n -> is_nat (S n) *)
```

### `param2`

Binary parametricity translation.

Main command is `derive.param2`
```coq
Elpi derive.param2 nat.
Print nat_R. (*
Inductive nat_R : nat -> nat -> Set :=
| O_R : nat_R 0 0
| S_R : forall H H0 : nat, nat_R H H0 -> nat_R (S H) (S H0).
```

The command `derive.param2.register` can be used to register
handcrafted parametricity rules, so that they can be used by further
`derive.param2` commands.
```coq
Definition fa := 0.
Definition fb := fa.
Fail Elpi derive.param2 fb.
(* derive.param2: No binary parametricity translation for fa *)
Definition fa_R := O_R.
Elpi derive.param2.register fa fa_R.
Elpi derive.param2 fb.
```

### `param1_functor`

```coq
Expand Down
11 changes: 10 additions & 1 deletion apps/derive/elpi/param2.elpi
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ dispatch (const GR as C) Suffix Clauses :- do! [
C1 = (param Term Term (global (const TermR)) :- !),
coq.elpi.accumulate _ "derive.param2.db" (clause _ (before "param:fail") C1),
C2 = (paramR Term Term (global (const TermR)) :- !),
coq.elpi.accumulate _ "derive.param2.db" (clause _ (before "paramR:fail") C1),
coq.elpi.accumulate _ "derive.param2.db" (clause _ (before "paramR:fail") C2),
coq.elpi.accumulate _ "derive.param2.db" (clause _ _ (param-done C)),
Clauses = [param-done C, C1, C2]
].
Expand Down Expand Up @@ -241,6 +241,15 @@ pred main i:gref, i:string, o:list prop.
main T _ Clauses :-
dispatch T "_R" Clauses.

pred main_register i:gref, i:gref.
main_register I R :-
GI = global I, GR = global R,
C1 = (param GI GI GR :- !),
coq.elpi.accumulate _ "derive.param2.db" (clause _ (before "param:fail") C1),
C2 = (paramR GI GI GR :- !),
coq.elpi.accumulate _ "derive.param2.db" (clause _ (before "paramR:fail") C2),
coq.elpi.accumulate _ "derive.param2.db" (clause _ _ (param-done I)).

}

/*
Expand Down
9 changes: 9 additions & 0 deletions apps/derive/tests/test_param2.v
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,12 @@ Definition size_seq (A : Type) : size_of (list A) := fun _ => 0.
Elpi derive.param2 size_of.

Elpi derive.param2 size_seq. (* Fixed by https://github.com/LPCIC/coq-elpi/pull/754 *)

Definition fa := 0.
Definition fb := fa.

Fail Elpi derive.param2 fb.

Definition fa_R := O_R.
Elpi derive.param2.register fa fa_R.
Elpi derive.param2 fb.
11 changes: 11 additions & 0 deletions apps/derive/theories/derive/param2.v
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ Elpi Accumulate lp:{{
usage :- coq.error "Usage: derive.param2 <object name>".
}}.

Elpi Command derive.param2.register.
Elpi Accumulate File param2.
Elpi Accumulate Db derive.param2.db.
Elpi Accumulate lp:{{
main [str I, str R] :- !, coq.locate I GRI, coq.locate R GRR,
derive.param2.main_register GRI GRR.
main _ :- usage.

usage :- coq.error "Usage: derive.param2.register <name> <name_R>".
}}.


(* hook into derive *)
Elpi Accumulate derive File param2.
Expand Down

0 comments on commit eea1606

Please sign in to comment.