520 |
520 |
521 lemma one_less_minus_of_num_iff: "\<not> 1 < - of_num n" |
521 lemma one_less_minus_of_num_iff: "\<not> 1 < - of_num n" |
522 by (simp add: not_less minus_of_num_le_one_iff) |
522 by (simp add: not_less minus_of_num_le_one_iff) |
523 |
523 |
524 lemma of_num_less_minus_one_iff: "\<not> of_num n < - 1" |
524 lemma of_num_less_minus_one_iff: "\<not> of_num n < - 1" |
525 by (simp add: not_less minus_one_le_of_num_iff) |
525 by (simp only: not_less minus_one_le_of_num_iff) |
526 |
526 |
527 lemma one_less_minus_one_iff: "\<not> 1 < - 1" |
527 lemma one_less_minus_one_iff: "\<not> 1 < - 1" |
528 by (simp add: not_less minus_one_le_one_iff) |
528 by (simp only: not_less minus_one_le_one_iff) |
529 |
529 |
530 lemmas le_signed_numeral_special [numeral] = |
530 lemmas le_signed_numeral_special [numeral] = |
531 minus_of_num_le_of_num_iff |
531 minus_of_num_le_of_num_iff |
532 minus_of_num_le_one_iff |
532 minus_of_num_le_one_iff |
533 minus_one_le_of_num_iff |
533 minus_one_le_of_num_iff |
968 "Mns k < Mns l \<longleftrightarrow> l < k" |
965 "Mns k < Mns l \<longleftrightarrow> l < k" |
969 by simp_all |
966 by simp_all |
970 |
967 |
971 hide_const (open) sub dup |
968 hide_const (open) sub dup |
972 |
969 |
973 text {* Pretty literals *} |
970 end |
974 |
971 |
975 ML {* |
|
976 local open Code_Thingol in |
|
977 |
|
978 fun add_code print target = |
|
979 let |
|
980 fun dest_num one' dig0' dig1' thm = |
|
981 let |
|
982 fun dest_dig (IConst (c, _)) = if c = dig0' then 0 |
|
983 else if c = dig1' then 1 |
|
984 else Code_Printer.eqn_error thm "Illegal numeral expression: illegal dig" |
|
985 | dest_dig _ = Code_Printer.eqn_error thm "Illegal numeral expression: illegal digit"; |
|
986 fun dest_num (IConst (c, _)) = if c = one' then 1 |
|
987 else Code_Printer.eqn_error thm "Illegal numeral expression: illegal leading digit" |
|
988 | dest_num (t1 `$ t2) = 2 * dest_num t2 + dest_dig t1 |
|
989 | dest_num _ = Code_Printer.eqn_error thm "Illegal numeral expression: illegal term"; |
|
990 in dest_num end; |
|
991 fun pretty sgn literals [one', dig0', dig1'] _ thm _ _ [(t, _)] = |
|
992 (Code_Printer.str o print literals o sgn o dest_num one' dig0' dig1' thm) t |
|
993 fun add_syntax (c, sgn) = Code_Target.add_const_syntax target c |
|
994 (SOME (Code_Printer.complex_const_syntax |
|
995 (1, ([@{const_name One}, @{const_name Dig0}, @{const_name Dig1}], |
|
996 pretty sgn)))); |
|
997 in |
|
998 add_syntax (@{const_name Pls}, I) |
|
999 #> add_syntax (@{const_name Mns}, (fn k => ~ k)) |
|
1000 end; |
|
1001 |
|
1002 end |
|
1003 *} |
|
1004 |
|
1005 hide_const (open) One Dig0 Dig1 |
|
1006 |
|
1007 |
|
1008 subsection {* Toy examples *} |
|
1009 |
|
1010 definition "foo \<longleftrightarrow> #4 * #2 + #7 = (#8 :: nat)" |
|
1011 definition "bar \<longleftrightarrow> #4 * #2 + #7 \<ge> (#8 :: int) - #3" |
|
1012 |
|
1013 code_thms foo bar |
|
1014 export_code foo bar checking SML OCaml? Haskell? Scala? |
|
1015 |
|
1016 text {* This is an ad-hoc @{text Code_Integer} setup. *} |
|
1017 |
|
1018 setup {* |
|
1019 fold (add_code Code_Printer.literal_numeral) |
|
1020 [Code_ML.target_SML, Code_ML.target_OCaml, Code_Haskell.target, Code_Scala.target] |
|
1021 *} |
|
1022 |
|
1023 code_type int |
|
1024 (SML "IntInf.int") |
|
1025 (OCaml "Big'_int.big'_int") |
|
1026 (Haskell "Integer") |
|
1027 (Scala "BigInt") |
|
1028 (Eval "int") |
|
1029 |
|
1030 code_const "0::int" |
|
1031 (SML "0/ :/ IntInf.int") |
|
1032 (OCaml "Big'_int.zero") |
|
1033 (Haskell "0") |
|
1034 (Scala "BigInt(0)") |
|
1035 (Eval "0/ :/ int") |
|
1036 |
|
1037 code_const Int.pred |
|
1038 (SML "IntInf.- ((_), 1)") |
|
1039 (OCaml "Big'_int.pred'_big'_int") |
|
1040 (Haskell "!(_/ -/ 1)") |
|
1041 (Scala "!(_ -/ 1)") |
|
1042 (Eval "!(_/ -/ 1)") |
|
1043 |
|
1044 code_const Int.succ |
|
1045 (SML "IntInf.+ ((_), 1)") |
|
1046 (OCaml "Big'_int.succ'_big'_int") |
|
1047 (Haskell "!(_/ +/ 1)") |
|
1048 (Scala "!(_ +/ 1)") |
|
1049 (Eval "!(_/ +/ 1)") |
|
1050 |
|
1051 code_const "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int" |
|
1052 (SML "IntInf.+ ((_), (_))") |
|
1053 (OCaml "Big'_int.add'_big'_int") |
|
1054 (Haskell infixl 6 "+") |
|
1055 (Scala infixl 7 "+") |
|
1056 (Eval infixl 8 "+") |
|
1057 |
|
1058 code_const "uminus \<Colon> int \<Rightarrow> int" |
|
1059 (SML "IntInf.~") |
|
1060 (OCaml "Big'_int.minus'_big'_int") |
|
1061 (Haskell "negate") |
|
1062 (Scala "!(- _)") |
|
1063 (Eval "~/ _") |
|
1064 |
|
1065 code_const "op - \<Colon> int \<Rightarrow> int \<Rightarrow> int" |
|
1066 (SML "IntInf.- ((_), (_))") |
|
1067 (OCaml "Big'_int.sub'_big'_int") |
|
1068 (Haskell infixl 6 "-") |
|
1069 (Scala infixl 7 "-") |
|
1070 (Eval infixl 8 "-") |
|
1071 |
|
1072 code_const "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int" |
|
1073 (SML "IntInf.* ((_), (_))") |
|
1074 (OCaml "Big'_int.mult'_big'_int") |
|
1075 (Haskell infixl 7 "*") |
|
1076 (Scala infixl 8 "*") |
|
1077 (Eval infixl 9 "*") |
|
1078 |
|
1079 code_const pdivmod |
|
1080 (SML "IntInf.divMod/ (IntInf.abs _,/ IntInf.abs _)") |
|
1081 (OCaml "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)") |
|
1082 (Haskell "divMod/ (abs _)/ (abs _)") |
|
1083 (Scala "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))") |
|
1084 (Eval "Integer.div'_mod/ (abs _)/ (abs _)") |
|
1085 |
|
1086 code_const "HOL.equal \<Colon> int \<Rightarrow> int \<Rightarrow> bool" |
|
1087 (SML "!((_ : IntInf.int) = _)") |
|
1088 (OCaml "Big'_int.eq'_big'_int") |
|
1089 (Haskell infix 4 "==") |
|
1090 (Scala infixl 5 "==") |
|
1091 (Eval infixl 6 "=") |
|
1092 |
|
1093 code_const "op \<le> \<Colon> int \<Rightarrow> int \<Rightarrow> bool" |
|
1094 (SML "IntInf.<= ((_), (_))") |
|
1095 (OCaml "Big'_int.le'_big'_int") |
|
1096 (Haskell infix 4 "<=") |
|
1097 (Scala infixl 4 "<=") |
|
1098 (Eval infixl 6 "<=") |
|
1099 |
|
1100 code_const "op < \<Colon> int \<Rightarrow> int \<Rightarrow> bool" |
|
1101 (SML "IntInf.< ((_), (_))") |
|
1102 (OCaml "Big'_int.lt'_big'_int") |
|
1103 (Haskell infix 4 "<") |
|
1104 (Scala infixl 4 "<") |
|
1105 (Eval infixl 6 "<") |
|
1106 |
|
1107 code_const Code_Numeral.int_of |
|
1108 (SML "IntInf.fromInt") |
|
1109 (OCaml "_") |
|
1110 (Haskell "toInteger") |
|
1111 (Scala "!_.as'_BigInt") |
|
1112 (Eval "_") |
|
1113 |
|
1114 export_code foo bar checking SML OCaml? Haskell? Scala? |
|
1115 |
|
1116 end |
|