| 1 |
From Coq Require Import BinInt BinNat QArith_base. |
| 2 |
From Coq Require Import List. |
| 3 |
From Coq Require Import String Ascii. |
| 4 |
Require Crypto.Util.Strings.String. |
| 5 |
Require Import Crypto.Util.ZUtil.Definitions. |
| 6 |
From Coq Require Import HexString. |
| 7 |
Require Import Crypto.Util.Strings.Decimal. |
| 8 |
Require Export Crypto.Util.Level. |
| 9 |
Import ListNotations. Local Open Scope Z_scope. Local Open Scope list_scope. Local Open Scope string_scope. |
| 10 |
Local Open Scope level_scope. |
| 11 |
|
| 12 |
Class ShowLevel T := show_lvl : T -> Level -> string. |
| 13 |
Class ShowLevel2 A B := show_lvl2 : A -> B -> Level -> string. |
| 14 |
Class ShowLevel3 A B C := show_lvl3 : A -> B -> C -> Level -> string. |
| 15 |
Class Show T := show : T -> string. |
| 16 |
Class ShowLines T := show_lines : T -> list string. |
| 17 |
Definition show_lvl2_explicit (F : ShowLevel unit -> ShowLevel unit -> ShowLevel2 unit unit) |
| 18 |
: (Level -> string) -> (Level -> string) -> (Level -> string) |
| 19 |
:= fun show_f show_x => F (fun 'tt => show_f) (fun 'tt => show_x) tt tt. |
| 20 |
Notation app_lvl := (Level.level 10) (only parsing). |
| 21 |
Notation term_lvl := (Level.level 200) (only parsing). |
| 22 |
Notation pair_lvl := (Level.prev term_lvl) (only parsing). |
| 23 |
Notation pair_assoc := LeftAssoc (only parsing). |
| 24 |
Notation opp_lvl := (Level.level 35) (only parsing). |
| 25 |
Notation impl_lvl := (Level.level 99) (only parsing). |
| 26 |
Notation impl_assoc := RightAssoc (only parsing). |
| 27 |
Notation iff_lvl := (Level.level 95) (only parsing). |
| 28 |
Notation iff_assoc := RightAssoc (only parsing). |
| 29 |
Notation lneg_lvl := (Level.level 75) (only parsing). |
| 30 |
Notation rel_lvl := (Level.level 70) (only parsing). |
| 31 |
Notation rel_assoc := RightAssoc (only parsing). |
| 32 |
Notation add_lvl := (Level.level 50) (only parsing). |
| 33 |
Notation add_assoc := FullyAssoc (only parsing). |
| 34 |
Notation sub_lvl := (Level.level 50) (only parsing). |
| 35 |
Notation sub_assoc := LeftAssoc (only parsing). |
| 36 |
Notation mul_lvl := (Level.level 40) (only parsing). |
| 37 |
Notation mul_assoc := FullyAssoc (only parsing). |
| 38 |
Notation div_lvl := (Level.level 40) (only parsing). |
| 39 |
Notation div_assoc := NoAssoc (only parsing). |
| 40 |
Notation mod_lvl := (Level.level 40) (only parsing). |
| 41 |
Notation mod_assoc := NoAssoc (only parsing). |
| 42 |
Notation andb_lvl := (Level.level 40) (only parsing). |
| 43 |
Notation andb_assoc := FullyAssoc (only parsing). |
| 44 |
Notation orb_lvl := (Level.level 40) (only parsing). |
| 45 |
Notation orb_assoc := FullyAssoc (only parsing). |
| 46 |
Notation pow_lvl := (Level.level 30) (only parsing). |
| 47 |
Notation pow_assoc := RightAssoc (only parsing). |
| 48 |
|
| 49 |
Global Instance Show_of_ShowLevel {T} `{ShowLevel T} : Show T | 1000 := fun t => show_lvl t term_lvl. |
| 50 |
Definition ShowLevel_of_Show {T} `{Show T} : ShowLevel T := fun t _ => show t. |
| 51 |
Definition ShowLines_of_Show {T} `{Show T} : ShowLines T := fun t => [show t]. |
| 52 |
Global Instance Show_of_ShowLines {T} `{ShowLines T} : Show T | 1000 := fun t => String.concat String.NewLine (show_lines t). |
| 53 |
Global Coercion Show_of_ShowLevel : ShowLevel >-> Show. |
| 54 |
Global Coercion ShowLevel_of_Show : Show >-> ShowLevel. |
| 55 |
|
| 56 |
Global Instance show_lvl_const : ShowLevel (unit -> string) := fun f _ => f tt. |
| 57 |
Global Instance show_lvl_id : ShowLevel (Level -> string) := fun f lvl => f lvl. |
| 58 |
|
| 59 |
Definition maybe_wrap_parens (parens : bool) (s : string) |
| 60 |
:= if parens then "(" ++ s ++ ")" else s. |
| 61 |
Definition show_wrap_parens (plvl : Level) {A} `{ShowLevel A} : ShowLevel A |
| 62 |
:= fun a lvl => if lvl <? plvl then "(" ++ show_lvl a term_lvl ++ ")" else show_lvl a lvl. |
| 63 |
Definition lvl_wrap_parens (plvl : Level) (s : string) : Level -> string |
| 64 |
:= show_wrap_parens plvl (fun 'tt => s). |
| 65 |
(** wrap with parentheses if the level is negative *) |
| 66 |
Definition neg_wrap_parens_with_lvl (f : Level -> string) : Level -> string |
| 67 |
:= show_wrap_parens 0 f. |
| 68 |
Definition neg_wrap_parens (f : string) : Level -> string |
| 69 |
:= neg_wrap_parens_with_lvl (fun _ => f). |
| 70 |
Definition ShowLevel2_binop_assoc (assoc : Associativity) (binop_lvl : Level) (binop : string) {A B} |
| 71 |
`{ShowLevel A, ShowLevel B} |
| 72 |
: ShowLevel2 A B |
| 73 |
:= fun a b |
| 74 |
=> lvl_wrap_parens |
| 75 |
binop_lvl |
| 76 |
(let lvl := binop_lvl in |
| 77 |
match assoc with |
| 78 |
| LeftAssoc => show_lvl a lvl ++ binop ++ show_lvl b (Level.next lvl) |
| 79 |
| RightAssoc => show_lvl a (Level.next lvl) ++ binop ++ show_lvl b lvl |
| 80 |
| NoAssoc => show_lvl a (Level.next lvl) ++ binop ++ show_lvl b (Level.next lvl) |
| 81 |
| FullyAssoc => show_lvl a lvl ++ binop ++ show_lvl b lvl |
| 82 |
| ExplicitAssoc lvl_l lvl_r => show_lvl a lvl_l ++ binop ++ show_lvl b lvl_r |
| 83 |
end). |
| 84 |
Definition show_lvl_binop (assoc : Associativity) (binop_lvl : Level) {A B} `{SA : ShowLevel A, SB : ShowLevel B} |
| 85 |
: A -> string -> B -> Level -> string |
| 86 |
:= fun a binop b => ShowLevel2_binop_assoc assoc binop_lvl binop a b. |
| 87 |
Definition show_lvl_app {A B} `{ShowLevel A, ShowLevel B} : ShowLevel2 A B |
| 88 |
:= ShowLevel2_binop_assoc LeftAssoc app_lvl " ". |
| 89 |
Definition show_lvl_app2 {A B C} `{ShowLevel A, ShowLevel B, ShowLevel C} : ShowLevel3 A B C |
| 90 |
:= fun a b c => show_lvl_app (show_lvl_app a b) c. |
| 91 |
Definition show_lvl_preop_assoc (assoc : Associativity) (op_lvl : Level) {A} |
| 92 |
`{ShowLevel A} |
| 93 |
: string -> A -> Level -> string |
| 94 |
:= fun preop a => show_lvl_binop assoc op_lvl (fun 'tt => "") preop a. |
| 95 |
Definition show_lvl_postop_assoc (assoc : Associativity) (op_lvl : Level) {A} |
| 96 |
`{ShowLevel A} |
| 97 |
: A -> string -> Level -> string |
| 98 |
:= fun a postop => show_lvl_binop assoc op_lvl a postop (fun 'tt => ""). |
| 99 |
Definition show_lvl_unop (unop_lvl : Level) (unop : string) {A} |
| 100 |
`{ShowLevel A} |
| 101 |
: ShowLevel A |
| 102 |
:= fun a => show_lvl_preop_assoc RightAssoc unop_lvl unop a. |
| 103 |
Definition show_lvl_postop (postop_lvl : Level) {A} |
| 104 |
`{ShowLevel A} |
| 105 |
: A -> string -> Level -> string |
| 106 |
:= fun a postop => show_lvl_postop_assoc LeftAssoc postop_lvl a postop. |
| 107 |
|
| 108 |
Definition maybe_wrap_parens_lines (parens : bool) (s : list string) |
| 109 |
:= match s, parens with |
| 110 |
| s, false => s |
| 111 |
| nil, _ => nil |
| 112 |
| [s], _ => [maybe_wrap_parens parens s] |
| 113 |
| s, true => "(" :: List.map (String " ") s ++ [")"] |
| 114 |
end%list. |
| 115 |
|
| 116 |
Global Instance show_lvl_unit : ShowLevel unit := fun 'tt => neg_wrap_parens "tt". |
| 117 |
Global Instance show_lvl_True : ShowLevel True := fun 'I => neg_wrap_parens "I". |
| 118 |
Global Instance show_lvl_False : ShowLevel False := fun pf => match pf with end. |
| 119 |
Global Instance show_lvl_Empty_set : ShowLevel Empty_set := fun pf => match pf with end. |
| 120 |
Global Instance show_lvl_bool : ShowLevel bool := fun b => neg_wrap_parens (if b then "true" else "false"). |
| 121 |
Global Instance show_lvl_option {T} `{ShowLevel T} : ShowLevel (option T) |
| 122 |
:= fun v |
| 123 |
=> match v with |
| 124 |
| Some v |
| 125 |
=> show_lvl_app (fun 'tt => "Some") (show_lvl v) |
| 126 |
| None => neg_wrap_parens "None" |
| 127 |
end. |
| 128 |
Global Instance show_option {T} `{Show T} : Show (option T) | 2000 := let _ := @ShowLevel_of_Show T in _. |
| 129 |
Global Instance show_lvl_list {T} `{ShowLevel T} : ShowLevel (list T) |
| 130 |
:= fun ls => neg_wrap_parens |
| 131 |
("[" ++ String.concat ", " (List.map (fun v => show_lvl v term_lvl) ls) ++ "]"). |
| 132 |
Global Instance show_list {T} `{Show T} : Show (list T) | 2000 := let _ := @ShowLevel_of_Show T in _. |
| 133 |
Global Instance show_lvl_prod {A B} `{ShowLevel A, ShowLevel B} : ShowLevel (A * B) |
| 134 |
:= fun '(a, b) => show_lvl_binop pair_assoc pair_lvl a ", " b. |
| 135 |
Global Instance show_prod {A B} `{Show A, Show B} : Show (A * B) | 2000 := |
| 136 |
let _ := @ShowLevel_of_Show A in |
| 137 |
let _ := @ShowLevel_of_Show B in |
| 138 |
_. |
| 139 |
Global Instance show_lvl_sum {A B} `{ShowLevel A, ShowLevel B} : ShowLevel (A + B) |
| 140 |
:= fun ab => match ab with |
| 141 |
| inl v => show_lvl_app (fun 'tt => "inl") (show_lvl v) |
| 142 |
| inr v => show_lvl_app (fun 'tt => "inr") (show_lvl v) |
| 143 |
end. |
| 144 |
Global Instance show_sum {A B} `{Show A, Show B} : Show (A + B) | 2000 |
| 145 |
:= let _ := @ShowLevel_of_Show A in |
| 146 |
let _ := @ShowLevel_of_Show B in |
| 147 |
_. |
| 148 |
Global Instance show_lvl_sigT {A B} `{ShowLevel A, forall a, ShowLevel (B a)} : ShowLevel (@sigT A B) |
| 149 |
:= fun '(existT a b) => show_lvl_binop pair_assoc pair_lvl a "; " b. |
| 150 |
Global Instance show_sigT {A B} `{Show A, forall a, Show (B a)} : Show (@sigT A B) | 2000 := |
| 151 |
let _ := @ShowLevel_of_Show A in |
| 152 |
let _ := fun a => @ShowLevel_of_Show (B a) in |
| 153 |
_. |
| 154 |
Global Instance show_lvl_sig {A} {B : A -> Prop} `{ShowLevel A, forall a, ShowLevel (B a)} : ShowLevel (@sig A B) |
| 155 |
:= fun '(exist a b) => show_lvl_binop pair_assoc pair_lvl a "; " b. |
| 156 |
Global Instance show_sig {A} {B : A -> Prop} `{Show A, forall a, Show (B a)} : Show (@sig A B) | 2000 := |
| 157 |
let _ := @ShowLevel_of_Show A in |
| 158 |
let _ := fun a => @ShowLevel_of_Show (B a) in |
| 159 |
_. |
| 160 |
Global Instance show_lvl_string : ShowLevel string := fun s => neg_wrap_parens ("""" ++ (String.replace """" """""" s) ++ """"). |
| 161 |
Global Instance show_lvl_ascii : ShowLevel ascii := fun ch => neg_wrap_parens (String "'" (String ch "'")). |
| 162 |
|
| 163 |
Global Instance show_lines_option {T} `{ShowLines T} : ShowLines (option T) |
| 164 |
:= fun v |
| 165 |
=> match v with |
| 166 |
| Some v |
| 167 |
=> match show_lines v with |
| 168 |
| [s] => ["Show " ++ s] |
| 169 |
| s => "Show"::List.map (String " ") s |
| 170 |
end |
| 171 |
| None => ["None"] |
| 172 |
end. |
| 173 |
Global Instance show_lines_list {T} `{ShowLines T} : ShowLines (list T) |
| 174 |
:= fun ls |
| 175 |
=> let ls := List.map show_lines ls in |
| 176 |
(if List.forallb |
| 177 |
(fun lines => Nat.eqb (List.length lines) 1) |
| 178 |
ls |
| 179 |
then ["[" ++ String.concat ", " (List.map (String.concat String.NewLine) ls) ++ "]"]%string |
| 180 |
else ["["] ++ match ls with |
| 181 |
| nil => ["]"] |
| 182 |
| x :: xs |
| 183 |
=> (List.map (String " ") x) |
| 184 |
++ (List.flat_map |
| 185 |
(fun x' => "," :: List.map (String " ") x') |
| 186 |
xs) |
| 187 |
++ ["]"] |
| 188 |
end)%list. |
| 189 |
Global Instance show_lines_prod {A B} `{ShowLines A, ShowLines B} : ShowLines (A * B) |
| 190 |
:= fun '(a, b) |
| 191 |
=> match show_lines a, show_lines b with |
| 192 |
| [a], [b] => ["(" ++ a ++ ", " ++ b ++ ")"]%string |
| 193 |
| a, b => ["("] ++ a ++ [", "] ++ b ++ [")"] |
| 194 |
end%list. |
| 195 |
|
| 196 |
Module Export Decimal. |
| 197 |
Global Instance show_lvl_positive : ShowLevel positive |
| 198 |
:= fun p => neg_wrap_parens (Decimal.Pos.to_string p). |
| 199 |
Global Instance show_positive : Show positive := show_lvl_positive. |
| 200 |
Global Instance show_lvl_N : ShowLevel N |
| 201 |
:= fun n => neg_wrap_parens |
| 202 |
match n with |
| 203 |
| N0 => "0" |
| 204 |
| Npos p => show p |
| 205 |
end. |
| 206 |
Global Instance show_N : Show N := show_lvl_N. |
| 207 |
Global Instance show_lvl_nat : ShowLevel nat |
| 208 |
:= fun n => show_lvl (N.of_nat n). |
| 209 |
Global Instance show_nat : Show nat := show_lvl_nat. |
| 210 |
Global Instance show_lvl_Z : ShowLevel Z |
| 211 |
:= fun z |
| 212 |
=> match z with |
| 213 |
| Zneg p => show_lvl_unop opp_lvl "-" p |
| 214 |
| Z0 => neg_wrap_parens "0" |
| 215 |
| Zpos p => show_lvl p |
| 216 |
end. |
| 217 |
Global Instance show_Z : Show Z := show_lvl_Z. |
| 218 |
Definition show_lvl_Q_frac : ShowLevel Q |
| 219 |
:= fun q |
| 220 |
=> if (Qden q =? 1)%positive |
| 221 |
then show_lvl (Qnum q) |
| 222 |
else show_lvl_binop |
| 223 |
div_assoc div_lvl |
| 224 |
(Qnum q) " / " (Qden q). |
| 225 |
Definition show_Q_frac : Show Q := show_lvl_Q_frac. |
| 226 |
Global Instance show_lvl_Q : ShowLevel Q |
| 227 |
:= fun q |
| 228 |
=> if (Qden q =? 1)%positive |
| 229 |
then show_lvl (Qnum q) |
| 230 |
else let lg10 := Z.log10 (Z.pos (Qden q)) in |
| 231 |
if (10^lg10 =? Z.pos (Qden q))%Z |
| 232 |
then let lg10 := Z.to_nat lg10 in |
| 233 |
let int_part := show_Z (Z.abs (Qnum q) / Z.pos (Qden q))%Z in |
| 234 |
let dec_part := show_Z (Z.abs (Qnum q) mod (Z.pos (Qden q)))%Z in |
| 235 |
let abs_part |
| 236 |
:= int_part |
| 237 |
++ "." |
| 238 |
++ String.string_of_list_ascii (List.repeat "0"%char (lg10 - length dec_part)) ++ dec_part in |
| 239 |
if (Qnum q <? 0)%Z |
| 240 |
then show_lvl_unop opp_lvl "-" (fun 'tt => abs_part) |
| 241 |
else neg_wrap_parens abs_part |
| 242 |
else |
| 243 |
show_lvl_Q_frac q. |
| 244 |
Global Instance show_Q : Show Q := show_lvl_Q. |
| 245 |
End Decimal. |
| 246 |
|
| 247 |
Module Hex. |
| 248 |
Definition show_lvl_positive : ShowLevel positive |
| 249 |
:= fun p => neg_wrap_parens (HexString.of_pos p). |
| 250 |
Definition show_positive : Show positive := show_lvl_positive. |
| 251 |
Definition show_lvl_Z : ShowLevel Z |
| 252 |
:= fun z => let s := HexString.of_Z (Z.abs z) in |
| 253 |
if z <? 0 |
| 254 |
then show_lvl_unop opp_lvl "-" (fun 'tt => s) |
| 255 |
else neg_wrap_parens s. |
| 256 |
Definition show_Z : Show Z := show_lvl_Z. |
| 257 |
Definition show_lvl_N : ShowLevel N |
| 258 |
:= fun n |
| 259 |
=> match n with |
| 260 |
| N0 => neg_wrap_parens "0" |
| 261 |
| Npos p => show_lvl_positive p |
| 262 |
end. |
| 263 |
Definition show_N : Show N := show_lvl_N. |
| 264 |
Definition show_lvl_nat : ShowLevel nat |
| 265 |
:= fun n => show_lvl_N (N.of_nat n). |
| 266 |
Definition show_nat : Show nat := show_lvl_nat. |
| 267 |
Definition show_lvl_Q : ShowLevel Q |
| 268 |
:= fun q |
| 269 |
=> if (Qden q =? 1)%positive |
| 270 |
then show_lvl_Z (Qnum q) |
| 271 |
else show_lvl_binop |
| 272 |
div_assoc div_lvl |
| 273 |
(show_lvl_Z (Qnum q)) " / " (show_lvl_positive (Qden q)). |
| 274 |
Definition show_Q : Show Q := show_lvl_Q. |
| 275 |
End Hex. |
| 276 |
|
| 277 |
Module PowersOfTwo. |
| 278 |
Fixpoint show_lvl_Z_up_to' (max : nat) (max_dec : Z) (neg : bool) : ShowLevel Z |
| 279 |
:= match max with |
| 280 |
| O => Hex.show_lvl_Z |
| 281 |
| S n' |
| 282 |
=> fun z |
| 283 |
=> if z <? max_dec |
| 284 |
then Decimal.show_lvl_Z z |
| 285 |
else if Z.abs (2^Z.log2_up z - z) <=? Z.abs (z - 2^Z.log2 z) |
| 286 |
then let log2z := Z.log2_up z in |
| 287 |
let z := 2^log2z - z in |
| 288 |
if z =? 0 |
| 289 |
then show_lvl_binop |
| 290 |
pow_assoc pow_lvl |
| 291 |
2 "^" (Decimal.show_lvl_Z log2z) |
| 292 |
else show_lvl_binop |
| 293 |
FullyAssoc add_lvl |
| 294 |
(show_lvl_binop |
| 295 |
pow_assoc pow_lvl |
| 296 |
2 "^" (Decimal.show_lvl_Z log2z)) |
| 297 |
(if neg then "+" else "-") |
| 298 |
(show_lvl_Z_up_to' n' max_dec (negb neg) z) |
| 299 |
else let log2z := Z.log2 z in |
| 300 |
let z := z - 2^log2z in |
| 301 |
if z =? 0 |
| 302 |
then show_lvl_binop |
| 303 |
pow_assoc pow_lvl |
| 304 |
2 "^" (Decimal.show_lvl_Z log2z) |
| 305 |
else show_lvl_binop |
| 306 |
FullyAssoc add_lvl |
| 307 |
(show_lvl_binop |
| 308 |
pow_assoc pow_lvl |
| 309 |
2 "^" (Decimal.show_lvl_Z log2z)) |
| 310 |
(if neg then "-" else "+") |
| 311 |
(show_lvl_Z_up_to' n' max_dec neg z) |
| 312 |
end%Z%string. |
| 313 |
|
| 314 |
Definition show_lvl_Z_up_to (max : nat) (max_dec : Z) : ShowLevel Z |
| 315 |
:= fun z |
| 316 |
=> (if z <? 0 |
| 317 |
then lvl_wrap_parens |
| 318 |
add_lvl |
| 319 |
("-" ++ show_lvl_Z_up_to' max max_dec false (-z) add_lvl) |
| 320 |
else if z =? 0 |
| 321 |
then neg_wrap_parens "0" |
| 322 |
else show_lvl_Z_up_to' max max_dec false z)%Z. |
| 323 |
Definition show_Z_up_to (max : nat) (max_dec : Z) : Show Z |
| 324 |
:= show_lvl_Z_up_to max max_dec. |
| 325 |
Definition show_lvl_Z : ShowLevel Z |
| 326 |
:= fun z => show_lvl_Z_up_to (S (Z.to_nat (Z.log2_up (Z.abs z)))) 32 z. |
| 327 |
Definition show_Z : Show Z |
| 328 |
:= show_lvl_Z. |
| 329 |
End PowersOfTwo. |