Skip to content

Commit 4a30036

Browse files
author
Gregory Malecha
committed
some more polymorphism.
1 parent 4c3ad63 commit 4a30036

File tree

5 files changed

+108
-98
lines changed

5 files changed

+108
-98
lines changed

theories/Programming/Injection.v

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ Require Import Coq.Strings.String.
44
Set Implicit Arguments.
55
Set Maximal Implicit Insertion.
66

7-
Class Injection x t := inject : x -> t.
7+
Polymorphic Class Injection (x : Type) (t : Type) := inject : x -> t.
88
(*
99
Class Projection x t := { project : t -> x ; pmodify : (x -> x) -> (t -> t) }.
1010
*)
1111

12-
Instance Injection_refl {T} : Injection T T := { inject := @id T }.
12+
Polymorphic Instance Injection_refl {T : Type} : Injection T T :=
13+
{ inject := @id T }.
1314

1415
Instance Injection_ascii_string : Injection ascii string :=
1516
{ inject a := String a EmptyString }.

theories/Programming/Show.v

Lines changed: 84 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ Require Import ExtLib.Core.RelDec.
1313
Set Implicit Arguments.
1414
Set 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.
6264
End 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.
13590
End sepBy.
13691

13792
Section 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.
153110
End 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

158115
Section 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

176135
Section 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

183142
End 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
(*
195203
Examples:
196204
Eval 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
*)

theories/Structures/MonadReader.v

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
Require Import Monad.
1+
Require Import ExtLib.Structures.Monad.
22

33
Set Implicit Arguments.
44
Set Maximal Implicit Arguments.
55

6-
Class MonadReader (T : Type) (m : Type -> Type) : Type :=
7-
{ local : forall {t}, (T -> T) -> m t -> m t
6+
Polymorphic Class MonadReader (T : Type) (m : Type@{d} -> Type) : Type :=
7+
{ local : forall {t : Type@{d}}, (T -> T) -> m t -> m t
88
; ask : m T
99
}.
1010

1111
Section monadic.
12-
Variable m : Type -> Type.
12+
Polymorphic Variable m : Type -> Type.
1313
Context {M : Monad m}.
14-
Variable T : Type.
14+
Polymorphic Variable T : Type.
1515
Context {MR : MonadReader T m}.
1616

17-
Definition asks {U} (f : T -> U) : m U :=
17+
Polymorphic Definition asks {U} (f : T -> U) : m U :=
1818
bind ask (fun x => ret (f x)).
1919

2020
End monadic.
2121

2222
Section SubReader.
23-
Variable m : Type -> Type.
23+
Polymorphic Variable m : Type -> Type.
2424
Context {M : Monad m}.
25-
Variable T S : Type.
25+
Polymorphic Variable T S : Type.
2626
Context {MR : MonadReader T m}.
2727

28-
Definition ReaderProd (f : T -> S) (g : S -> T -> T)
28+
Polymorphic Definition ReaderProd (f : T -> S) (g : S -> T -> T)
2929
: MonadReader S m :=
3030
{| ask := @asks m M T MR S f
3131
; local := fun _T up (c : m _T) => @local T m MR _ (fun s => g (up (f s)) s) c

theories/Structures/MonadWriter.v

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@ Require Import ExtLib.Structures.Monoid.
44
Set Implicit Arguments.
55
Set Maximal Implicit Arguments.
66

7-
Class MonadWriter (T : Type) (M : Monoid T) (m : Type -> Type) : Type :=
7+
Polymorphic Class MonadWriter (T : Type@{s}) (M : Monoid T)
8+
(m : Type@{d} -> Type) : Type :=
89
{ tell : T -> m unit
9-
; listen : forall {A}, m A -> m (A * T)%type
10-
; pass : forall {A}, m (A * (T -> T))%type -> m A
10+
; listen : forall {A : Type@{d}}, m A -> m (A * T)%type
11+
; pass : forall {A : Type@{d}}, m (A * (T -> T))%type -> m A
1112
}.
1213

1314
Section WriterOps.
14-
Context {m : Type -> Type}.
15-
Context {S : Type}.
15+
Polymorphic Context {m : Type -> Type}.
16+
Polymorphic Context {S : Type}.
1617
Context {Monad_m : Monad m}.
1718
Variable Monoid_S : Monoid S.
1819
Context {Writer_m : MonadWriter Monoid_S m}.
1920

20-
Definition listens {A B} (f : S -> B) (c : m A) : m (A * B) :=
21+
Polymorphic Definition listens {A B : Type} (f : S -> B) (c : m A) : m (A * B) :=
2122
liftM (fun x => (fst x, f (snd x))) (listen c).
2223

23-
Definition censor {A} (f : S -> S) (c : m A) : m A :=
24+
Polymorphic Definition censor {A : Type} (f : S -> S) (c : m A) : m A :=
2425
pass (liftM (fun x => (x, f)) c).
25-
End WriterOps.
26+
End WriterOps.

theories/Structures/Monoid.v

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ Set Implicit Arguments.
55
Set Maximal Implicit Insertion.
66

77
Section Monoid.
8-
Variable S : Type.
8+
Polymorphic Variable S : Type.
99

10-
Record Monoid : Type :=
10+
Polymorphic Record Monoid : Type :=
1111
{ monoid_plus : S -> S -> S
1212
; monoid_unit : S
1313
}.
1414

1515
Context {Type_S : type S}.
1616

17-
Class MonoidLaws (M : Monoid) : Type :=
17+
Polymorphic Class MonoidLaws (M : Monoid) : Type :=
1818
{ monoid_assoc :> Associative M.(monoid_plus) equal
1919
; monoid_lunit :> LeftUnit M.(monoid_plus) M.(monoid_unit) equal
2020
; monoid_runit :> RightUnit M.(monoid_plus) M.(monoid_unit) equal

0 commit comments

Comments
 (0)