Fil-C on Nix

Filnix wiki

the formal campaign

rocq/backend/Text.v

Text as the backend builds it: a list of parts, each a string, which the driver writes one after another. A printer takes the parts that follow it and puts its own in front, as asm.pl's grammar rules do with their difference lists, so no text is ever copied to be joined: SQLite's assembly is about 100 MB.

From Stdlib Require Import String ZArith List Ascii
Import ListNotations
Open Scope string_scope

The parts of a text, in order. A printer of some X has the type X → parts → parts: the parts of its text, then those given.

13Definition parts ≔
liststring

The newline and tab characters, which Rocq's string notation cannot write.

17Definition nl: string ≔
String"010"%char EmptyString
18Definition tab: string ≔
String"009"%char EmptyString

The digits 0 to 9. A definition of its own, like ten below, because the extracted code builds a list or a number again each time it meets one written inside a function.

23Definition digit_texts: liststring ≔
"0"; "1"; "2"; "3"; "4"; "5"; "6"; "7"; "8"; "9"

The digit d, for 0 to 9.

27Definition digitd : nat : string ≔
nthd digit_texts "?"
Open Scope Z_scope
31Definition ten: Z ≔
10

The digits of n, which is not negative, in front of acc. The fuel is a bound on the number of digits.

35Fixpoint digitsfuel : nat n : Z acc : string : string ≔
match fuel with
O
⇒
acc
Sfuel′
⇒
let q, r ≔ Z.div_eucln ten in
let acc′ ≔ digitZ.to_natr ++ acc%string in
q =? 0
→
acc′
else
digitsfuel′ q acc′

Numbers below this have at most 18 decimal digits.

45Definition eighteen_digits: Z ≔
10 ^ 18

A bound on the number of decimal digits of n: 18 for the numbers below 10^18, which are nearly all a compile prints, and the number of binary digits for the others.

50Definition digits_fueln : Z : nat ≔
→
18%nat
else
match n with
Zposp
⇒
Pos.size_natp
_
⇒
1%nat

n in decimal, with a minus sign when it is negative, as Prolog writes an integer.

59Definition decimaln : Z : string ≔
n <? 0
→
"-" ++ digitsdigits_fuel−n −n ""%string
63Example decimal_0: decimal0 = "0"%string
Proof 1 step Qed
reflexivity
65Example decimal_neg: decimal−1080 = "-1080"%string
Proof 1 step Qed
reflexivity
67Example decimal_big: decimal18446744073709551615 = "18446744073709551615"%string
Proof 1 step Qed
reflexivity