| 1 |
From Coq Require Import Ascii String List. |
| 2 |
From Coq Require Import BinNat. |
| 3 |
From Coq Require Import QArith. |
| 4 |
From Coq Require Import BinInt. |
| 5 |
Require Import Crypto.Util.Option. |
| 6 |
From Coq Require BinaryString. |
| 7 |
From Coq Require OctalString. |
| 8 |
From Coq Require HexString. |
| 9 |
Require Import Crypto.Util.Strings.Decimal. |
| 10 |
Require Import Crypto.Util.Strings.Parse.Common. |
| 11 |
Require Import Crypto.Util.Option. |
| 12 |
Require Import Crypto.Util.OptionList. |
| 13 |
Require Import Crypto.Util.ListUtil.GroupAllBy. |
| 14 |
Require Import Crypto.Util.Notations. |
| 15 |
Import ListNotations. |
| 16 |
Import BinPosDef. |
| 17 |
Local Set Implicit Arguments. |
| 18 |
Local Open Scope option_scope. |
| 19 |
Local Open Scope list_scope. |
| 20 |
Local Open Scope char_scope. |
| 21 |
Local Open Scope string_scope. |
| 22 |
Local Open Scope Z_scope. |
| 23 |
Local Open Scope nat_scope. |
| 24 |
|
| 25 |
Definition is_num (ch : ascii) : bool |
| 26 |
:= match DecimalString.uint_of_char ch (Some Decimal.Nil) with Some _ => true | None => false end. |
| 27 |
|
| 28 |
Definition is_oct_num (ch : ascii) : bool |
| 29 |
:= match OctalString.ascii_to_digit ch with Some _ => true | None => false end. |
| 30 |
|
| 31 |
Definition is_hex_num (ch : ascii) : bool |
| 32 |
:= match HexString.ascii_to_digit ch with Some _ => true | None => false end. |
| 33 |
|
| 34 |
Definition startswith_oct (s : string) : bool |
| 35 |
:= match s with |
| 36 |
| String zero (String kind (String d rest)) |
| 37 |
=> (zero =? "0")%char && (kind =? "o")%char && is_oct_num d |
| 38 |
| _ => false |
| 39 |
end. |
| 40 |
|
| 41 |
Definition startswith_hex (s : string) : bool |
| 42 |
:= match s with |
| 43 |
| String zero (String kind (String d rest)) |
| 44 |
=> (zero =? "0")%char && (kind =? "x")%char && is_hex_num d |
| 45 |
| _ => false |
| 46 |
end. |
| 47 |
|
| 48 |
Definition parse_bin_prefix : ParserAction _ := "0b". |
| 49 |
Definition parse_oct_prefix : ParserAction _ := "0o". |
| 50 |
Definition parse_dec_prefix : ParserAction _ := "". |
| 51 |
Definition parse_hex_prefix : ParserAction _ := "0x". |
| 52 |
Definition parse_hex_postfix : ParserAction _ := "h". |
| 53 |
|
| 54 |
(** Is [E]/[e] a valid digit in the given base? *) |
| 55 |
Definition base_accepts_E (base : N) : bool |
| 56 |
:= (10 + N_of_ascii "E" - N_of_ascii "A" <? base)%N. |
| 57 |
|
| 58 |
Definition parse_digits_gen_step (base : N) : ParserAction N |
| 59 |
:= (parse_strs |
| 60 |
(List.flat_map |
| 61 |
(fun v => if (v <? 10)%N |
| 62 |
then [(String (ascii_of_N (v + N_of_ascii "0")) "", v)] |
| 63 |
else [(String (ascii_of_N (v - 10 + N_of_ascii "A")) "", v); |
| 64 |
(String (ascii_of_N (v - 10 + N_of_ascii "a")) "", v)]) |
| 65 |
(List.map N.of_nat (List.seq 0 (N.to_nat base))))). |
| 66 |
|
| 67 |
(* |
| 68 |
Definition parse_oct_digits : ParserAction (list N) |
| 69 |
:= Eval cbv -[ParserAction parse_alt_gen parse_impossible parse_str parse_star] in |
| 70 |
parse_digits_gen 8. |
| 71 |
Definition parse_dec_digits : ParserAction (list N) |
| 72 |
:= Eval cbv -[ParserAction parse_alt_gen parse_impossible parse_str parse_star] in |
| 73 |
parse_digits_gen 10. |
| 74 |
Definition parse_hex_digits : ParserAction (list N) |
| 75 |
:= Eval cbv -[ParserAction parse_alt_gen parse_impossible parse_str parse_star] in |
| 76 |
parse_digits_gen 16. |
| 77 |
*) |
| 78 |
|
| 79 |
Definition digits_to_N (base : N) (digits : list N) : N |
| 80 |
:= List.fold_left |
| 81 |
(fun so_far d => base * so_far + d)%N |
| 82 |
digits |
| 83 |
0%N. |
| 84 |
|
| 85 |
Redirect "log" Check eq_refl : digits_to_N 10 [1;2;3;4;5;6]%N = 123456%N. |
| 86 |
|
| 87 |
Local Coercion N.of_nat : nat >-> N. |
| 88 |
Local Coercion Z.of_N : N >-> Z. |
| 89 |
Local Coercion inject_Z : Z >-> Q. |
| 90 |
|
| 91 |
Definition parse_N_digits (base : N) (first_digit_numeric : bool) : ParserAction N |
| 92 |
:= (parse_map (digits_to_N base) ((parse_digits_gen_step (if first_digit_numeric then N.min 10 base else base);;->{cons}(parse_digits_gen_step base)* ))). |
| 93 |
|
| 94 |
Definition parse_N_fixed_digits (base : N) (first_digit_numeric : bool) (n : nat) : ParserAction N := ( |
| 95 |
let parse_first_digit := parse_digits_gen_step (if first_digit_numeric then N.min 10 base else base) in |
| 96 |
let parse_nil := parse_map (fun _ => nil) "" in |
| 97 |
parse_map (digits_to_N base) |
| 98 |
match n with |
| 99 |
| O => parse_nil |
| 100 |
| S n => |
| 101 |
fold_right (fun p ps => p ;;->{cons} ps) |
| 102 |
parse_nil |
| 103 |
(parse_first_digit :: List.repeat (parse_digits_gen_step base) n) |
| 104 |
end)%parse. |
| 105 |
|
| 106 |
Notation parse_oct_char := (parse_digits_gen_step 8 false 1). |
| 107 |
Notation parse_hex_char := (parse_digits_gen_step 16 false 1). |
| 108 |
|
| 109 |
Definition parse_num_gen {P P'} (allow_neg : bool) (base : N) (parse_prefix : option (ParserAction P)) (parse_postfix : option (ParserAction P')) : ParserAction Q |
| 110 |
:= (let parse_E_exponent |
| 111 |
:= (("e" || "E") ;;->{ fun _ v => Qpower 10 v } |
| 112 |
(((strip_whitespace_after "-")?) |
| 113 |
;;->{ fun n (v:N) => if n:option _ then (-v)%Z else v } |
| 114 |
(parse_N_digits base false)))%parse in |
| 115 |
let parse_P_exponent |
| 116 |
:= (("p" || "P") ;;->{ fun _ v => Qpower 2 v } |
| 117 |
(((strip_whitespace_after "-")?) |
| 118 |
;;->{ fun n (v:N) => if n:option _ then (-v)%Z else v } |
| 119 |
(parse_N_digits base false)))%parse in |
| 120 |
(if allow_neg then ((strip_whitespace_after "-")?) else parse_map (fun _ => None) "") |
| 121 |
;;->{ fun n v => if n:option _ then (-v)%Q else v } |
| 122 |
let parse_main first_digit_numeric |
| 123 |
:= ((((parse_N_digits base first_digit_numeric) |
| 124 |
;;->{ fun (d:N) decimals |
| 125 |
=> (d |
| 126 |
+ digits_to_N base decimals / base^List.length decimals)%Q } |
| 127 |
"." ;;R |
| 128 |
(parse_digits_gen_step base)* ) |
| 129 |
|| parse_map (digits_to_N base : _ -> Q) ((parse_digits_gen_step (if first_digit_numeric then N.min 10 base else base))+))) |
| 130 |
;;->{ fun n e => match e with Some e => n * e | None => n end%Q } |
| 131 |
((if base_accepts_E base |
| 132 |
then parse_P_exponent (* if [e] is a valid character in the base, then we don't permit [e] as an exponent *) |
| 133 |
else (parse_E_exponent || parse_P_exponent))?) in |
| 134 |
match parse_prefix, parse_postfix with |
| 135 |
| None, None => parse_main true |
| 136 |
| Some parse_prefix, None => parse_prefix ;;R parse_main false |
| 137 |
| Some parse_prefix, Some parse_postfix => parse_prefix ;;R parse_main false ;;L parse_postfix |
| 138 |
| None, Some parse_postfix => parse_main true ;;L parse_postfix |
| 139 |
end)%parse. |
| 140 |
|
| 141 |
Definition parse_num_gen_prefix {P} (allow_neg : bool) (base : N) (parse_prefix : ParserAction P) : ParserAction Q |
| 142 |
:= @parse_num_gen P unit allow_neg base (Some parse_prefix) None. |
| 143 |
Definition parse_num_gen_postfix {P} (allow_neg : bool) (base : N) (parse_postfix : ParserAction P) : ParserAction Q |
| 144 |
:= @parse_num_gen unit P allow_neg base None (Some parse_postfix). |
| 145 |
|
| 146 |
Definition parse_num (allow_neg : bool) : ParserAction Q |
| 147 |
:= parse_num_gen_prefix allow_neg 2 parse_bin_prefix |
| 148 |
|| parse_num_gen_prefix allow_neg 8 parse_oct_prefix |
| 149 |
|| parse_num_gen_prefix allow_neg 10 parse_dec_prefix |
| 150 |
|| parse_num_gen_prefix allow_neg 16 parse_hex_prefix |
| 151 |
|| parse_num_gen_postfix allow_neg 16 parse_hex_postfix. |
| 152 |
|
| 153 |
Definition parse_int_gen {P P'} (allow_neg : bool) (base : N) (parse_prefix : option (ParserAction P)) (parse_postfix : option (ParserAction P')) : ParserAction Z |
| 154 |
:= (let parse_E_exponent |
| 155 |
:= (("e" || "E") ;;->{ fun _ (v:N) => Z.pow 10 v } (parse_N_digits base false))%parse in |
| 156 |
let parse_P_exponent |
| 157 |
:= (("p" || "P") ;;->{ fun _ (v:N) => Z.pow 2 v } (parse_N_digits base false))%parse in |
| 158 |
(if allow_neg then ((strip_whitespace_after "-")?) else parse_map (fun _ => None) "") |
| 159 |
;;->{ fun n v => if n:option _ then (-v)%Z else v } |
| 160 |
let parse_main first_digit_numeric |
| 161 |
:= (parse_N_digits base first_digit_numeric) |
| 162 |
;;->{ fun (n:N) e => match e with Some e => n * e | None => n end%Z } |
| 163 |
((if base_accepts_E base |
| 164 |
then parse_P_exponent (* if [e] is a valid character in the base, then we don't permit [e] as an exponent *) |
| 165 |
else (parse_E_exponent || parse_P_exponent))?) in |
| 166 |
match parse_prefix, parse_postfix with |
| 167 |
| None, None => parse_main true |
| 168 |
| Some parse_prefix, None => parse_prefix ;;R parse_main false |
| 169 |
| Some parse_prefix, Some parse_postfix => parse_prefix ;;R parse_main false ;;L parse_postfix |
| 170 |
| None, Some parse_postfix => parse_main true ;;L parse_postfix |
| 171 |
end)%parse. |
| 172 |
|
| 173 |
Definition parse_int_gen_prefix {P} (allow_neg : bool) (base : N) (parse_prefix : ParserAction P) : ParserAction Z |
| 174 |
:= @parse_int_gen P unit allow_neg base (Some parse_prefix) None. |
| 175 |
Definition parse_int_gen_postfix {P} (allow_neg : bool) (base : N) (parse_postfix : ParserAction P) : ParserAction Z |
| 176 |
:= @parse_int_gen unit P allow_neg base None (Some parse_postfix). |
| 177 |
|
| 178 |
Definition parse_int (allow_neg : bool) : ParserAction Z |
| 179 |
:= parse_int_gen_prefix allow_neg 2 parse_bin_prefix |
| 180 |
|| parse_int_gen_prefix allow_neg 8 parse_oct_prefix |
| 181 |
|| parse_int_gen_prefix allow_neg 10 parse_dec_prefix |
| 182 |
|| parse_int_gen_prefix allow_neg 16 parse_hex_prefix |
| 183 |
|| parse_int_gen_postfix allow_neg 16 parse_hex_postfix. |
| 184 |
|
| 185 |
Redirect "log" Check let ls := [("-1234", -(1234):Q); ("0xF", 15:Q); ("10.5", (10 + 5/10)); ("-10.5", -(10+5/10))]%Q in |
| 186 |
eq_refl |
| 187 |
: List.map (fun '(s, v) => ((parse_num true;;->{fun v _ => v} ε)%parse s )) ls |
| 188 |
= List.map (fun '(s, v) => [(v, "")]) ls. |
| 189 |
|
| 190 |
(* This was previously stack overflowing due to treating [e] as an exponent *) |
| 191 |
Redirect "log" Compute parse_num false "0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551". |
| 192 |
|
| 193 |
Inductive Qexpr_poly {var : Type} := Qvar (v : var) | Qv (_ : Q) | Qeopp (a : Qexpr_poly) | Qeadd (a b : Qexpr_poly) | Qesub (a b : Qexpr_poly) | Qemul (a b : Qexpr_poly) | Qediv (a b : Qexpr_poly) | Qepow (b e : Qexpr_poly). |
| 194 |
Global Arguments Qexpr_poly var : clear implicits. |
| 195 |
Declare Scope Qexpr_scope. |
| 196 |
Delimit Scope Qexpr_scope with Qexpr. |
| 197 |
Bind Scope Qexpr_scope with Qexpr_poly. |
| 198 |
Infix "^" := Qepow : Qexpr_scope. |
| 199 |
Infix "*" := Qemul : Qexpr_scope. |
| 200 |
Infix "+" := Qeadd : Qexpr_scope. |
| 201 |
Infix "/" := Qediv : Qexpr_scope. |
| 202 |
Infix "-" := Qesub : Qexpr_scope. |
| 203 |
Notation "- x" := (Qeopp x) : Qexpr_scope. |
| 204 |
Notation Qexpr := (@Qexpr_poly Empty_set). |
| 205 |
Coercion QvQ (x : Q) : Qexpr := Qv x. |
| 206 |
|
| 207 |
Definition monomial_in T var := (T * (var * Z) * list (var * Z (* approximate exponent *)))%type. |
| 208 |
Definition poly_in T var := (T * list (monomial_in T var))%type. |
| 209 |
Definition reduce_monomial {T var} (var_beq : var -> var -> bool) (m : monomial_in T var) : T + monomial_in T var |
| 210 |
:= let '(c, v, vs) := m in |
| 211 |
let vs := List.groupAllBy (fun '(v, _) '(v', _) => var_beq v v') (v :: vs) in |
| 212 |
match Option.List.map |
| 213 |
(fun vs |
| 214 |
=> match vs with |
| 215 |
| [] => None |
| 216 |
| (v, _) :: _ |
| 217 |
=> let e := fold_right Z.add 0%Z (List.map snd vs) in |
| 218 |
if (Z.eqb e 0) |
| 219 |
then None |
| 220 |
else Some (v, e) |
| 221 |
end) |
| 222 |
vs with |
| 223 |
| [] => inl c |
| 224 |
| v :: vs => inr (c, v, vs) |
| 225 |
end. |
| 226 |
Definition const_poly {T var} (v : T) : poly_in T var := (v, []). |
| 227 |
Definition var_monomial {T var} (one : T) (v : var) : monomial_in T var := (one, (v, 1%Z), []). |
| 228 |
Definition var_poly {T var} (zero : T) (one : T) (v : var) : poly_in T var := (zero, [var_monomial one v]). |
| 229 |
Definition add_poly {T var} (addT : T -> T -> T) (p1 p2 : poly_in T var) : poly_in T var |
| 230 |
:= let '(c1, p1) := p1 in |
| 231 |
let '(c2, p2) := p2 in |
| 232 |
(addT c1 c2, p1 ++ p2)%list. |
| 233 |
Definition mul_monomial {T var} (mulT : T -> T -> T) (m1 m2 : monomial_in T var) : monomial_in T var |
| 234 |
:= let '(coef1, (v1, e1), vs1) := m1 in |
| 235 |
let '(coef2, (v2, e2), vs2) := m2 in |
| 236 |
(mulT coef1 coef2, (v1, e1), vs1 ++ (v2, e2) :: vs2)%list. |
| 237 |
Definition scmul_monomial {T var} (mulT : T -> T -> T) (sc : T) (m : monomial_in T var) : monomial_in T var |
| 238 |
:= let '(coef, (v, e), vs) := m in |
| 239 |
(mulT sc coef, (v, e), vs). |
| 240 |
Definition mul_poly {T var} (addT : T -> T -> T) (mulT : T -> T -> T) (p1 p2 : poly_in T var) : poly_in T var |
| 241 |
:= let '(c1, p1) := p1 in |
| 242 |
let '(c2, p2) := p2 in |
| 243 |
(mulT c1 c2, List.map (scmul_monomial mulT c1) p2 ++ List.map (scmul_monomial mulT c2) p1 ++ List.flat_map (fun p => List.map (mul_monomial mulT p) p2) p1)%list. |
| 244 |
Definition pow_monomial {T var} (powT : T -> Z -> T) (m : monomial_in T var) (e : Z) : monomial_in T var |
| 245 |
:= let '(coef, (v, ev), vs) := m in |
| 246 |
(powT coef e, (v, ev + e), List.map (fun '(v, ev) => (v, ev + e)) vs)%Z. |
| 247 |
Definition pow_monomial_strict {T var} (powT : T -> Z -> option T) (m : monomial_in T var) (e : Z) : option (monomial_in T var) |
| 248 |
:= let '(coef, (v, ev), vs) := m in |
| 249 |
(coef <- powT coef e; |
| 250 |
Some (coef, (v, ev + e), List.map (fun '(v, ev) => (v, ev + e)) vs))%Z%option. |
| 251 |
Definition pow_poly {T var} (addT : T -> T -> T) (mulT : T -> T -> T) (powT : T -> Z -> T) (one : T) (p : poly_in T var) (e : Z) : poly_in T var |
| 252 |
:= match p with |
| 253 |
| (c, []) => (powT c e, []) |
| 254 |
| (c, _) |
| 255 |
=> match e with |
| 256 |
| Z.neg _ => (powT c e, []) (* approximate *) |
| 257 |
| Z0 => (one, []) |
| 258 |
| Z.pos e => BinPos.Pos.iter (mul_poly addT mulT p) (const_poly one) e |
| 259 |
end |
| 260 |
end. |
| 261 |
Definition pow_poly_strict {T var} (addT : T -> T -> T) (mulT : T -> T -> T) (powT : T -> Z -> option T) (one : T) (p : poly_in T var) (e : Z) : option (poly_in T var) |
| 262 |
:= match p with |
| 263 |
| (c, []) => c <- powT c e; Some (c, []) |
| 264 |
| (c, _) |
| 265 |
=> match e with |
| 266 |
| Z.neg _ => None |
| 267 |
| Z0 => Some (one, []) |
| 268 |
| Z.pos e => Some (BinPos.Pos.iter (mul_poly addT mulT p) (const_poly one) e) |
| 269 |
end |
| 270 |
end%option. |
| 271 |
Definition monomial_var_part_beq {T var} (var_beq : var -> var -> bool) (m1 m2 : monomial_in T var) : bool |
| 272 |
:= match reduce_monomial var_beq (mul_monomial (fun a _ => a) m1 (pow_monomial (fun a _ => a) m2 (-1))) with |
| 273 |
| inl _ => true (* the var parts cancel, and we have only a constant part *) |
| 274 |
| inr _ => false (* var parts don't cancel, so must be different *) |
| 275 |
end. |
| 276 |
Definition reduce_poly {T var} (var_beq : var -> var -> bool) (T_zero_beq : T -> bool) (reduceT : T -> T) (addT : T -> T -> T) (p : poly_in T var) : poly_in T var |
| 277 |
:= let '(c, vs) := p in |
| 278 |
let vs := List.map (reduce_monomial var_beq) vs in |
| 279 |
let cs := Option.List.map (fun v => match v with inl v => Some v | inr _ => None end) vs in |
| 280 |
let vs := Option.List.map (fun v => match v with inr v => Some v | inl _ => None end) vs in |
| 281 |
let c := fold_right addT c cs in |
| 282 |
let vs := List.groupAllBy (monomial_var_part_beq var_beq) vs in |
| 283 |
(reduceT c, |
| 284 |
Option.List.map |
| 285 |
(fun vs |
| 286 |
=> match vs with |
| 287 |
| [] => None |
| 288 |
| (c, v0, v0s) :: vs |
| 289 |
=> let c := reduceT (fold_right addT c (List.map (fun '(c, _, _) => c) vs)) in |
| 290 |
if T_zero_beq c |
| 291 |
then None |
| 292 |
else Some (c, v0, v0s) |
| 293 |
end) |
| 294 |
vs). |
| 295 |
|
| 296 |
Definition reduce_Qpoly {var} (var_beq : var -> var -> bool) : poly_in Q var -> poly_in Q var |
| 297 |
:= reduce_poly var_beq (Qeq_bool 0) Qred Qplus. |
| 298 |
|
| 299 |
Local Notation add_Qpoly := (add_poly Qplus). |
| 300 |
Local Notation mul_Qpoly := (mul_poly Qplus Qmult). |
| 301 |
Local Notation pow_Qpoly := (pow_poly Qplus Qmult Qpower 1%Q). |
| 302 |
Local Notation pow_Qpoly_strict := (pow_poly_strict Qplus Qmult (fun x y => if andb (Qeq_bool x 0) (Z.leb y 0) then None else Some (Qpower x y)) 1%Q). |
| 303 |
Local Notation opp_Qpoly := (mul_Qpoly (const_poly (-1)%Q)). |
| 304 |
Local Notation sub_Qpoly a b := (add_Qpoly a (opp_Qpoly b)). |
| 305 |
|
| 306 |
Definition poly_coef_map {T1 T2 var} (f : T1 -> T2) (p : poly_in T1 var) : poly_in T2 var |
| 307 |
:= let '(c, vs) := p in |
| 308 |
(f c, List.map (fun '(c, v, vs) => (f c, v, vs)) vs). |
| 309 |
|
| 310 |
Definition poly_coef_map_strict {T1 T2 var} (f : T1 -> option T2) (p : poly_in T1 var) : option (poly_in T2 var) |
| 311 |
:= let '(c, vs) := p in |
| 312 |
(c <- f c; |
| 313 |
vs <-- List.map (fun '(c, v, vs) => c <- f c; Some (c, v, vs)) vs; |
| 314 |
Some (c, vs))%option. |
| 315 |
|
| 316 |
Fixpoint eval_Qexpr_poly {var} (v : Qexpr_poly var) : poly_in Q var |
| 317 |
:= match v with |
| 318 |
| Qvar v => var_poly 0 1 v |
| 319 |
| Qv x => const_poly x |
| 320 |
| Qeopp a => opp_Qpoly (eval_Qexpr_poly a) |
| 321 |
| Qeadd a b => add_Qpoly (eval_Qexpr_poly a) (eval_Qexpr_poly b) |
| 322 |
| Qesub a b => sub_Qpoly (eval_Qexpr_poly a) (eval_Qexpr_poly b) |
| 323 |
| Qemul a b => mul_Qpoly (eval_Qexpr_poly a) (eval_Qexpr_poly b) |
| 324 |
| Qediv a b => mul_Qpoly (eval_Qexpr_poly a) (pow_Qpoly (eval_Qexpr_poly b) (-1)) |
| 325 |
| Qepow b e => let b := eval_Qexpr_poly b in |
| 326 |
let '(e, _) := eval_Qexpr_poly e in |
| 327 |
let (qe, re) := Z.div_eucl (Qnum e) (Z.pos (Qden e)) in |
| 328 |
(* (b^qe)*(b^(re/Qden e)) *) |
| 329 |
mul_Qpoly (pow_Qpoly b qe) (pow_Qpoly b (* approximate *) (re / Z.pos (Qden e))) |
| 330 |
end%Q. |
| 331 |
|
| 332 |
Definition eval_Qexpr (v : Qexpr) : Q |
| 333 |
:= match eval_Qexpr_poly v with |
| 334 |
| (c, []) => c |
| 335 |
| (c, (_, (v, _), _)::_) => match v with end |
| 336 |
end. |
| 337 |
|
| 338 |
Fixpoint eval_Qexpr_poly_strict {var} (v : Qexpr_poly var) : option (poly_in Q var) |
| 339 |
:= match v with |
| 340 |
| Qvar v => Some (var_poly 0%Q 1%Q v) |
| 341 |
| Qv x => Some (const_poly x) |
| 342 |
| Qeopp a => option_map opp_Qpoly (eval_Qexpr_poly_strict a) |
| 343 |
| Qeadd a b => (a <- eval_Qexpr_poly_strict a; b <- eval_Qexpr_poly_strict b; Some (add_Qpoly a b)) |
| 344 |
| Qesub a b => (a <- eval_Qexpr_poly_strict a; b <- eval_Qexpr_poly_strict b; Some (sub_Qpoly a b)) |
| 345 |
| Qemul a b => (a <- eval_Qexpr_poly_strict a; b <- eval_Qexpr_poly_strict b; Some (mul_Qpoly a b)) |
| 346 |
| Qediv a b => (a <- eval_Qexpr_poly_strict a; |
| 347 |
b <- eval_Qexpr_poly_strict b; |
| 348 |
b <- pow_Qpoly_strict b (-1); |
| 349 |
Some (mul_Qpoly a b)) |
| 350 |
| Qepow b e => (b <- eval_Qexpr_poly_strict b; |
| 351 |
e <- eval_Qexpr_poly_strict e; |
| 352 |
match e with |
| 353 |
| (_, _::_) => None |
| 354 |
| (e, []) |
| 355 |
=> let (qe, re) := Z.div_eucl (Qnum e) (Z.pos (Qden e)) in |
| 356 |
if Z.eqb re 0 |
| 357 |
then pow_Qpoly_strict b qe |
| 358 |
else None |
| 359 |
end) |
| 360 |
end. |
| 361 |
|
| 362 |
Definition eval_Qexpr_strict (v : Qexpr) : option Q |
| 363 |
:= match eval_Qexpr_poly_strict v with |
| 364 |
| Some (c, []) => Some c |
| 365 |
| Some (c, (_, (v, _), _)::_) => match v with end |
| 366 |
| None => None |
| 367 |
end. |
| 368 |
|
| 369 |
Definition eval_Qexpr_poly_reduced {var} (var_beq : var -> var -> bool) (v : Qexpr_poly var) : poly_in Q var |
| 370 |
:= reduce_Qpoly var_beq (eval_Qexpr_poly v). |
| 371 |
|
| 372 |
Definition eval_Qexpr_poly_reduced_strict {var} (var_beq : var -> var -> bool) (v : Qexpr_poly var) : option (poly_in Q var) |
| 373 |
:= option_map (reduce_Qpoly var_beq) (eval_Qexpr_poly_strict v). |
| 374 |
|
| 375 |
Definition parse_ops {A} (ls : list (string * (A -> A -> A))) (parse : ParserAction A) : ParserAction A |
| 376 |
:= parse ;;->{fun l fs => List.fold_left (fun v f => f v) fs l} |
| 377 |
(strip_whitespace_around (parse_strs ls) ;;->{fun f r l => f l r} |
| 378 |
parse)*. |
| 379 |
|
| 380 |
Definition parse_prefix_ops {A} (ls : list (string * (A -> A))) (parse : ParserAction A) : ParserAction A |
| 381 |
:= ((strip_whitespace_after (parse_strs ls))* ) |
| 382 |
;;->{fun ls v => fold_right (fun f x => f x) v ls } |
| 383 |
parse. |
| 384 |
|
| 385 |
Section step. |
| 386 |
Context {var : Type} |
| 387 |
(parse_num : ParserAction Q) |
| 388 |
(parse_var : ParserAction var). |
| 389 |
Local Notation Qexpr_poly := (Qexpr_poly var). |
| 390 |
Context (parse : ParserAction Qexpr_poly). |
| 391 |
|
| 392 |
Definition parseQexpr_poly_gen_parens : ParserAction Qexpr_poly |
| 393 |
:= ("(" ;;->{fun _ v => v} strip_whitespace_around parse ;;->{fun v _ => v} ")") |
| 394 |
|| parse_map Qv parse_num |
| 395 |
|| parse_map Qvar parse_var. |
| 396 |
Definition parseQexpr_poly_gen_exp : ParserAction Qexpr_poly |
| 397 |
:= parse_ops [("^", Qepow)] parseQexpr_poly_gen_parens. |
| 398 |
Definition parseQexpr_poly_gen_opp : ParserAction Qexpr_poly |
| 399 |
:= parse_prefix_ops [("-", Qeopp)] parseQexpr_poly_gen_exp. |
| 400 |
Definition parseQexpr_poly_gen_mul_div : ParserAction Qexpr_poly |
| 401 |
:= parse_ops [("*", Qemul); ("/", Qediv)] parseQexpr_poly_gen_opp. |
| 402 |
Definition parseQexpr_poly_gen_add_sub : ParserAction Qexpr_poly |
| 403 |
:= parse_ops [("+", Qeadd); ("-", Qesub)] parseQexpr_poly_gen_mul_div. |
| 404 |
End step. |
| 405 |
|
| 406 |
Fixpoint parse_Qexpr_poly_fueled {var} (parse_num : ParserAction Q) (parse_var : ParserAction var) (fuel : nat) : ParserAction (Qexpr_poly var) |
| 407 |
:= strip_whitespace_around |
| 408 |
(parseQexpr_poly_gen_add_sub |
| 409 |
parse_num |
| 410 |
parse_var |
| 411 |
(match fuel with |
| 412 |
| O => parse_impossible |
| 413 |
| S fuel => parse_Qexpr_poly_fueled parse_num parse_var fuel |
| 414 |
end)). |
| 415 |
|
| 416 |
Definition parse_Qexpr_poly_gen {var} (parse_num : ParserAction Q) (parse_var : ParserAction var) : ParserAction (Qexpr_poly var) := fuel (parse_Qexpr_poly_fueled parse_num parse_var). |
| 417 |
|
| 418 |
Definition parse_Qexpr_gen (parse_num : ParserAction Q) : ParserAction Qexpr := parse_Qexpr_poly_gen parse_num parse_impossible. |
| 419 |
|
| 420 |
Definition parse_Qexpr_poly {var} (parse_var : ParserAction var) : ParserAction (Qexpr_poly var) := parse_Qexpr_poly_gen (parse_num false) parse_var. |
| 421 |
|
| 422 |
Definition parse_Qexpr_with_vars (vars : list (string * Q)) : ParserAction Qexpr |
| 423 |
:= parse_Qexpr_poly_gen |
| 424 |
(List.fold_right |
| 425 |
(fun '((s, q) : string * Q) default => default || parse_map (fun _ => q) s)%parse |
| 426 |
(parse_num false) |
| 427 |
vars) |
| 428 |
parse_impossible. |
| 429 |
|
| 430 |
Definition Q_to_Z (q : Q) : Z := (Qnum q / Z.pos (Qden q))%Z. |
| 431 |
Definition Q_to_Z_strict (x : Q) : option Z |
| 432 |
:= let '(q, r) := Z.div_eucl (Qnum x) (Zpos (Qden x)) in |
| 433 |
if Z.eqb r 0 |
| 434 |
then Some q |
| 435 |
else None. |
| 436 |
Definition N_of_Z_strict (z : Z) : option N |
| 437 |
:= match z with Z0 => Some N0 | Zpos p => Some (Npos p) | Zneg _ => None end. |
| 438 |
Definition positive_of_N (n : N) : positive |
| 439 |
:= match n with N0 => 1%positive | Npos p => p end. |
| 440 |
Definition positive_of_N_strict (n : N) : option positive |
| 441 |
:= match n with N0 => None | Npos p => Some p end. |
| 442 |
|
| 443 |
Section with_var. |
| 444 |
Context {var : Type} |
| 445 |
(var_beq : var -> var -> bool) |
| 446 |
(parse_var : ParserAction var). |
| 447 |
|
| 448 |
Definition parse_Q_with_vars (vars : list (string * Q)) : ParserAction Q |
| 449 |
:= parse_map eval_Qexpr (parse_Qexpr_with_vars vars). |
| 450 |
Definition parse_Q_strict_with_vars (vars : list (string * Q)) : ParserAction Q |
| 451 |
:= parse_option_list_map eval_Qexpr_strict (parse_Qexpr_with_vars vars). |
| 452 |
Definition parse_Q_poly : ParserAction (poly_in Q var) |
| 453 |
:= parse_map (eval_Qexpr_poly_reduced var_beq) (parse_Qexpr_poly parse_var). |
| 454 |
Definition parse_Q_poly_strict : ParserAction (poly_in Q var) |
| 455 |
:= parse_option_list_map (eval_Qexpr_poly_reduced_strict var_beq) (parse_Qexpr_poly parse_var). |
| 456 |
Definition parse_Z_with_vars (vars : list (string * Q)) : ParserAction Z |
| 457 |
:= parse_map Q_to_Z (parse_Q_with_vars vars). |
| 458 |
Definition parse_Z_strict_with_vars (vars : list (string * Q)) : ParserAction Z |
| 459 |
:= parse_option_list_map Q_to_Z_strict (parse_Q_strict_with_vars vars). |
| 460 |
Definition parse_Z_poly : ParserAction (poly_in Z var) |
| 461 |
:= parse_map (poly_coef_map Q_to_Z) parse_Q_poly. |
| 462 |
Definition parse_Z_poly_strict : ParserAction (poly_in Z var) |
| 463 |
:= parse_option_list_map (poly_coef_map_strict Q_to_Z_strict) parse_Q_poly_strict. |
| 464 |
Definition parse_N_with_vars (vars : list (string * Q)) : ParserAction N |
| 465 |
:= parse_map Z.to_N (parse_Z_with_vars vars). |
| 466 |
Definition parse_N_strict_with_vars (vars : list (string * Q)) : ParserAction N |
| 467 |
:= parse_option_list_map N_of_Z_strict (parse_Z_strict_with_vars vars). |
| 468 |
Definition parse_N_poly : ParserAction (poly_in N var) |
| 469 |
:= parse_map (poly_coef_map Z.to_N) parse_Z_poly. |
| 470 |
Definition parse_N_poly_strict : ParserAction (poly_in N var) |
| 471 |
:= parse_option_list_map (poly_coef_map_strict N_of_Z_strict) parse_Z_poly_strict. |
| 472 |
Definition parse_nat_with_vars (vars : list (string * Q)) : ParserAction nat |
| 473 |
:= parse_map N.to_nat (parse_N_with_vars vars). |
| 474 |
Definition parse_nat_strict_with_vars (vars : list (string * Q)) : ParserAction nat |
| 475 |
:= parse_map N.to_nat (parse_N_strict_with_vars vars). |
| 476 |
Definition parse_nat_poly : ParserAction (poly_in nat var) |
| 477 |
:= parse_map (poly_coef_map N.to_nat) parse_N_poly. |
| 478 |
Definition parse_nat_poly_strict : ParserAction (poly_in nat var) |
| 479 |
:= parse_map (poly_coef_map N.to_nat) parse_N_poly_strict. |
| 480 |
Definition parse_positive_with_vars (vars : list (string * Q)) : ParserAction positive |
| 481 |
:= parse_map positive_of_N (parse_N_with_vars vars). |
| 482 |
Definition parse_positive_strict_with_vars (vars : list (string * Q)) : ParserAction positive |
| 483 |
:= parse_option_list_map positive_of_N_strict (parse_N_strict_with_vars vars). |
| 484 |
Definition parse_positive_poly : ParserAction (poly_in positive var) |
| 485 |
:= parse_map (poly_coef_map positive_of_N) parse_N_poly. |
| 486 |
Definition parse_positive_poly_strict : ParserAction (poly_in positive var) |
| 487 |
:= parse_option_list_map (poly_coef_map_strict positive_of_N_strict) parse_N_poly_strict. |
| 488 |
|
| 489 |
Definition parseQexpr_arith_with_vars (vars : list (string * Q)) (s : string) : option Qexpr |
| 490 |
:= finalize (parse_Qexpr_with_vars vars) s. |
| 491 |
Definition parseQ_arith_with_vars (vars : list (string * Q)) (s : string) : option Q |
| 492 |
:= option_map eval_Qexpr (parseQexpr_arith_with_vars vars s). |
| 493 |
Definition parseZ_arith_with_vars (vars : list (string * Q)) (s : string) : option Z |
| 494 |
:= (q <- parseQ_arith_with_vars vars s; Some (Qnum q / Z.pos (Qden q)))%Z%option. |
| 495 |
Definition parsepositive_arith_with_vars (vars : list (string * Q)) (s : string) : option positive |
| 496 |
:= (z <- parseZ_arith_with_vars vars s; match z with Z0 => None | Zpos p => Some p | Zneg _ => None end). |
| 497 |
Definition parseN_arith_with_vars (vars : list (string * Q)) (s : string) : option N |
| 498 |
:= (z <- parseZ_arith_with_vars vars s; match z with Z0 => Some 0 | Zpos p => Some (Npos p) | Zneg _ => None end)%N%option. |
| 499 |
Definition parsenat_arith_with_vars (vars : list (string * Q)) (s : string) : option nat |
| 500 |
:= (n <- parseN_arith_with_vars vars s; Some (N.to_nat n)). |
| 501 |
|
| 502 |
Definition parseQ_arith_strict_with_vars (vars : list (string * Q)) (s : string) : option Q |
| 503 |
:= (q <- parseQexpr_arith_with_vars vars s; eval_Qexpr_strict q)%option. |
| 504 |
Definition parseZ_arith_strict_with_vars (vars : list (string * Q)) (s : string) : option Z |
| 505 |
:= (q <- parseQ_arith_strict_with_vars vars s; Q_to_Z_strict q)%option. |
| 506 |
Definition parsepositive_arith_strict_with_vars (vars : list (string * Q)) (s : string) : option positive |
| 507 |
:= (z <- parseZ_arith_strict_with_vars vars s; match z with Z0 => None | Zpos p => Some p | Zneg _ => None end). |
| 508 |
Definition parseN_arith_strict_with_vars (vars : list (string * Q)) (s : string) : option N |
| 509 |
:= (z <- parseZ_arith_strict_with_vars vars s; match z with Z0 => Some 0 | Zpos p => Some (Npos p) | Zneg _ => None end)%N%option. |
| 510 |
Definition parsenat_arith_strict_with_vars (vars : list (string * Q)) (s : string) : option nat |
| 511 |
:= (n <- parseN_arith_strict_with_vars vars s; Some (N.to_nat n)). |
| 512 |
|
| 513 |
Definition parseQexpr_poly_arith (s : string) : option (Qexpr_poly var) |
| 514 |
:= finalize (parse_Qexpr_poly parse_var) s. |
| 515 |
Definition parseQ_poly_arith (s : string) : option (poly_in Q var) |
| 516 |
:= option_map (eval_Qexpr_poly_reduced var_beq) (parseQexpr_poly_arith s). |
| 517 |
Definition parseZ_poly_arith (s : string) : option (poly_in Z var) |
| 518 |
:= (q <- parseQ_poly_arith s; Some (poly_coef_map (fun q => (Qnum q / Z.pos (Qden q))%Z) q))%option. |
| 519 |
Definition parsepositive_poly_arith (s : string) : option (poly_in positive var) |
| 520 |
:= (z <- parseZ_poly_arith s; poly_coef_map_strict (fun z => match z with Z0 => None | Zpos p => Some p | Zneg _ => None end) z). |
| 521 |
Definition parseN_poly_arith (s : string) : option (poly_in N var) |
| 522 |
:= (z <- parseZ_poly_arith s; poly_coef_map_strict (fun z => match z with Z0 => Some 0 | Zpos p => Some (Npos p) | Zneg _ => None end)%N z)%option. |
| 523 |
Definition parsenat_poly_arith (s : string) : option (poly_in nat var) |
| 524 |
:= (n <- parseN_poly_arith s; Some (poly_coef_map N.to_nat n)). |
| 525 |
|
| 526 |
Definition parseQ_poly_arith_strict (s : string) : option (poly_in Q var) |
| 527 |
:= (q <- parseQexpr_poly_arith s; eval_Qexpr_poly_strict q)%option. |
| 528 |
Definition parseZ_poly_arith_strict (s : string) : option (poly_in Z var) |
| 529 |
:= (q <- parseQ_poly_arith_strict s; poly_coef_map_strict Q_to_Z_strict q)%option. |
| 530 |
Definition parsepositive_poly_arith_strict (s : string) : option (poly_in positive var) |
| 531 |
:= (z <- parseZ_poly_arith_strict s; poly_coef_map_strict (fun z => match z with Z0 => None | Zpos p => Some p | Zneg _ => None end) z). |
| 532 |
Definition parseN_poly_arith_strict (s : string) : option (poly_in N var) |
| 533 |
:= (z <- parseZ_poly_arith_strict s; poly_coef_map_strict (fun z => match z with Z0 => Some 0 | Zpos p => Some (Npos p) | Zneg _ => None end)%N z)%option. |
| 534 |
Definition parsenat_poly_arith_strict (s : string) : option (poly_in nat var) |
| 535 |
:= (n <- parseN_poly_arith_strict s; Some (poly_coef_map N.to_nat n)). |
| 536 |
|
| 537 |
Definition parse_Qexpr := parse_Qexpr_with_vars []. |
| 538 |
Definition parseQexpr_arith := parseQexpr_arith_with_vars []. |
| 539 |
Definition parseQ_arith := parseQ_arith_with_vars []. |
| 540 |
Definition parseZ_arith := parseZ_arith_with_vars []. |
| 541 |
Definition parsepositive_arith := parsepositive_arith_with_vars []. |
| 542 |
Definition parseN_arith := parseN_arith_with_vars []. |
| 543 |
Definition parsenat_arith := parsenat_arith_with_vars []. |
| 544 |
Definition parseQ_arith_strict := parseQ_arith_strict_with_vars []. |
| 545 |
Definition parseZ_arith_strict := parseZ_arith_strict_with_vars []. |
| 546 |
Definition parsepositive_arith_strict := parsepositive_arith_strict_with_vars []. |
| 547 |
Definition parseN_arith_strict := parseN_arith_strict_with_vars []. |
| 548 |
Definition parsenat_arith_strict := parsenat_arith_strict_with_vars []. |
| 549 |
|
| 550 |
Definition parse_Q_arith := parse_Q_with_vars []. |
| 551 |
Definition parse_Q_arith_strict := parse_Q_strict_with_vars []. |
| 552 |
Definition parse_Z_arith := parse_Z_with_vars []. |
| 553 |
Definition parse_Z_arith_strict := parse_Z_strict_with_vars []. |
| 554 |
Definition parse_N_arith := parse_N_with_vars []. |
| 555 |
Definition parse_N_arith_strict := parse_N_strict_with_vars []. |
| 556 |
Definition parse_nat_arith := parse_nat_with_vars []. |
| 557 |
Definition parse_nat_arith_strict := parse_nat_strict_with_vars []. |
| 558 |
Definition parse_positive_arith := parse_positive_with_vars []. |
| 559 |
Definition parse_positive_arith_strict := parse_positive_strict_with_vars []. |
| 560 |
End with_var. |
| 561 |
|
| 562 |
Redirect "log" Check eq_refl : parseQexpr_arith "1 + -2" = Some (1%Q + - 2%Q)%Qexpr. |
| 563 |
Redirect "log" Check eq_refl : parseQ_poly_arith String.eqb "x" "(5 * x + 3 * x^2 + 10)/2" = Some (5, [(5 / 2, ("x", 1%Z), []); (3 / 2, ("x", 2%Z), [])])%Q. |