import Tablet.DecimalGoalVocabulary open Aeneas Aeneas.Std Result ControlFlow Error set_option autoImplicit false namespace dec2flt_model -- [TABLET NODE: DecimalGoalVocabularyDefault] noncomputable def DecimalGoalVocabularyDefault : DecimalGoalVocabulary := -- BODY let signBytes := fun sign : Option Bool => match sign with | none => [] | some negative => [if negative then 45 else 43] let digitByte := fun d : Nat => 48 + d let allDigits := fun ds : List Nat => ∀ d ∈ ds, d < 10 let validNumeric := fun x : DecimalLexeme => allDigits x.integerDigits ∧ (match x.fractionDigits with | none => True | some ds => allDigits ds) ∧ (x.integerDigits ≠ [] ∨ match x.fractionDigits with | none => False | some ds => ds ≠ []) ∧ (match x.exponent with | none => True | some (_, _, ds) => ds ≠ [] ∧ allDigits ds) let renderNumeric := fun x : DecimalLexeme => signBytes x.leadingSign ++ x.integerDigits.map digitByte ++ (match x.fractionDigits with | none => [] | some ds => 46 :: ds.map digitByte) ++ (match x.exponent with | none => [] | some (upper, sign, ds) => (if upper then 69 else 101) :: (match sign with | none => [] | some neg => [if neg then 45 else 43]) ++ ds.map digitByte) let asciiCaseEq := fun xs ys : List Nat => List.Forall₂ (fun actual lower => actual = lower ∨ actual + 32 = lower) xs ys let specialLiteral := fun bs : List Nat => fun sign : Option Bool => fun nan : Bool => ∃ body, bs = signBytes sign ++ body ∧ if nan then asciiCaseEq body [110, 97, 110] else asciiCaseEq body [105, 110, 102] ∨ asciiCaseEq body [105, 110, 102, 105, 110, 105, 116, 121] let numericLiteral := fun bs : List Nat => fun x : DecimalLexeme => validNumeric x ∧ renderNumeric x = bs let classifies := fun bs : List Nat => fun c : Option (Sum DecimalLexeme (Option Bool × Bool)) => match c with | some (Sum.inl x) => numericLiteral bs x | some (Sum.inr (sign, nan)) => specialLiteral bs sign nan | none => (¬ ∃ x, numericLiteral bs x) ∧ (¬ ∃ sign nan, specialLiteral bs sign nan) let digitsValue : List Nat -> Nat := fun ds => ds.foldl (fun (n d : Nat) => 10 * n + d) 0 let exponentValue := fun x : DecimalLexeme => match x.exponent with | none => 0 | some (_, sign, ds) => let n : Int := digitsValue ds match sign with | some true => -n | _ => n let accumulatedExponentValue := fun x : DecimalLexeme => match x.exponent with | none => 0 | some (_, sign, ds) => let n : Nat := ds.foldl (fun (n d : Nat) => if n < 65536 then 10 * n + d else n) 0 match sign with | some true => -(n : Int) | _ => (n : Int) let fractionCount := fun x : DecimalLexeme => match x.fractionDigits with | none => 0 | some ds => ds.length let significandDigits := fun x : DecimalLexeme => x.integerDigits ++ match x.fractionDigits with | none => [] | some ds => ds let magnitudeWithExponent := fun x : DecimalLexeme => fun e : Int => (digitsValue (significandDigits x) : Real) * (10 : Real) ^ (e - (fractionCount x : Int)) let signBit : Nat := 2 ^ 63 let fractionUnit : Nat := 2 ^ 52 let infinityBits : Nat := 2047 * fractionUnit let finiteMagnitudeValue := fun bits : Nat => let e := bits / fractionUnit let m := bits % fractionUnit if e = 0 then (m : Real) / ((2 : Real) ^ (1074 : Nat)) else if 1075 <= e then ((fractionUnit + m : Nat) : Real) * ((2 : Real) ^ (e - 1075)) else ((fractionUnit + m : Nat) : Real) / ((2 : Real) ^ (1075 - e)) let overflowThreshold : Real := ((2 : Real) ^ (1024 : Nat)) - ((2 : Real) ^ (970 : Nat)) let roundsMagnitude := fun x : Real => fun w : F64 => 0 <= x ∧ w.val <= infinityBits ∧ if w.val = infinityBits then overflowThreshold <= x else x < overflowThreshold ∧ ∀ v : F64, v.val < infinityBits -> v ≠ w -> abs (x - finiteMagnitudeValue w.val) <= abs (x - finiteMagnitudeValue v.val) ∧ (abs (x - finiteMagnitudeValue w.val) = abs (x - finiteMagnitudeValue v.val) -> w.val % 2 = 0) { bytes := fun s => s.val.map (fun b => b.val) sourceString := fun s => s.val.length <= 9223372036854775807 ∧ ∃ t : String, s.val.map (fun b => b.val) = t.toUTF8.toList.map UInt8.toNat validNumeric := validNumeric renderNumeric := renderNumeric lexicalNegative := fun x => x.leadingSign = some true specialLiteral := specialLiteral classifies := classifies mathExplicitExponent := exponentValue accumulatedExplicitExponent := accumulatedExponentValue exactMagnitude := fun x => magnitudeWithExponent x (exponentValue x) accumulatedMagnitude := fun x => magnitudeWithExponent x (accumulatedExponentValue x) decimalMagnitude := fun d => (d.mantissa.val : Real) * (10 : Real) ^ d.exponent.val roundsMagnitude := roundsMagnitude isNan := fun w => (w.val % signBit) / fractionUnit = 2047 ∧ w.val % fractionUnit ≠ 0 isInfinity := fun negative w => w.val = infinityBits + if negative then signBit else 0 isNegative := fun w => signBit <= w.val signedMagnitude := fun negative magnitude result => magnitude.val < signBit ∧ result.val = magnitude.val + if negative then signBit else 0 assembledWord := fun fp w => w.val = Nat.lor fp.m.val (((fp.p_biased.val.emod (2 ^ 64)).toNat * (2 ^ 52)) % (2 ^ 64)) } end dec2flt_model