2 (* -------------------------------------------------------------------------- *)
4 (* -------------------------------------------------------------------------- *)
6 module type Ocaml_sml =
9 type const_name = | Dodec_slope
49 type func_name = | Anc
103 | Vor_analytic_x_flipped
106 type const = | Decimal of int * int
108 | Named of const_name
114 | Cplus of const * const
115 | Cmul of const * const
116 | Cdiv of const * const
118 type expr = | Const of const
119 | Funcall of func_name * expr list
128 type monom = const * expr
130 type lcomb = monom list
132 type bounds = {lo : const;
135 type ineq = {name : string;
136 vars : (string * bounds) list;
140 put the hales inequalities in a normal form
142 !x_1 ... x_n. ineq [lower_1,x_1,upper_1;
145 c_1_1 f_1 + ... + c_1_m f_m < &0 \/
147 c_k_1 f_k + ... + c_k_m f_m < &0
149 val normalize : Term.term -> Term.term
152 (* Translate HOL Light term to OCaml datatype *)
153 val translate : string * Term.term -> ineq
155 val translate_list : ignore : string list ->
156 terms : (string * Term.term) list -> ineq list
158 (* Print Ocaml datatype as an SML datatype *)
159 val ineq_to_sml : ineq -> unit
161 val ineqs_to_sml : file:string -> ineqs:ineq list -> unit
165 (* -------------------------------------------------------------------------- *)
167 (* -------------------------------------------------------------------------- *)
169 module Ocaml_sml (* : Ocaml_sml *) =
172 type const_name = | Dodec_slope
212 type func_name = | Anc
266 | Vor_analytic_x_flipped
269 type const = | Decimal of int * int
271 | Named of const_name
277 | Cplus of const * const
278 | Cmul of const * const
279 | Cdiv of const * const
281 type expr = | Const of const
282 | Funcall of func_name * expr list
291 type monom = const * expr
293 type lcomb = monom list
295 type bounds = {lo : const;
298 type ineq = {name : string;
299 vars : (string * bounds) list;
302 (* -------------------------------------------------------------------------- *)
304 (* -------------------------------------------------------------------------- *)
306 (* remove "kepler'" from the front of a name *)
309 let kep = String.sub s 0 7 in
310 if kep = "kepler'" then String.sub s 7 (String.length s - 7)
312 with Invalid_argument _ -> s
314 let const_of_string const = match const with
321 | "dodecSlope" -> Dodec_slope
322 | "dodecTrunc" -> Dodec_trunc
338 | "pp_solt0" -> Ppsolt0
343 | "sqrt_2t0" -> Sqrt_2t0
344 | "square_2t0" -> Square_2t0
345 | "square_4t0" -> Square_4t0
349 | "twoDodecTrunc" -> Two_dodec_trunc
350 | "twoDodecTruncSq" -> Two_dodec_trunc_sq
351 | "xi_gamma" -> Xi_gamma
352 | "xi'_gamma" -> Xi'_gamma
354 | _ -> failwith ("not a constant: " ^ const)
356 let const_of_string = const_of_string o unkepler
358 let const_to_string const = match const with
365 | Dodec_slope -> "DodecSlope"
366 | Dodec_trunc -> "DodecTrunc"
382 | Ppsolt0 -> "Ppsolt0"
387 | Sqrt_2t0 -> "Sqrt2t0"
388 | Square_2t0 -> "Square2t0"
389 | Square_4t0 -> "Square4t0"
392 | Two_dodec_trunc -> "TwoDodecTrunc"
393 | Two_dodec_trunc_sq -> "TwoDodecTruncSq"
395 | Xi_gamma -> "XiGamma"
396 | Xi'_gamma -> "Xi'Gamma"
399 let func_of_string func = match func with
401 | "arclength" -> Arclength
404 | "cross_diag_x" -> Cross_diag_x
406 | "delta_x" -> Delta_x
412 | "gamma_x" -> Gamma_x
415 | "mu_flat_x" -> Mu_flat_x
416 | "mu_flipped_x" -> Mu_flipped_x
417 | "mu_upright_x" -> Mu_upright_x
418 | "nu_gamma_x" -> Nu_gamma_x
421 | "octavor0_x" -> Octavor0_x
422 | "octavor_analytic_x" -> Octavor_analytic_x
423 | "omega_x" -> Omega_x
424 | "overlap_f" -> Overlap_f
427 | "sigma1_qrtet_x" -> Sigma1_qrtet_x
428 | "sigma32_qrtet_x" -> Sigma32_qrtet_x
429 | "sigma_qrtet_x" -> Sigma_qrtet_x
430 | "sigmahat_x" -> Sigmahat_x
432 | "squander_x" -> Squander_x
434 | "tauC0_x" -> Tauc0_x
435 | "tauVt_x" -> Tauvt_x
436 | "tau_0_x" -> Tau_0_x
437 | "tau_analytic_x" -> Tau_analytic_x
438 | "tau_sigma_x" -> Tau_sigma_x
439 | "tauhat_x" -> Tauhat_x
440 | "tauhatpi_x" -> Tauhatpi_x
441 | "taumu_flat_x" -> Taumu_flat_x
442 | "taunu_x" -> Taunu_x
443 | "truncated_volume_x" -> Truncated_volume_x
447 | "volume_x" -> Volume_x
449 | "vorC0_x" -> Vorc0_x
451 | "vor_0_x" -> Vor_0_x
452 | "vor_0_x_flipped" -> Vor_0_x_flipped
453 | "vor_analytic_x" -> Vor_analytic_x
454 | "vor_analytic_x_flipped" -> Vor_analytic_x_flipped
456 | _ -> failwith ("no such const: " ^ func)
458 let func_of_string = func_of_string o unkepler
460 let func_to_string func = match func with
462 | Arclength -> "Arclength"
465 | Cross_diag_x -> "CrossDiagX"
467 | Delta_x -> "DeltaX"
473 | Gamma_x -> "GammaX"
476 | Mu_flat_x -> "MuFlatX"
477 | Mu_flipped_x -> "MuFlippedX"
478 | Mu_upright_x -> "MuUprightX"
479 | Nu_gamma_x -> "NuGammaX"
482 | Octavor0_x -> "Octavor0X"
483 | Octavor_analytic_x -> "OctavorAnalyticX"
484 | Omega_x -> "OmegaX"
485 | Overlap_f -> "OverlapF"
488 | Sigma1_qrtet_x -> "Sigma1QrtetX"
489 | Sigma32_qrtet_x -> "Sigma32QrtetX"
490 | Sigma_qrtet_x -> "SigmaQrtetX"
491 | Sigmahat_x -> "SigmahatX"
493 | Squander_x -> "SquanderX"
495 | Tauc0_x -> "Tauc0X"
496 | Tauvt_x -> "TauvtX"
498 | Tau_analytic_x -> "TauAnalyticX"
499 | Tau_sigma_x -> "TauSigmaX"
500 | Tauhat_x -> "TauhatX"
501 | Tauhatpi_x -> "TauhatpiX"
502 | Taumu_flat_x -> "TaumuFlatX"
503 | Taunu_x -> "TaunuX"
504 | Truncated_volume_x -> "TruncatedVolumeX"
508 | Volume_x -> "VolumeX"
510 | Vorc0_x -> "Vorc0X"
513 | Vor_0_x_flipped -> "Vor0XFlipped"
514 | Vor_analytic_x -> "VorAnalyticX"
515 | Vor_analytic_x_flipped -> "VorAnalyticXFlipped"
518 let var_to_string v = fst (dest_var v)
519 let dest_add t = try Some (dest_binop `(+.)` t) with _ -> None
520 let dest_sub t = try Some (dest_binop `(-.)` t) with _ -> None
521 let dest_mul t = try Some (dest_binop `( *. )` t) with _ -> None
522 let dest_div t = try Some (dest_binop `( / )` t) with _ -> None
524 (* -------------------------------------------------------------------------- *)
526 (* -------------------------------------------------------------------------- *)
529 let translate_decimal t : const option =
531 let dec,numden = strip_comb t in
532 let num,den = match numden with [num;den] -> num,den | _ -> failwith "" in
533 if dec = `DECIMAL` then
534 match dest_numeral num,dest_numeral den with
535 Num.Int num', Num.Int den' -> Some (Decimal (num',den'))
541 let translate_rint t : const option =
543 let amp,n = dest_comb t in
545 match dest_numeral n with
546 Num.Int n' -> Some (Int n')
552 let translate_int t : int option =
554 match dest_numeral t with
555 Num.Int n' -> Some n'
560 let translate_named t : const option =
562 let c,_ = dest_const t in
563 Some (Named (const_of_string c))
566 let rec translate_const t : const option =
567 match translate_decimal t with
570 match translate_rint t with
573 match translate_named t with
576 match translate_cos t with
579 match translate_acos t with
582 match translate_sqr t with
585 match translate_sqrt t with
588 match translate_copp t with
591 match translate_cplus t with
594 match translate_cmul t with
597 match translate_cdiv t with
601 and translate_unop p con t : const option =
603 let p',c = dest_comb t in
605 match translate_const c with
606 Some c -> Some (con c)
612 and translate_binop p con t : const option =
614 let l,r = dest_binop p t in
615 match translate_const l,translate_const r with
616 Some l', Some r' -> Some (con(l',r'))
620 and translate_cos t = translate_unop `cos` (fun x -> Cos x) t
621 and translate_acos t = translate_unop `acs` (fun x -> Acos x) t
622 and translate_sqr t = translate_unop `square` (fun x -> Sqr x) t
623 and translate_sqrt t = translate_unop `sqrt` (fun x -> Sqrt x) t
624 and translate_copp t = translate_unop `(--.)` (fun x -> Copp x) t
625 and translate_cplus t = translate_binop `(+.)` (fun x,y -> Cplus (x,y)) t
626 and translate_cmul t = translate_binop `( *. )` (fun x,y -> Cmul (x,y)) t
627 and translate_cdiv t = translate_binop `( / )` (fun x,y -> Cdiv (x,y)) t
629 let translate_var t =
630 if is_var t then Some (Var (fst (dest_var t))) else None
632 let translate_varsqrt t =
634 let sqrt,x = dest_comb t in
635 if sqrt = `sqrt` && is_var x then
636 Some (Varsqrt (fst (dest_var x)))
640 let rec translate_expr t : expr option =
641 match translate_const t with
642 Some c -> Some (Const c)
644 match translate_funcall t with
647 match translate_varsqrt t with
650 match translate_var t with
653 match translate_opp t with
656 match translate_mul t with
659 match translate_div t with
662 match translate_pow t with
664 | None -> failwith "translate_expr"
666 and translate_funcall t =
668 let f,xs = strip_comb t in
669 let func_str,_ = dest_const f in
670 let func = func_of_string func_str in
671 let xs' = map (fun x -> match translate_expr x with Some e -> e | None -> failwith "") xs in
672 Some (Funcall(func,xs'))
675 and translate_unop p con t : expr option =
677 let p',c = dest_comb t in
679 match translate_expr c with
680 Some c -> Some (con c)
686 and translate_binop p con t : expr option =
688 let l,r = dest_binop p t in
689 match translate_expr l,translate_expr r with
690 Some l', Some r' -> Some (con(l',r'))
694 and translate_opp t = translate_unop `(--.)` (fun x -> Opp x) t
695 and translate_mul t = translate_binop `( *. )` (fun x,y -> Mul (x,y)) t
696 and translate_div t = translate_binop `( / )` (fun x,y -> Div (x,y)) t
697 and translate_pow t =
699 let f,[x;n] = strip_comb t in
700 if not(f = `((pow):real->num->real)`) then None else
701 let Some x' = translate_expr x in
702 let Some n' = translate_int n in
706 let translate_monom t : monom option =
707 match translate_const t with
708 Some x -> Some (x,One)
710 match dest_mul t with
712 (match translate_const c, translate_expr e with
713 Some c', Some e' -> Some (c',e')
714 | _ -> match translate_expr t with
715 Some e -> Some (Int 1,e)
718 match translate_expr t with
719 Some e -> Some (Int 1,e)
722 let rec translate_lcomb t =
723 match dest_add t with
725 (match translate_lcomb l ,translate_lcomb r with
726 Some l', Some r' -> Some (l' @ r')
729 match translate_monom t with
733 let translate_rel t =
735 let lcomb,zero = dest_binop `(<.)` t in
736 let _ = if zero <> `(&0)` then failwith "not zero" else () in
737 translate_lcomb lcomb
738 with _ -> failwith "translate_rel"
740 let translate_ineq t =
742 let _,body = strip_forall t in
743 let ineq_tm,bounds_rel = strip_comb body in
744 let bounds,rel = match bounds_rel with [bounds;rel] -> bounds,rel | _ -> failwith "" in
745 let ineqs = disjuncts rel in
746 let map_fn q = match translate_rel q with Some l -> l | None -> failwith "" in
747 let ineqs = map map_fn ineqs in
749 let x,yz = dest_pair xyz in
750 let x = match translate_const x with Some x -> x | None -> failwith "" in
751 let y,z = dest_pair yz in
752 let y,_ = dest_var y in
753 let z = match translate_const z with Some x -> x | None -> failwith "" in
754 y,{lo = x; hi = z} in
755 let bounds' = map dest_trip (dest_list bounds) in
757 with _ -> failwith "translate_ineq"
759 (* -------------------------------------------------------------------------- *)
761 (* -------------------------------------------------------------------------- *)
765 REAL_ARITH `x *. (y + z) = x * y + x * z`;
766 REAL_ARITH `(x +. y) * z = z * x + z * y`;
767 REAL_ARITH `&0 *. x = &0`;
768 REAL_ARITH `x * -- y = -- x * y`;
769 REAL_ARITH `(x -. y) = x + (-- y)`;
770 REAL_ARITH `(x +. y) + z = x + y + z`;
771 REAL_ARITH `--. (x * y) = (--. x) * y`;
772 REAL_ARITH `-- #0.0 = &0`;
773 REAL_ARITH `-- &0 = &0`;
774 REAL_ARITH `x + &0 = x`;
775 REAL_ARITH `&0 + x = x`;
776 REAL_ARITH `--. (x + y) = (--. x) + (-- y)`;
777 REAL_ARITH `--. (-- x) = x`;
778 REAL_ARITH `!f:real->real->real->real->real->real->real. (-- (f x1 x2 x3 x4 x5 x6)) = (-- (&1) *. (f x1 x2 x3 x4 x5 x6))`;
779 MESON [] `(a \/ (b \/ c)) = (a \/ b \/ c)`;
786 REAL_ARITH `!x y z:real. (x < y) = x - y < &0`;
787 REAL_ARITH `!x y z:real. (x > y) = y - x < &0`;
790 snd (dest_eq (concl (
791 (DEPTH_CONV BETA_CONV THENC
792 ONCE_REWRITE_CONV once_thms THENC
793 REWRITE_CONV thms THENC
794 NUM_REDUCE_CONV) x)))
796 let translate (name,q) =
797 let _ = print_endline ("translating ineq: " ^ name) in
798 let bounds,rels = translate_ineq (normalize q) in
803 let translate_list ~ignore ~terms =
804 map translate (List.filter (fun x,_ -> not (mem x ignore)) terms)
806 (* -------------------------------------------------------------------------- *)
807 (* Pretty Printing *)
808 (* -------------------------------------------------------------------------- *)
815 print_string (string_of_int n);
819 let pp_pair f (l,r) =
822 print_string "(";f l;print_string ",";f r;print_string ")";
827 let rec separate x l str = match l with
829 | [h] -> List.rev (h::str)
830 | h::h'::t -> separate x (h'::t) (x::h::str) in
831 fun x l -> separate x l []
833 let rec iter_butlast f l = match l with
835 | h::t -> (f h;iter_butlast f t)
837 let rec last l = match l with
842 let pp_list_horiz f l = if l = [] then print_string "[]" else
846 iter_butlast (fun x -> (f x; print_string ", ")) l;
852 let pp_list_vert f l = if l = [] then print_string "[]" else
856 iter_butlast (fun x -> (f x; print_string ",";print_cut())) l;
872 let pp_binop p1 p2 s c1 c2 =
884 let pp_decimal (x,y) =
887 print_string "Decimal";
888 pp_pair pp_int (x,y);
892 let pp_named n = print_string (const_to_string n)
894 let rec pp_const c = match c with
895 | Decimal (x,y) -> pp_decimal(x,y)
896 | Int n -> (print_string "Int "; pp_int n)
897 | Named n -> (print_string "Named ";pp_named n)
899 | Acos c -> pp_acos c
901 | Sqrt c -> pp_sqrt c
902 | Copp c -> pp_copp c
903 | Cplus (x,y) -> pp_cplus x y
904 | Cmul (x,y) -> pp_cmul x y
905 | Cdiv (x,y) -> pp_cdiv x y
907 and pp_cos c = pp_unop pp_const "Cos" c
908 and pp_acos c = pp_unop pp_const "Acos" c
909 and pp_sqr c = pp_unop pp_const "Sqr" c
910 and pp_sqrt c = pp_unop pp_const "Sqrt" c
911 and pp_copp c = pp_unop pp_const "Copp" c
912 and pp_cplus c1 c2 = pp_binop pp_const pp_const "Cplus" c1 c2
913 and pp_cmul c1 c2 = pp_binop pp_const pp_const "Cmul" c1 c2
914 and pp_cdiv c1 c2 = pp_binop pp_const pp_const "Cdiv" c1 c2
916 let rec pp_expr e = match e with
917 | Const c -> (print_string "Const (";pp_const c;print_string ")")
918 | Funcall (f,xs) -> pp_funcall f xs
920 | Varsqrt v -> pp_varsqrt v
922 | Mul(x,y) -> pp_mul x y
923 | Div(x,y) -> pp_div x y
924 | Pow(x,n) -> pp_pow x n
925 | One -> print_string "One"
927 and pp_funcall f xs =
930 print_string "Funcall(";
931 print_string (func_to_string f);
933 pp_list_horiz pp_expr xs;
938 and pp_var v = print_string ("Var \"" ^ v ^ "\"")
939 and pp_varsqrt v = print_string ("Varsqrt \"" ^ v ^ "\"")
940 and pp_opp e = pp_unop pp_expr "Opp" e
941 and pp_mul e1 e2 = pp_binop pp_expr pp_expr "Mul" e1 e2
942 and pp_div e1 e2 = pp_binop pp_expr pp_expr "Div" e1 e2
943 and pp_pow e1 e2 = pp_binop pp_expr pp_int "Pow" e1 e2
956 let pp_lcomb l = pp_list_horiz pp_monom l
958 let pp_bounds (v, {lo=lo;hi=hi}) =
961 print_string ("(\"" ^ v ^ "\",");
964 print_string "{lo = ";
970 print_string " hi = ";
981 print_string "name = \"";
986 print_string "vars = ";
987 pp_list_vert pp_bounds q.vars;
991 print_string "rels = ";
992 pp_list_vert pp_lcomb q.rels;
997 let ineqs_to_sml qs =
1008 iter_butlast doit qs;
1009 ineq_to_sml (last qs);
1015 (*============================================================================*)
1016 (* THIS FILE IS AUTOGENERATED. DO NOT EDIT!!! *)
1017 (*============================================================================*)
1019 structure " ^ univ ^ "InequalitySyntaxBase:> INEQUALITY_SYNTAX_BASE =
1025 val unexpandedIneqs = [
1034 let ineqs_to_sml ~file ~ineqs ~univ =
1035 let chan = open_out file in
1037 set_formatter_out_channel chan;
1038 print_string (header univ);
1040 print_string footer;
1041 set_formatter_out_channel stdout;
1044 set_formatter_out_channel stdout;
1052 needs "Examples/analysis.ml";;
1053 needs "Examples/transc.ml";;
1054 needs "Jordan/lib_ext.ml";;
1055 needs "Jordan/parse_ext_override_interface.ml";;
1056 unambiguous_interface();;
1058 #use "../../kepler/sml/inequalities/holl/definitions_kepler.ml";;
1059 #use "../../kepler/sml/inequalities/holl/kep_inequalities.ml";;
1060 #use "../../kepler/sml/inequalities/holl/ineq_names.ml";;
1061 #use "../../kepler/sml/inequalities/holl/ocaml_to_sml.ml";;
1062 let ocaml_ineqs = Ocaml_sml.translate_list ~ignore:Ineq_names.ignore ~terms:Ineq_names.ineqs;;
1064 Ocaml_sml.ineqs_to_sml
1065 ~file:"/Users/seanmcl/save/versioned/projects/kepler/sml/inequalities/inequality-syntax.sml"
1066 ~ineqs:ocaml_ineqs;;
1068 let file = "/Users/seanmcl/save/versioned/projects/kepler/sml/inequalities/inequality-syntax.sml"
1070 let q = List.nth ocaml_ineqs 0;;
1071 (print_endline ""; Ocaml_sml.ineq_to_sml q; print_endline "";)
1073 let t = Ocaml_sml.normalize x;;
1074 Ocaml_sml.translate_ineq t
1078 #trace Ocaml_sml.normalize;;
1079 #trace Ocaml_sml.translate_const;;
1080 #trace Ocaml_sml.translate_cdiv;;
1081 #trace Ocaml_sml.translate_expr;;
1082 #trace Ocaml_sml.translate_funcall;;
1083 #trace Ocaml_sml.translate_div;;
1084 #trace Ocaml_sml.translate_monom;;
1085 #trace Ocaml_sml.translate_lcomb;;
1086 #trace Ocaml_sml.translate_rel;;
1087 #trace Ocaml_sml.translate_ineq;;
1088 #trace Ocaml_sml.translate_pow;;
1089 #trace Ocaml_sml.translate;;
1093 let x = I_327474205_1
1094 Ocaml_sml.translate_ineq (Ocaml_sml.normalize x)
1095 Ocaml_sml.normalize I_327474205_1
1097 translate_ineq I_867513567_13
1098 let t = `tau_sigma_x x1 x2 x3 x4 x5 x6 -. #0.2529 *. dih_x x1 x2 x3 x4 x5 x6 >.