Commit ad809b09 authored by Michael Hanus 's avatar Michael Hanus
Browse files

System libraries WUI and WUIjs packaged

parents
*~
.cpm
.curry
Copyright (c) 2017, Michael Hanus
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the names of the copyright holders nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
wui: Type-Oriented Web User Interfaces
======================================
This package contains libraries to support the type-oriented construction
of Web User Interfaces (WUIs).
The ideas behind the application and implementation of WUIs are
described in these papers:
M. Hanus:
[Type-Oriented Construction of Web User Interfaces](http://doi.acm.org/10.1145/1140335.1140341)
Proc. of the 8th International ACM SIGPLAN Conference on Principles and Practice of Declarative Programming (PPDP'06), ACM Press, pp. 27-38, 2006
M. Hanus:
[Putting Declarative Programming into the Web: Translating Curry to JavaScript](http://doi.acm.org/10.1145/1273920.1273942)
Proc. of the 9th International ACM SIGPLAN Conference on Principles and Practice of Declarative Programming (PPDP'06), ACM Press, pp. 155-166, 2007
--------------------------------------------------------------------------
-- A simple example to demonstrate Web User Interfaces and
-- the Curry->JavaScript translator.
-- This example implements a dynamic web page to edit persons
-- consisting of name, email, and date of birth.
-- If JavaScript is enabled, wrong dates in input fields are immediately
-- detected and shown as error in the web page.
--
-- The background about this example and the Curry->JavaScript translator
-- are described in:
--
-- M. Hanus:
-- Putting Declarative Programming into the Web: Translating Curry to JavaScript
-- Proceedings of the 9th ACM SIGPLAN International Conference on
-- rinciples and Practice of Declarative Programming (PPDP'07),
-- ACM Press, 2007, pp. 155-166
module Persons where
import HTML
import WUIjs
import Time(validDate)
-- a form that just shows its argument:
resultForm :: a -> IO HtmlForm
resultForm v = return $ form "Result" [htxt ("Modified value: "++show v)]
-- a date WUI:
wDate = wTriple (wSelectInt [1..31]) (wSelectInt [1..12]) wInt
`withConditionJS` correctDate
`withError` "Illegal date:"
correctDate (d,m,y) = validDate y m d
-- an email WUI:
wEmail = wStringSize 20 `withConditionJS` correctEmail
`withError` "Invalid email address:"
correctEmail s = not (null (snd (break ('@'==) s)))
-- a person WUI:
wPerson = w4Tuple (wRequiredStringSize 12) (wRequiredStringSize 12) wEmail wDate
-- a WUI for persons:
main = mainWUI (wList wPerson) persons resultForm
persons = [("Bob","Carter","bob@carter.com",(3,10,1965))
,("Bill","Jones","billy@acm.org",(29,2,1982))
,("Joe","Smith","smith.org",(20,3,1978))
]
-- To install this example as an interactive web page:
--
-- First, ensure that curry2js is installed, e.g.:
--
-- > cpm update && cpm installbin curry2js
--
-- Then compile and install this script into your home www directory:
--
-- > cpm exec curry makecgi -wuijs -o ~/public_html/persons.cgi Persons
-----------------------------------------------------------------------------
--- Solving Su Doku puzzles in Curry with a web-based interface
---
--- @author Michael Hanus
--- @version May 2017
-----------------------------------------------------------------------------
import AllSolutions (getOneSolution)
import CLPFD
import HTML
import List (transpose)
import WUI
-----------------------------------------------------------------------------
-- Solving a Su Doku puzzle represented as a matrix of numbers (possibly free
-- variables):
sudoku :: [[Int]] -> Bool
sudoku m =
domain (concat m) 1 9 & -- define domain of all digits
all allDifferent m & -- all rows contain different digits
all allDifferent (transpose m) & -- all columns have different digits
all allDifferent (squares m) & -- all 3x3 squares are different
labeling [FirstFailConstrained] (concat m)
where
-- translate a matrix into a list of small 3x3 squares
squares :: [[a]] -> [[a]]
squares [] = []
squares (l1:l2:l3:ls) = group3Rows [l1,l2,l3] ++ squares ls
group3Rows l123 = if head l123 == [] then [] else
concatMap (take 3) l123 : group3Rows (map (drop 3) l123)
-- Compute one solution to a SuDoKu puzzle by encapsulated search:
solveSudoku :: [[Int]] -> IO (Maybe [[Int]])
solveSudoku s = getOneSolution (\m -> m=:=map (map transDigit) s &> sudoku m)
where transDigit i = if i==0 then _ else i
-----------------------------------------------------------------------------
-- the main form to input SuDoKu puzzles:
initForm :: [[Int]] -> IO HtmlForm
initForm s = let (hexp,handler) = wui2html wSudoku s solveForm in
return $ standardForm "SuDoku" [hexp, wuiHandler2button "Solve" handler]
-- the specification of the SuDoku WUI:
wSudoku :: WuiSpec [[Int]]
wSudoku = wMatrix (wSelect (\i->if i==0 then " " else show i) [0..9])
solveForm :: [[Int]] -> IO HtmlForm
solveForm m = do
mbsol <- solveSudoku m
return $ standardForm "SuDoku" $
maybe [h1 [htxt "No solution"]]
(\sol -> [fst (wui2html (wMatrix (wConstant (\d->HtmlText ("&nbsp;"++show d++"&nbsp;"))))
sol initForm)])
mbsol
-- An empty initial form:
formEmpty :: IO HtmlForm
formEmpty = initForm (map (const (take 9 (repeat 0))) [1..9])
formExample :: IO HtmlForm
formExample =
initForm [ [9,0,0,2,0,0,5,0,0]
, [0,4,0,0,6,0,0,3,0]
, [0,0,3,0,0,0,0,0,6]
, [0,0,0,9,0,0,2,0,0]
, [0,0,0,0,5,0,0,8,0]
, [0,0,7,0,0,4,0,0,3]
, [7,0,0,0,0,0,1,0,0]
, [0,5,0,0,2,0,0,4,0]
, [0,0,1,0,0,6,0,0,9] ]
-- Generate cgi program with:
-- cpm exec curry makecgi -m formExample -o ~/public_html/sudoku.cgi sudoku
{
"name": "wui",
"version": "1.0.0",
"author": "Michael Hanus <mh@informatik.uni-kiel.de>",
"synopsis": "Libraries to support the type-oriented construction of web user interfaces",
"category": [ "User Interface", "Web" ],
"dependencies": {
"javascript": ">= 1.0.0"
},
"exportedModules": [ "WUI", "WUIjs" ],
"compilerCompatibility": {
"pakcs": ">= 1.14.0, < 2.0.0",
"kics2": ">= 0.5.0, < 2.0.0"
},
"license": "BSD-3-Clause",
"licenseFile": "LICENSE",
"testsuite": [
{ "src-dir": "src",
"options": "-v --nosource",
"modules": [ "WUI", "WUIjs" ]
},
{ "src-dir": "examples",
"options": "-v",
"modules": [ "Persons", "sudoku" ]
}
],
"source": {
"git": "https://git.ps.informatik.uni-kiel.de/curry-packages/wui.git",
"tag": "$version"
}
}
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment