tricu

An interpreted language for exploring Tree Calculus
Log | Files | Refs | README | LICENSE

shipping.tri (1930B)


      1 !import bd2c14d6db6b69308a5252a8e839705a55d30e815f012900b094315263e08d81 *
      2 !export v1
      3 !export v2
      4 
      5 -- Shipping policy: arguments are destination, cart subtotal in cents, grams.
      6 -- Destinations: domestic, regional, international. Grams: 1..5000.
      7 -- Select v1/v2 with the same cart: domestic free shipping changes from $75 to $100.
      8 -- Otherwise domestic costs $4.99/$5.99, regional $9.99, international $19.99.
      9 -- Packages above 1kg add $2.50. Subtotals are bounded to $10,000.
     10 -- The bundle hash + export pins the policy used to reproduce a quote later.
     11 -- Records use [fieldName tag value]: 0 = text, 1 = natural, 2 = Boolean.
     12 -- Base supplies canonical-natural checks, arithmetic and Booleans.
     13 
     14 failure message = [["status" 0 "error"] ["message" 0 message]]
     15 
     16 baseRate 1 "domestic" = 499
     17 baseRate _ "domestic" = 599
     18 baseRate _ "regional" = 999
     19 baseRate _ _ = 1999
     20 
     21 paidRate version zone grams = case lte_? grams 1000 of
     22   ^true = baseRate version zone
     23   _ = add 250 (baseRate version zone)
     24 
     25 freeThreshold 1 = 7500
     26 freeThreshold _ = 10000
     27 
     28 amount version zone subtotal grams = case and? (equal? zone "domestic") (lte_? (freeThreshold version) subtotal) of
     29   ^true = 0
     30   _ = paidRate version zone grams
     31 
     32 quote version zone subtotal grams = case and? (isNat? subtotal) (isNat? grams) of
     33   ^true = (case or? (equal? zone "domestic") (or? (equal? zone "regional") (equal? zone "international")) of
     34     ^true = (case and? (lte_? subtotal 1000000) (and? (lte_? 1 grams) (lte_? grams 5000)) of
     35       ^true = [["status" 0 "ok"] ["policyVersion" 1 version]
     36         ["destination" 0 zone] ["shippingCents" 1 (amount version zone subtotal grams)]]
     37       _ = failure "Subtotal must be <=1000000 cents; grams must be 1..5000.")
     38     _ = failure "Unknown destination.")
     39   _ = failure "Subtotal and grams must be canonical natural numbers."
     40 
     41 v1 zone subtotal grams = quote 1 zone subtotal grams
     42 v2 zone subtotal grams = quote 2 zone subtotal grams