src/HOL/Library/Efficient_Nat.thy
changeset 34944 970e1466028d
parent 34902 780172c006e1
child 35625 9c818cab0dd0
equal deleted inserted replaced
34943:e97b22500a5c 34944:970e1466028d
   285 *}
   285 *}
   286 
   286 
   287 code_reserved Haskell Nat
   287 code_reserved Haskell Nat
   288 
   288 
   289 code_include Scala "Nat" {*
   289 code_include Scala "Nat" {*
       
   290 import scala.Math
       
   291 
   290 object Nat {
   292 object Nat {
   291 
   293 
   292   def apply(numeral: BigInt): Nat = new Nat(numeral max 0)
   294   def apply(numeral: BigInt): Nat = new Nat(numeral max 0)
   293   def apply(numeral: Int): Nat = Nat(BigInt(numeral))
   295   def apply(numeral: Int): Nat = Nat(BigInt(numeral))
   294   def apply(numeral: String): Nat = Nat(BigInt(numeral))
   296   def apply(numeral: String): Nat = Nat(BigInt(numeral))
   307   override def toString(): String = this.value.toString
   309   override def toString(): String = this.value.toString
   308 
   310 
   309   def equals(that: Nat): Boolean = this.value == that.value
   311   def equals(that: Nat): Boolean = this.value == that.value
   310 
   312 
   311   def as_BigInt: BigInt = this.value
   313   def as_BigInt: BigInt = this.value
   312   def as_Int: Int = this.value
   314   def as_Int: Int = if (this.value >= Math.MAX_INT && this.value <= Math.MAX_INT)
       
   315       this.value.intValue
       
   316     else error("Int value too big:" + this.value.toString)
   313 
   317 
   314   def +(that: Nat): Nat = new Nat(this.value + that.value)
   318   def +(that: Nat): Nat = new Nat(this.value + that.value)
   315   def -(that: Nat): Nat = Nat(this.value + that.value)
   319   def -(that: Nat): Nat = Nat(this.value + that.value)
   316   def *(that: Nat): Nat = new Nat(this.value * that.value)
   320   def *(that: Nat): Nat = new Nat(this.value * that.value)
   317 
   321 
   346   -- {* this interacts as desired with @{thm nat_number_of_def} *}
   350   -- {* this interacts as desired with @{thm nat_number_of_def} *}
   347   by (simp add: number_nat_inst.number_of_nat)
   351   by (simp add: number_nat_inst.number_of_nat)
   348 
   352 
   349 setup {*
   353 setup {*
   350   fold (Numeral.add_code @{const_name number_nat_inst.number_of_nat}
   354   fold (Numeral.add_code @{const_name number_nat_inst.number_of_nat}
   351     false true Code_Printer.str) ["SML", "OCaml", "Haskell"]
   355     false Code_Printer.literal_positive_numeral) ["SML", "OCaml", "Haskell"]
   352   #> Numeral.add_code @{const_name number_nat_inst.number_of_nat}
   356   #> Numeral.add_code @{const_name number_nat_inst.number_of_nat}
   353     false true (fn s => (Pretty.block o map Code_Printer.str) ["Nat.Nat", s]) "Scala"
   357     false Code_Printer.literal_positive_numeral "Scala"
   354 *}
   358 *}
   355 
   359 
   356 text {*
   360 text {*
   357   Since natural numbers are implemented
   361   Since natural numbers are implemented
   358   using integers in ML, the coercion function @{const "of_nat"} of type
   362   using integers in ML, the coercion function @{const "of_nat"} of type