aboutsummaryrefslogtreecommitdiff
path: root/plugins/syntax/NatSyntaxViaZ.v
diff options
context:
space:
mode:
authorPierre Letouzey2017-02-22 23:15:24 +0100
committerJason Gross2018-08-31 20:05:53 -0400
commitb786723fe7aff0c58ac949d44a356e2df6805645 (patch)
treeac9420ae8a7264db22e46b6ec914550519c0a65a /plugins/syntax/NatSyntaxViaZ.v
parentb3c75936a4912ca794cdcecfeb92044552336c63 (diff)
Numeral Notation (for inductive types)
This is a portion of roglo's PR#156 introducing a Numeral Notation command : we deal here with inductive types via conversion fonctions from/to Z written in Coq. For an example, see plugins/syntax/NatSyntaxViaZ.v This commit does not include the part about printing via some ltac. Using ltac was meant for dealing with real numbers, let's see first what become PR#415 about a compact representation for real literals.
Diffstat (limited to 'plugins/syntax/NatSyntaxViaZ.v')
-rw-r--r--plugins/syntax/NatSyntaxViaZ.v56
1 files changed, 56 insertions, 0 deletions
diff --git a/plugins/syntax/NatSyntaxViaZ.v b/plugins/syntax/NatSyntaxViaZ.v
new file mode 100644
index 0000000000..adee347d90
--- /dev/null
+++ b/plugins/syntax/NatSyntaxViaZ.v
@@ -0,0 +1,56 @@
+Declare ML Module "numeral_notation_plugin".
+Require Import BinNums.
+
+(** ** Parsing and Printing digit strings as type nat *)
+
+Fixpoint pos_pred_double x :=
+ match x with
+ | xI p => xI (xO p)
+ | xO p => xI (pos_pred_double p)
+ | xH => xH
+ end.
+
+Definition nat_of_Z x :=
+ match x with
+ | Z0 => Some O
+ | Zpos p =>
+ let fix iter p a :=
+ match p with
+ | xI p0 => a + iter p0 (a + a)
+ | xO p0 => iter p0 (a + a)
+ | xH => a
+ end
+ in
+ Some (iter p (S O))
+ | Zneg p => None
+ end.
+
+Fixpoint pos_succ x :=
+ match x with
+ | xI p => xO (pos_succ p)
+ | xO p => xI p
+ | xH => xO xH
+ end.
+
+Definition Z_succ x :=
+ match x with
+ | Z0 => Zpos xH
+ | Zpos x => Zpos (pos_succ x)
+ | Zneg x =>
+ match x with
+ | xI p => Zneg (xO p)
+ | xO p => Zneg (pos_pred_double p)
+ | xH => Z0
+ end
+ end.
+
+Fixpoint Z_of_nat_loop n :=
+ match n with
+ | O => Z0
+ | S p => Z_succ (Z_of_nat_loop p)
+ end.
+
+Definition Z_of_nat n := Some (Z_of_nat_loop n).
+
+Numeral Notation nat nat_of_Z Z_of_nat : nat_scope
+ (warning after 5000).