1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
From Coq Require Ascii.
From Coq Require Import String.
From Coq.Program Require Import Wf.
From Coq Require Import BinPos.
From Coq Require Import ZArith.
Require Import ExtLib.Structures.Monoid.
Require Import ExtLib.Structures.Reducible.
Require Import ExtLib.Programming.Injection.
Require Import ExtLib.Data.Char.
Require Import ExtLib.Data.String.
Require Import ExtLib.Data.Fun.
Require Import ExtLib.Core.RelDec.
Set Implicit Arguments.
Set Strict Implicit.
Set Universe Polymorphism.
Set Printing Universes.
Monomorphic Universe Ushow.
Definition showM@{T} : Type@{Ushow} :=
forall m : Type@{T}, Injection ascii m -> Monoid m -> m.
Class ShowScheme@{t} (T : Type@{t}) : Type :=
{ show_mon : Monoid@{t} T
; show_inj : Injection ascii T
}.
Global Instance ShowScheme_string : ShowScheme string :=
{ show_mon := Monoid_string_append
; show_inj := fun x => String x EmptyString
}.
Global Instance ShowScheme_string_compose : ShowScheme (string -> string) :=
{ show_mon := Monoid_compose string
; show_inj := String
}.
Definition runShow {T} {M : ShowScheme T} (m : showM) : T :=
m _ show_inj show_mon.
Class Show@{t m} (T : Type@{t}) : Type :=
show : T -> showM@{m}.
Definition to_string {T} {M : Show T} (v : T) : string :=
runShow (show v) ""%string.
Definition empty : showM :=
fun _ _ m => monoid_unit m.
Definition cat (a b : showM) : showM :=
fun _ i m => monoid_plus m (a _ i m) (b _ i m).
Global Instance Injection_ascii_showM : Injection ascii showM :=
fun v => fun _ i _ => i v.
Fixpoint show_exact (s : string) : showM :=
match s with
| EmptyString => empty
| String a s' => cat (inject a) (show_exact s')
end.
Module ShowNotation.
Delimit Scope show_scope with show.
Notation "x << y" := (cat x%show y%show) (at level 100) : show_scope.
Coercion show_exact : string >-> showM.
Definition _inject_char : ascii -> showM := inject.
Coercion _inject_char : ascii >-> showM.
End ShowNotation.
Definition indent (indent : showM) (v : showM) : showM :=
let nl := Ascii.ascii_of_nat 10 in
fun _ inj mon =>
v _ (fun a => if eq_dec a nl
then monoid_plus mon (inj a) (indent _ inj mon)
else inj a) mon.
Section sepBy.
Import ShowNotation.
Local Open Scope show_scope.
Definition sepBy {T : Type}
{F : Foldable T showM} (sep : showM) (ls : T) : showM :=
match
fold (fun s acc =>
match acc with
| None => Some s
| Some x => Some (x << sep << s)
end) None ls
with
| None => empty
| Some s => s
end.
End sepBy.
Section sepBy_f.
Import ShowNotation.
Local Open Scope show_scope.
Variables (T : Type) (E : Type).
Context {F : Foldable T E}.
Variable (f : E -> showM).
Definition sepBy_f (sep : showM) (ls : T) : showM :=
match
fold (fun s acc =>
match acc with
| None => Some (f s)
| Some x => Some (x << sep << f s)
end) None ls
with
| None => empty
| Some s => s
end.
End sepBy_f.
Definition wrap (before after : showM) (x : showM) : showM :=
cat before (cat x after).
Section sum_Show.
Import ShowNotation.
Local Open Scope show_scope.
Definition sum_Show@{a m}
{A : Type@{a}} {B : Type@{a}} {AS:Show@{a m} A} {BS:Show@{a m} B}
: Show@{a m} (A+B) :=
fun s =>
let (tag, payload) :=
match s with
| inl a => (show_exact "inl"%string, show a)
| inr b => (show_exact "inr"%string, show b)
end
in
"("%char <<
tag <<
" "%char <<
payload <<
")"%char.
End sum_Show.
Section foldable_Show.
Context {A:Type} {B:Type} {F : Foldable B A} {BS : Show A}.
Global Instance foldable_Show : Show B :=
{ show s := sepBy_f show (show_exact ", "%string) s }.
End foldable_Show.
Fixpoint iter_show (ss : list showM) : showM :=
match ss with
| nil => empty
| cons s ss => cat s (iter_show ss)
end.
Section hiding_notation.
Import ShowNotation.
Local Open Scope show_scope.
Import Ascii.
Import String.
Global Instance unit_Show : Show unit :=
{ show u := "tt"%string }.
Global Instance bool_Show : Show bool :=
{ show b := if b then "true"%string else "false"%string }.
Global Instance ascii_Show : Show ascii :=
fun a => "'"%char << a << "'"%char.
Global Instance string_Show : Show string :=
{ show s := """"%char << s << """"%char }.
Program Fixpoint nat_show (n:nat) {measure n} : showM :=
if Compare_dec.le_gt_dec n 9 then
inject (Char.digit2ascii n)
else
let n' := Nat.div n 10 in
(@nat_show n' _) << (inject (Char.digit2ascii (n - 10 * n'))).
Next Obligation.
assert (Nat.div n 10 < n) ; eauto.
eapply Nat.div_lt.
match goal with [ H : n > _ |- _ ] => inversion H end; apply Nat.lt_0_succ.
repeat constructor.
Defined.
Global Instance nat_Show : Show nat := { show := nat_show }.
Global Instance Show_positive : Show positive :=
fun x => nat_show (Pos.to_nat x).
Global Instance Show_Z : Show Z :=
fun x =>
match x with
| Z0 => "0"%char
| Zpos p => show p
| Zneg p => "-"%char << show p
end.
End hiding_notation.
Section pair_Show.
Import ShowNotation.
Local Open Scope show_scope.
Definition pair_Show@{a m t}
{A : Type@{a}} {B : Type@{a}} {AS:Show A} {BS:Show B}
: Show@{_ t} (A*B) :=
fun p =>
let (a,b) := p in
"("%char << show a << ","%char << show b << ")"%char.
End pair_Show.
(*
Examples:
Eval compute in (runShow (show (42,"foo"%string)) : string).
Eval compute in (runShow (show (inl true : bool+string))).
*)
|