(.module: [library [lux (#- nat) [abstract [equivalence (#+ Equivalence)] [order (#+ Order)]] [control ["." try (#+ Try)] ["." exception (#+ exception:)]] [data [text ["%" format (#+ format)]] ["." format #_ ["#" binary (#+ Writer)]]] [macro ["." template]] [math [number ["n" nat] ["." i64]]] [type abstract]]]) (abstract: #export (Unsigned brand) Nat (def: #export value (-> (Unsigned Any) Nat) (|>> :representation)) (implementation: #export equivalence (All [brand] (Equivalence (Unsigned brand))) (def: (= reference sample) (n.= (:representation reference) (:representation sample)))) (implementation: #export order (All [brand] (Order (Unsigned brand))) (def: &equivalence ..equivalence) (def: (< reference sample) (n.< (:representation reference) (:representation sample)))) (exception: #export (value_exceeds_the_maximum {type Name} {value Nat} {maximum (Unsigned Any)}) (exception.report ["Type" (%.name type)] ["Value" (%.nat value)] ["Maximum" (%.nat (:representation maximum))])) (exception: #export [brand] (subtraction_cannot_yield_negative_value {type Name} {parameter (Unsigned brand)} {subject (Unsigned brand)}) (exception.report ["Type" (%.name type)] ["Parameter" (%.nat (:representation parameter))] ["Subject" (%.nat (:representation subject))])) (template [ <+> <-> ] [(with_expansions [ (template.identifier [ "'"])] (abstract: #export Any) (type: #export (Unsigned ))) (def: #export ) (def: #export (|> (n.* i64.bits_per_byte) i64.mask :abstraction)) (def: #export ( value) (-> Nat (Try )) (if (n.<= (:representation ) value) (#try.Success (:abstraction value)) (exception.throw ..value_exceeds_the_maximum [(name_of ) value ]))) (def: #export (<+> parameter subject) (-> (Try )) ( (n.+ (:representation parameter) (:representation subject)))) (def: #export (<-> parameter subject) (-> (Try )) (let [parameter' (:representation parameter) subject' (:representation subject)] (if (n.<= subject' parameter') (#try.Success (:abstraction (n.- parameter' subject'))) (exception.throw ..subtraction_cannot_yield_negative_value [(name_of ) parameter subject])))) (def: #export ( left right) (-> ) (:abstraction (n.max (:representation left) (:representation right))))] [1 U1 bytes/1 u1 maximum/1 +/1 -/1 max/1] [2 U2 bytes/2 u2 maximum/2 +/2 -/2 max/2] [4 U4 bytes/4 u4 maximum/4 +/4 -/4 max/4] ) (template [ ] [(def: #export (-> ) (|>> :transmutation))] [lift/2 U1 U2] [lift/4 U2 U4] ) (template [ ] [(def: #export (Writer ) (|>> :representation ))] [writer/1 U1 format.bits/8] [writer/2 U2 format.bits/16] [writer/4 U4 format.bits/32] ) )