-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
114 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,80 @@ | ||
# Coercion | ||
|
||
The `coercion` predicate enables to program Coq coercions in Elpi. | ||
The `coercion` app enables to program Coq coercions in Elpi. | ||
|
||
## Example of `coercion` | ||
This app is experimental. | ||
|
||
## The coercion predicate | ||
|
||
The `coercion` predicate lives in the database `coercion.db` | ||
|
||
```elpi | ||
% [coercion Ctx V Inferred Expected Res] is queried to cast V to Res | ||
% - [Ctx] is the context | ||
% - [V] is the value to be coerced | ||
% - [Inferred] is the type of [V] | ||
% - [Expected] is the type [V] should be coerced to | ||
% - [Res] is the result (of type [Expected]) | ||
pred coercion i:goal-ctx, i:term, i:term, i:term, o:term. | ||
``` | ||
|
||
By addings rules for this predicate one can recover from a type error, that is | ||
when `Inferred` and `Expected` are not unifiable. | ||
|
||
## Simple example of coercion | ||
|
||
This example maps `True : Prop` to `true : bool`, which is a function you | ||
cannot express in type theory, hence in the standard Coercion system. | ||
|
||
```coq | ||
From elpi.apps Require Import coercion. | ||
From Coq Require Import Bool. | ||
Elpi Accumulate Coercion lp:{{ | ||
Elpi Accumulate coercion.db lp:{{ | ||
coercion _ {{ true }} {{ bool }} {{ Prop }} {{ True }}. | ||
coercion _ {{ false }} {{ bool }} {{ Prop }} {{ False }}. | ||
coercion _ {{ True }} {{ Prop }} {{ bool }} {{ true }}. | ||
coercion _ {{ False }} {{ Prop }} {{ bool }} {{ false }}. | ||
}}. | ||
Elpi Typecheck Coercion. | ||
Elpi Typecheck coercion. (* checks the elpi program is OK *) | ||
Check true : Prop. | ||
Check false : Prop. | ||
Check True && False. | ||
``` | ||
|
||
## Requirements | ||
## Example of coercion with proof automation | ||
|
||
This coercion enriches `x : T` to a `{x : T | P x}` by using | ||
`my_solver` to prove `P x`. | ||
|
||
```coq | ||
From elpi.apps Require Import coercion. | ||
From Coq Require Import Arith ssreflect. | ||
Ltac my_solver := trivial with arith. | ||
Elpi Accumulate coercion.db lp:{{ | ||
coercion _ X Ty {{ @sig lp:Ty lp:P }} Solution :- std.do! [ | ||
% we unfold letins since the solver is dumb and the `as` in the second | ||
% example introduces a letin | ||
(pi a b b1\ copy a b :- def a _ _ b, copy b b1) => copy X X1, | ||
% we build the solution | ||
Solution = {{ @exist lp:Ty lp:P lp:X1 _ }}, | ||
% we call the solver | ||
coq.ltac.collect-goals Solution [G] [], | ||
coq.ltac.open (coq.ltac.call-ltac1 "my_solver") G [], | ||
]. | ||
Appropriate version of Coq with support for coercion hooks | ||
https://github.com/coq/coq/pull/17794 | ||
}}. | ||
Elpi Typecheck coercion. | ||
Goal {x : nat | x > 0}. | ||
apply: 3. | ||
Qed. | ||
Definition ensure_pos n : {x : nat | x > 0} := | ||
match n with | ||
| O => 1 | ||
| S x as y => y | ||
end. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
Require Import test_coercion. | ||
|
||
Check true : Prop. | ||
Check True : bool. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
From elpi.apps Require Import coercion. | ||
From Coq Require Import Arith ssreflect. | ||
|
||
Ltac my_solver := trivial with arith. | ||
|
||
Elpi Accumulate coercion.db lp:{{ | ||
|
||
coercion _ X Ty {{ @sig lp:Ty lp:P }} Solution :- std.do! [ | ||
% we unfold letins since the solve is dumb | ||
(pi a b b1\ copy a b :- def a _ _ b, copy b b1) => copy X X1, | ||
% we build the solution | ||
Solution = {{ @exist lp:Ty lp:P lp:X1 _ }}, | ||
% we call the solver | ||
coq.ltac.collect-goals Solution [G] [], | ||
coq.ltac.open (coq.ltac.call-ltac1 "my_solver") G [], | ||
]. | ||
|
||
}}. | ||
Elpi Typecheck coercion. | ||
|
||
Goal {x : nat | x > 0}. | ||
apply: 3. | ||
Qed. | ||
|
||
Definition add1 n : {x : nat | x > 0} := | ||
match n with | ||
| O => 1 | ||
| S x as y => y | ||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters