src/HOL/Tools/float_syntax.ML
author bulwahn
Wed, 30 Mar 2011 11:32:52 +0200
changeset 42163 392fd6c4669c
parent 37744 3daaf23b9ab4
child 42290 b1f544c84040
permissions -rw-r--r--
renewing specifications in HOL: replacing types by type_synonym

(*  Title:      HOL/Tools/float_syntax.ML
    Author:     Tobias Nipkow, TU Muenchen

Concrete syntax for floats.
*)

signature FLOAT_SYNTAX =
sig
  val setup: theory -> theory
end;

structure Float_Syntax: FLOAT_SYNTAX =
struct

(* parse translation *)

local

fun mk_number i =
  let
    fun mk 0 = Syntax.const @{const_syntax Int.Pls}
      | mk ~1 = Syntax.const @{const_syntax Int.Min}
      | mk i =
          let val (q, r) = Integer.div_mod i 2
          in HOLogic.mk_bit r $ (mk q) end;
  in Syntax.const @{const_syntax Int.number_of} $ mk i end;

fun mk_frac str =
  let
    val {mant = i, exp = n} = Syntax.read_float str;
    val exp = Syntax.const @{const_syntax Power.power};
    val ten = mk_number 10;
    val exp10 = if n = 1 then ten else exp $ ten $ mk_number n;
  in Syntax.const @{const_syntax divide} $ mk_number i $ exp10 end;

in

fun float_tr (*"_Float"*) [t as Const (str, _)] = mk_frac str
  | float_tr (*"_Float"*) ts = raise TERM ("float_tr", ts);

end;


(* theory setup *)

val setup =
  Sign.add_trfuns ([], [(@{syntax_const "_Float"}, float_tr)], [], []);

end;