Commit 34025f5f authored by Michael Hanus 's avatar Michael Hanus
Browse files

Manual and examples updated

parent 6c21e583
......@@ -80,12 +80,10 @@ S.~Antoy and M.~Hanus.
(Constraint) Logic Programming (WFLP 2014)}, volume 1335 of {\em {CEUR}
Workshop Proceedings}, pages 140--154. CEUR-WS.org, 2014.
\bibitem{AntoyHanus16PADL}
\bibitem{AntoyHanus17TPLP}
S.~Antoy and M.~Hanus.
\newblock Default rules for {Curry}.
\newblock In {\em Proc. of the 18th International Symposium on Practical
Aspects of Declarative Languages (PADL 2016)}, pages 65--82. Springer LNCS
9585, 2016.
\newblock {\em Theory and Practice of Logic Programming}, 17(2):121--147, 2017.
\bibitem{BrasselHanusMueller08PADL}
B.~Bra{\ss}el, M.~Hanus, and M.~M{\"u}ller.
......
......@@ -19,10 +19,8 @@ like regular expressions, format specifications (\ccode{printf}),
HTML and XML code.
\item[Default rules:]
If this feature is used, one can add a default rule
to operations defined in a Curry module.
This provides a similar power than sequential rules
but with a better operational behavior.
The idea of default rules is described in \cite{AntoyHanus16PADL}.
to top-level operations defined in a Curry module.
The idea of default rules is described in \cite{AntoyHanus17TPLP}.
\item[Contracts:]
If this feature is used, the Curry preprocessor looks for contracts
(i.e., specification, pre- and postconditions) occurring in a Curry module
......@@ -660,11 +658,9 @@ SQL translator can be found in \cite{HanusKrone17EPTCS,Krone15}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Default Rules}
An alternative to sequential rules are default rules, i.e.,
these two options cannot be simultaneously used.
Default rules are activated by the preprocessor option
\ccode{defaultrules}.
In this case, one can add to each operation a default rule.
In this case, one can add to each top-level operation a default rule.
A default rule for a function $f$ is defined as a rule
defining the operation \ccode{$f$'default} (this mechanism
avoids any language extension for default rules).
......@@ -672,23 +668,19 @@ A default rule is applied only if no ``standard'' rule is
applicable, either because the left-hand sides' pattern do not match
or the conditions are not satisfiable.
The idea and detailed semantics of
default rules are described in \cite{AntoyHanus16PADL}.
default rules are described in \cite{AntoyHanus17TPLP}.
Default rules are preferable over the sequential rule selection
strategy since they have a better operational behavior.
This is due to the fact that the test for the application
of default rules is done with the same (sometimes optimal)
strategy than the selection of standard rules.
Moreover, default rules provide a similar power than sequential rules,
i.e., they can be applied if the standard rules have
complex (functional) patterns or complex conditions.
As a simple example, we show the implementation of the
previous example for sequential rules with a default rule:
As a simple example, the following program defines a lookup operation
in association lists by a functional pattern.
The default rule is applied only if there is no
appropriate key in the association list (the role of the \code{import}
declarations is discussed below):
%
\begin{curry}
{-# OPTIONS_FRONTEND -F --pgmF=currypp --optF=defaultrules #-}
import Control.SetFunctions
mlookup key (_ ++ [(key,value)] ++ _) = Just value
mlookup'default _ _ = Nothing
\end{curry}
......@@ -703,10 +695,17 @@ of the queens on the chessboard (see the first rule of \code{safe}):
\begin{curry}
{-# OPTIONS_FRONTEND -F --pgmF=currypp --optF=defaultrules #-}
import Combinatorial(permute)
import Integer(abs)
import Control.SetFunctions
-- Some permutation of a list of elements:
perm :: [a] -> [a]
perm [] = []
perm (x:xs) = ndinsert (perm xs)
where ndinsert ys = x : ys
ndinsert (y:ys) = y : ndinsert ys
-- A placement is safe if two queens are not in a same diagonal:
safe :: [Int] -> [Int]
safe (_++[x]++ys++[z]++_) | abs (x-z) == length ys + 1 = failed
safe'default xs = xs
......@@ -715,12 +714,14 @@ queens :: Int -> [Int]
queens n = safe (permute [1..n])
\end{curry}
\paragraph{Important note:}
\paragraph{Important notes:}
\begin{enumerate}
\item
The implementation of default rules is based on set functions
(implemented by the module \code{Control.SetFunctions}).
Therefore, the package \code{setfunctions} should be installed
as a dependency.
This can easily done by executing
This can easily be done by executing
%
\begin{curry}
> cypm add setfunctions
......@@ -729,6 +730,15 @@ This can easily done by executing
before compiling a program containing default rules with the
Curry preprocessor.
\item
Default rules can only be added to operations defined at the top-level
(i.e., not to locally defined operations).
A reason for this restriction is that default rules are applied
after searching for all possibilities to apply a previous standard rule.
With local definitions, the precise scope of the ``previous'' search
is difficult to define.
\end{enumerate}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Contracts}
......
{-# OPTIONS_FRONTEND -F --pgmF=currypp --optF=defaultrules #-}
import Control.SetFunctions
import Test.Prop
-- The following task should be solved:
......
{-# OPTIONS_FRONTEND -F --pgmF=currypp --optF=defaultrules #-}
{-# OPTIONS_FRONTEND -Wnone #-}
import Control.SetFunctions
import Test.Prop
-- The implementation of coloring a map using a default rule
......
{-# OPTIONS_FRONTEND -F --pgmF=currypp --optF=defaultrules #-}
import Control.SetFunctions
import Test.Prop
-- Dijsktra's Dutch National Flag problem with functional patterns
......
{-# OPTIONS_FRONTEND -F --pgmF=currypp --optF=defaultrules #-}
import Control.SetFunctions
import Test.Prop
-- Operation to delete suffixes of the fornm ".0"
......
{-# OPTIONS_FRONTEND -F --pgmF=currypp --optF=defaultrules #-}
import Control.SetFunctions
import Test.Prop
-- Example: predicate to check for float strings
......
{-# OPTIONS_FRONTEND -F --pgmF=currypp --optF=defaultrules #-}
{-# OPTIONS_FRONTEND -Wnone #-}
import Control.SetFunctions
import Test.Prop
-- Examples for multiple rules with guards:
......
{-# OPTIONS_FRONTEND -F --pgmF=currypp --optF=defaultrules #-}
{-# OPTIONS_FRONTEND -Wnone #-}
import Control.SetFunctions
import Test.Prop
-- Warning: these redefinitions work fine with KiCS2, but with PAKCS
......
{-# OPTIONS_FRONTEND -F --pgmF=currypp --optF=defaultrules #-}
import Control.SetFunctions
import Test.Prop
-- Lookup with default rules:
......
{-# OPTIONS_FRONTEND -F --pgmF=currypp --optF=defaultrules #-}
{-# OPTIONS_FRONTEND -Wnone #-}
import Control.SetFunctions
import Test.Prop
-- A simple implementation of a two-person game (here: Nim) with default rules
......
{-# OPTIONS_FRONTEND -F --pgmF=currypp --optF=defaultrules #-}
{-# OPTIONS_FRONTEND -Wnone #-}
import Control.SetFunctions
import Test.Prop
-- Parallel or with default rules:
......
{-# OPTIONS_FRONTEND -F --pgmF=currypp --optF=defaultrules #-}
{-# OPTIONS_FRONTEND -Wnone #-}
import Control.SetFunctions
import Test.Prop
-------------------------------------------------------------------------
......@@ -8,13 +9,14 @@ import Test.Prop
-- Some permutation of a list of elements:
perm :: [a] -> [a]
perm [] = []
perm [] = []
perm (x:xs) = ndinsert (perm xs)
where
ndinsert ys = x : ys
ndinsert (y:ys) = y : ndinsert ys
-- A placement is safe if two queens are not in a same diagonal:
safe :: [Int] -> [Int]
safe (_++[x]++y++[z]++_) | abs (x-z) == length y + 1 = failed
safe'default xs = xs
......
......@@ -2,6 +2,7 @@
{-# OPTIONS_FRONTEND -Wnone #-}
import Test.Prop
import Control.SetFunctions -- required by default rules
-- Reverse a list if it has exactly two elements:
rev2 :: Data a => [a] -> [a]
......
{-# OPTIONS_FRONTEND -F --pgmF=currypp --optF=defaultrules #-}
{-# OPTIONS_FRONTEND -Wnone #-}
import Control.SetFunctions
import Test.Prop
-- Example: parse World Cup soccer scores (e.g., "_:_", "3:2")
......
{-# OPTIONS_FRONTEND -F --pgmF=currypp --optF=defaultrules #-}
import Control.SetFunctions -- required by default rules
import Test.Prop
-- Bubble sort formulation with default rule as deterministic function
......
{-# OPTIONS_FRONTEND -F --pgmF=currypp --optF=defaultrules #-}
import Control.SetFunctions -- required by default rules
import Test.Prop
-- Dijsktra's Dutch National Flag problem with functional patterns,
......
{-# OPTIONS_FRONTEND -F --pgmF=currypp --optF=defaultrules #-}
import Control.SetFunctions -- required by deterministic functions
import Test.Prop
-- Parallel or declared as a deterministic function:
......
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