@@ -13,10 +13,12 @@ Require Import ExtLib.Core.RelDec.
1313Set Implicit Arguments .
1414Set Strict Implicit .
1515
16- Definition showM : Type :=
17- forall m, Injection ascii m -> Monoid m -> m.
16+ Set Printing Universes .
1817
19- Class ShowScheme (T : Type ) : Type :=
18+ Polymorphic Definition showM : Type :=
19+ forall m : Type, Injection ascii m -> Monoid m -> m.
20+
21+ Polymorphic Class ShowScheme (T : Type ) : Type :=
2022{ show_mon : Monoid T
2123; show_inj : Injection ascii T
2224}.
@@ -31,22 +33,22 @@ Global Instance ShowScheme_string_compose : ShowScheme (string -> string) :=
3133; show_inj := String
3234}.
3335
34- Definition runShow {T} {M : ShowScheme T} (m : showM) : T :=
36+ Polymorphic Definition runShow {T} {M : ShowScheme T} (m : showM) : T :=
3537 m _ show_inj show_mon.
3638
37- Class Show T := show : T -> showM.
39+ Polymorphic Class Show T := show : T -> showM.
3840
39- Definition to_string {T} {M : Show T} (v : T) : string :=
41+ Polymorphic Definition to_string {T} {M : Show T} (v : T) : string :=
4042 runShow (show v) ""%string.
4143
42- Definition empty : showM :=
44+ Polymorphic Definition empty : showM :=
4345 fun _ _ m => monoid_unit m.
44- Definition cat (a b : showM) : showM :=
46+ Polymorphic Definition cat (a b : showM) : showM :=
4547 fun _ i m => monoid_plus m (a _ i m) (b _ i m).
46- Global Instance Injection_ascii_showM : Injection ascii showM :=
48+ Global Polymorphic Instance Injection_ascii_showM : Injection ascii showM :=
4749 fun v => fun _ i _ => i v.
4850
49- Fixpoint show_exact (s : string) : showM :=
51+ Polymorphic Fixpoint show_exact (s : string) : showM :=
5052 match s with
5153 | EmptyString => empty
5254 | String a s' => cat (inject a) (show_exact s')
@@ -61,67 +63,20 @@ Module ShowNotation.
6163 Coercion _inject_char : ascii >-> showM.
6264End ShowNotation.
6365
64- Definition indent (indent : showM) (v : showM) : showM :=
66+ Polymorphic Definition indent (indent : showM) (v : showM) : showM :=
6567 let nl := Ascii.ascii_of_nat 10 in
6668 fun _ inj mon =>
6769 v _ (fun a => if eq_dec a nl
6870 then monoid_plus mon (inj a) (indent _ inj mon)
6971 else inj a) mon.
7072
71- Section hiding_notation .
73+ Section sepBy .
7274 Import ShowNotation.
7375 Local Open Scope show_scope.
74- Require Import Ascii.
75- Require Import String.
76-
77- Global Instance unit_Show : Show unit :=
78- { show u := "tt"%string }.
79- Global Instance bool_Show : Show bool :=
80- { show b := if b then "true"%string else "false"%string }.
81- Global Instance ascii_Show : Show ascii :=
82- fun a => "'"%char << a << "'"%char.
83- Global Instance string_Show : Show string :=
84- { show s := """"%char << s << """"%char }.
85-
86- Program Fixpoint nat_show (n:nat) {measure n} : showM :=
87- if Compare_dec.le_gt_dec n 9 then
88- inject (Char.digit2ascii n)
89- else
90- let n' := NPeano.Nat.div n 10 in
91- (@nat_show n' _) << (inject (Char.digit2ascii (n - 10 * n'))).
92- Next Obligation .
93- assert (NPeano.Nat.div n 10 < n) ; eauto .
94- eapply NPeano.Nat.div_lt.
95- inversion H; apply Lt.lt_O_Sn.
96- repeat constructor.
97- Defined .
98- Global Instance nat_Show : Show nat := { show := nat_show }.
99-
100- Global Instance Show_positive : Show positive :=
101- fun x => nat_show (Pos.to_nat x).
102-
103- Global Instance Show_Z : Show Z :=
104- fun x =>
105- match x with
106- | Z0 => "0"%char
107- | Zpos p => show p
108- | Zneg p => "-"%char << show p
109- end .
110-
111- Section pair_Show.
112- Context {A B} {AS:Show A} {BS:Show B}.
113- Global Instance pair_Show : Show (A*B) :=
114- { show p :=
115- let (a,b) := p in
116- "("%char << show a << ","%char << show b << ")"%char
117- }.
118- End pair_Show.
119-
120- Section sepBy.
121- Variable T : Type.
76+ Polymorphic Variable T : Type.
12277 Context {F : Foldable T showM}.
12378
124- Definition sepBy (sep : showM) (ls : T) : showM :=
79+ Polymorphic Definition sepBy (sep : showM) (ls : T) : showM :=
12580 match
12681 fold (fun s acc =>
12782 match acc with
@@ -135,11 +90,13 @@ Section sepBy.
13590End sepBy.
13691
13792Section sepBy_f.
138- Variable T E : Type.
139- Context {F : Foldable T E}.
140- Variable (f : E -> showM).
93+ Import ShowNotation.
94+ Local Open Scope show_scope.
95+ Polymorphic Variables (T : Type) (E : Type).
96+ Polymorphic Context {F : Foldable T E}.
97+ Polymorphic Variable (f : E -> showM).
14198
142- Definition sepBy_f (sep : showM) (ls : T) : showM :=
99+ Polymorphic Definition sepBy_f (sep : showM) (ls : T) : showM :=
143100 match
144101 fold (fun s acc =>
145102 match acc with
@@ -152,12 +109,14 @@ Section sepBy_f.
152109 end .
153110End sepBy_f.
154111
155- Definition wrap (before after : showM) (x : showM) : showM :=
156- before << x << after.
112+ Polymorphic Definition wrap (before after : showM) (x : showM) : showM :=
113+ cat before (cat x after) .
157114
158115Section sum_Show.
159- Context {A B} {AS:Show A} {BS:Show B}.
160- Global Instance sum_Show : Show (A+B) :=
116+ Import ShowNotation.
117+ Local Open Scope show_scope.
118+ Polymorphic Context {A : Type@{a}} {B : Type@{b}} {AS:Show A} {BS:Show B}.
119+ Global Polymorphic Instance sum_Show : Show (A+B) :=
161120 { show s :=
162121 let (tag, payload) :=
163122 match s with
@@ -175,24 +134,73 @@ End sum_Show.
175134
176135Section foldable_Show.
177136 Require Import ExtLib.Structures.Reducible.
178- Context {A B } {F : Foldable B A} {BS : Show A}.
137+ Polymorphic Context {A:Type} {B:Type } {F : Foldable B A} {BS : Show A}.
179138
180- Global Instance foldable_Show : Show B :=
181- { show s := sepBy_f show ", "%string s }.
139+ Global Polymorphic Instance foldable_Show : Show B :=
140+ { show s := sepBy_f show (show_exact ", "%string) s }.
182141
183142End foldable_Show.
184143
185- End hiding_notation.
186-
187- Fixpoint iter_show (ss : list showM) : showM :=
144+ Polymorphic Fixpoint iter_show (ss : list showM) : showM :=
188145 match ss with
189146 | nil => empty
190147 | cons s ss => cat s (iter_show ss)
191148 end .
192149
150+ Section hiding_notation.
151+ Import ShowNotation.
152+ Local Open Scope show_scope.
153+ Require Import Ascii.
154+ Require Import String.
155+
156+ Global Instance unit_Show : Show unit :=
157+ { show u := "tt"%string }.
158+ Global Instance bool_Show : Show bool :=
159+ { show b := if b then "true"%string else "false"%string }.
160+ Global Instance ascii_Show : Show ascii :=
161+ fun a => "'"%char << a << "'"%char.
162+ Global Instance string_Show : Show string :=
163+ { show s := """"%char << s << """"%char }.
164+
165+ Program Fixpoint nat_show (n:nat) {measure n} : showM :=
166+ if Compare_dec.le_gt_dec n 9 then
167+ inject (Char.digit2ascii n)
168+ else
169+ let n' := NPeano.Nat.div n 10 in
170+ (@nat_show n' _) << (inject (Char.digit2ascii (n - 10 * n'))).
171+ Next Obligation .
172+ assert (NPeano.Nat.div n 10 < n) ; eauto .
173+ eapply NPeano.Nat.div_lt.
174+ inversion H; apply Lt.lt_O_Sn.
175+ repeat constructor.
176+ Defined .
177+ Global Instance nat_Show : Show nat := { show := nat_show }.
178+
179+ Global Instance Show_positive : Show positive :=
180+ fun x => nat_show (Pos.to_nat x).
181+
182+ Global Instance Show_Z : Show Z :=
183+ fun x =>
184+ match x with
185+ | Z0 => "0"%char
186+ | Zpos p => show p
187+ | Zneg p => "-"%char << show p
188+ end .
189+
190+ Section pair_Show.
191+ Polymorphic Context {A : Type@{a}} {B : Type@{b}} {AS:Show A} {BS:Show B}.
192+ Global Polymorphic Instance pair_Show : Show (A*B) :=
193+ { show p :=
194+ let (a,b) := p in
195+ "("%char << show a << ","%char << show b << ")"%char
196+ }.
197+ End pair_Show.
198+ End hiding_notation.
199+
200+
193201
194202(*
195203Examples:
196204Eval compute in (runShow (show (42,"foo"%string)) : string).
197- Eval compute in (runShow (show (inl true : bool+string))
205+ Eval compute in (runShow (show (inl true : bool+string))).
198206 *)
0 commit comments