typename Date = [|Date:(Int,Int)|];
typename BookingInfo = (person:String, age:Int, arrival:Date, departure:Date);
var checkedInt = inputInt;
sig date : Formlet(Date)
var date =
formlet
<#>
{ checkedInt -> day } / { checkedInt -> mo }
#>
yields Date(day, mo);
fun dateToXml(Date(d, m)) { {intToXml(d)}/{intToXml(m)} }
sig travelForm : Formlet(BookingInfo)
var travelForm =
formlet
| Person: | { input -> person } |
| Age: | { checkedInt -> age } |
| Arrival: | { date -> arr } |
| Departure: | { date -> dep } |
| { submit("Submit") } |
yields
(person=person, age=age, arrival = arr, departure = dep);
sig isBefore : (Date,Date) -> Bool
fun isBefore(Date (day1, month1), Date (day2, month2)) {
month1 < month2 || (month1 == month2 && day1 < day2)
}
sig checkBookingInfo : (BookingInfo) -> Bool
fun checkBookingInfo((person= _, age= _, arrival=arrival, departure=departure)) {
not (departure `isBefore` arrival)
}
sig checkedForm : Formlet(BookingInfo)
var checkedForm = travelForm `satisfies` (checkBookingInfo `errorMsg` fun (_) { "You can't depart before you arrive" });
fun showBookingInformation((person=p, age=a, arrival=arr, departure=dep)) {
page
Results
You are {stringToXml(p)}, {intToXml(a)} years old.
You'll arrive on {dateToXml(arr)}
and leave on {dateToXml(dep)}.
}
fun main() {
page
Date:
{ checkedForm => showBookingInformation }
}
main()