Documentation Menu · What Hector removes for a C++ or Rust programmer
What Hector removes for a C++ or Rust programmer
Every section contrasts technical infrastructure you write by hand in C++ or Rust against the pure business rule left in Hector.
Each section contrasts a technical decision you might write by hand with the business rule left in Hector. Each example is a complete program checked by hector check.
An index #
In C++ or Rust, fast row lookup may mean choosing a hash table or tree, updating it for each creation and checking uniqueness yourself. In Hector, state the rule once: a card code is unique. Hector refuses a duplicate without an index written by you. Choosing among access structures for the load belongs to stage 4 of the contract and is not demonstrated by this program.
-- but: une carte d'accès a un code unique ; on la retrouve par son code
Desk
Card code: Text, holder: Text
law unique(code)
show holder to Desk within 1 s
-> on Issue by Desk
example code unique
c = Issue(code: "A-17", holder: "Ana")
see c.holder = "Ana"
refused Issue(code: "A-17", holder: "Bob")
A cache #
In C++ or Rust, a free-seat count may be a field decremented on reservation and repaired on cancellation at each place that changes the room. In Hector, write the definition once (seats minus reservations) and the law (never negative). Hector maintains the total by differences; the contract places these totals in the business core, which computes only what is read. You write no counter update or consistency check.
-- but: une salle a un nombre de places ; chaque réservation en prend une, jamais plus
Desk
Room name: Text, seats: Count
free = seats - count(bookings)
law free >= 0
show name, free to Desk within 1 s
Booking room: Room, guest: Text
-> on Book by Desk
example salle pleine
r = Room(name: "Salle A", seats: 2)
Book(room: r, guest: "Ana")
Book(room: r, guest: "Bob")
see r.free = 0
refused Book(room: r, guest: "Eve")
Memory and layout #
In C++ or Rust, you choose field order, width, alignment and array layout, and bound integers yourself. In Hector, state the range of a quantity and what a thing is. Hector chooses the column layout and retains the bound as a fact: check reports the chosen layout, and a batch outside the range is refused. The contract assigns load-dependent layout choice to Hector; this program shows the chosen layout, not a comparison against another one.
-- but: un lot de semences compte entre 0 et 500 sacs ; un lot hors de cette plage est refusé
Desk
Lot variety: Text, bags: Count 0..500
show variety, bags to Desk within 1 s
-> on Receive by Desk
example stock borné
l = Receive(variety: "Pois", bags: 120)
see l.bags = 120
refused Receive(variety: "Fève", bags: 501)
A lock or a guard #
In C++ or Rust, preventing a second sale may require a mutex around the check and write, released on every exit. In Hector, state the rule: a seat is sold only once. Hector derives the guard that refuses the second sale; the contract forbids a lock in the native step. You write neither the guard nor an operation order.
-- but: un siège ne se vend qu'une fois ; la deuxième vente est refusée
Desk
Seat label: Text
sold = count(sales)
law sold <= 1
show label, sold to Desk within 1 s
Sale seat: Seat, buyer: Text
-> on Sell by Desk
example deux clients pour un siège
s = Seat(label: "B4")
Sell(seat: s, buyer: "Ana")
see s.sold = 1
refused Sell(seat: s, buyer: "Bob")