| author | paulson | 
| Thu, 04 Sep 2003 11:15:53 +0200 | |
| changeset 14182 | 5f49f00fe084 | 
| parent 13495 | af27202d6d1c | 
| child 14981 | e73f8140af78 | 
| permissions | -rw-r--r-- | 
| 6905 | 1 | (* Title: HOL/Tools/numeral_syntax.ML | 
| 2 | ID: $Id$ | |
| 3 | Author: Markus Wenzel, TU Muenchen | |
| 9230 | 4 | License: GPL (GNU GENERAL PUBLIC LICENSE) | 
| 6905 | 5 | |
| 6 | Concrete syntax for generic numerals. Assumes consts and syntax of | |
| 7 | theory HOL/Numeral. | |
| 8 | *) | |
| 9 | ||
| 10 | signature NUMERAL_SYNTAX = | |
| 11 | sig | |
| 10891 | 12 | val setup: (theory -> theory) list | 
| 6905 | 13 | end; | 
| 14 | ||
| 15 | structure NumeralSyntax: NUMERAL_SYNTAX = | |
| 16 | struct | |
| 17 | ||
| 18 | ||
| 19 | (* bits *) | |
| 20 | ||
| 21 | fun dest_bit (Const ("False", _)) = 0
 | |
| 22 |   | dest_bit (Const ("True", _)) = 1
 | |
| 23 | | dest_bit _ = raise Match; | |
| 24 | ||
| 25 | ||
| 26 | (* bit strings *) (*we try to handle superfluous leading digits nicely*) | |
| 27 | ||
| 28 | fun prefix_len _ [] = 0 | |
| 29 | | prefix_len pred (x :: xs) = | |
| 30 | if pred x then 1 + prefix_len pred xs else 0; | |
| 31 | ||
| 13491 | 32 | fun bin_of (Const ("bin.Pls", _)) = []
 | 
| 6905 | 33 |   | bin_of (Const ("bin.Min", _)) = [~1]
 | 
| 13495 | 34 |   | bin_of (Const ("Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
 | 
| 6905 | 35 | | bin_of _ = raise Match; | 
| 36 | ||
| 7550 | 37 | val dest_bin = HOLogic.int_of o bin_of; | 
| 6905 | 38 | |
| 39 | fun dest_bin_str tm = | |
| 40 | let | |
| 41 | val rev_digs = bin_of tm; | |
| 42 | val (sign, zs) = | |
| 43 | (case rev rev_digs of | |
| 44 |         ~1 :: bs => ("-", prefix_len (equal 1) bs)
 | |
| 45 |       | bs => ("", prefix_len (equal 0) bs));
 | |
| 11701 
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
 wenzelm parents: 
11488diff
changeset | 46 | val i = HOLogic.int_of rev_digs; | 
| 
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
 wenzelm parents: 
11488diff
changeset | 47 | val num = string_of_int (abs i); | 
| 
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
 wenzelm parents: 
11488diff
changeset | 48 | in | 
| 
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
 wenzelm parents: 
11488diff
changeset | 49 | if i = 0 orelse i = 1 then raise Match | 
| 
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
 wenzelm parents: 
11488diff
changeset | 50 | else sign ^ implode (replicate zs "0") ^ num | 
| 
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
 wenzelm parents: 
11488diff
changeset | 51 | end; | 
| 6905 | 52 | |
| 53 | ||
| 54 | (* translation of integer numeral tokens to and from bitstrings *) | |
| 55 | ||
| 11488 
4ff900551340
constify numeral tokens in order to allow translations;
 wenzelm parents: 
10891diff
changeset | 56 | fun numeral_tr (*"_Numeral"*) [t as Const (str, _)] = | 
| 10693 | 57 | (Syntax.const "Numeral.number_of" $ HOLogic.mk_bin (Syntax.read_xnum str)) | 
| 6905 | 58 |   | numeral_tr (*"_Numeral"*) ts = raise TERM ("numeral_tr", ts);
 | 
| 59 | ||
| 60 | fun numeral_tr' show_sorts (*"number_of"*) (Type ("fun", [_, T])) (t :: ts) =
 | |
| 11488 
4ff900551340
constify numeral tokens in order to allow translations;
 wenzelm parents: 
10891diff
changeset | 61 | let val t' = Syntax.const "_Numeral" $ Syntax.free (dest_bin_str t) in | 
| 7429 | 62 | if not (! show_types) andalso can Term.dest_Type T then t' | 
| 6905 | 63 | else Syntax.const Syntax.constrainC $ t' $ Syntax.term_of_typ show_sorts T | 
| 64 | end | |
| 13110 | 65 | | numeral_tr' _ (*"number_of"*) T (t :: ts) = | 
| 66 | if T = dummyT then Syntax.const "_Numeral" $ Syntax.free (dest_bin_str t) | |
| 67 | else raise Match | |
| 6905 | 68 | | numeral_tr' _ (*"number_of"*) _ _ = raise Match; | 
| 69 | ||
| 70 | ||
| 71 | (* theory setup *) | |
| 72 | ||
| 73 | val setup = | |
| 13495 | 74 | [Theory.hide_consts false ["Numeral.bin.Pls", "Numeral.bin.Min"], | 
| 75 |   Theory.add_trfuns ([], [("_Numeral", numeral_tr)], [], []),
 | |
| 6905 | 76 |   Theory.add_trfunsT [("number_of", numeral_tr'), ("Numeral.number_of", numeral_tr')]];
 | 
| 77 | ||
| 78 | ||
| 79 | end; |