2

In section 3.5.6 of the Curry tutorial (pdf), we are advised to use default rules to "regain control after a failed search". The following example is given. (For clarity I have added a type signature and curried the input.)

lookup :: k -> [(k,v)] -> Maybe v
lookup key (_++[(key,value)]++_ ) = Just value
lookup’default _ _ = Nothing

I can't get that to compile unless I replace the with a '. Once I do, it behaves like this:

test> test.lookup 1 [(2,3)]
*** No value found!

Question 1: What is the default declaration for?

Why would you need to specify that a particular clause is the default one? Won't it be arrived at one way or another, once the others fail?

Question 2: How is it written? Should it be written at all?

If instead I drop the string 'default:

lookup :: k -> [(k,v)] -> Maybe v
lookup key (_++[(key,value)]++_ ) = Just value
lookup _ _ = Nothing

it behaves as intended:

test> test.lookup 1 [(2,3)]
Nothing
test>

Has the 'default syntax changed since the tutorial was written? Has it been removed altogether?

Jeffrey Benjamin Brown
  • 3,427
  • 2
  • 28
  • 40

2 Answers2

3

This is the code you are looking for. Yours was missing the preprocessor directive to allow default rules. And using the wrong quote character.

-- Use default rules
{-# OPTIONS_CYMAKE -F --pgmF=currypp --optF=defaultrules #-}

lookup :: k -> [(k,v)] -> Maybe v
lookup key (_++[(key,value)]++_ ) = Just value
lookup'default _ _ = Nothing

test_positive = lookup 2 [(2,3)] == Just 3
test_negative = lookup 1 [(2,3)] == Nothing

A default rule serves various purposes. Regaining control after a failed search is a particularly useful one, since you cannot check with equality whether an expression is a failure.

  • 1
    That `OPTIONS` line gave me errors until I deleted `-F` from it. Now it compiles, but I don't detect an effect. I can define `myLookup` (`Prelude.lookup` seems to shadow it if I use the name `lookup`) with the rule `myLookup _ _ = Nothing`, or I can instead use the rule `myLookup'default _ _ = Nothing`, but in both cases I get the same behavior: In the REPL, `myLookup 1 [(1,2)]` yields first `Just 1` and then `Nothing`, while `myLookup 2 [(1,2)]` yields the single value `Nothing`. (I'm running PAKCS, version 2.0.1-1, installed via apt on Ubuntu 18.04.) – Jeffrey Benjamin Brown Nov 19 '18 at 03:46
  • Please check if you have the executable `currypp` in your path. As far as I know, it comes with your Curry installation, but I'm not completely sure. – ichistmeinname Nov 20 '18 at 07:44
  • I tried `cypm update && cypm install currypp`, imitating this [advice](https://www.informatik.uni-kiel.de/~curry/listarchive/1328.html), which installed currypp. Loading [test.curry](https://github.com/JeffreyBenjaminBrown/curry-studies/blob/a92adf1ab80dcaeba0b2aebc15628f014475dd61/test.curry) from the `pakcs` repl now produces this [error](https://github.com/JeffreyBenjaminBrown/curry-studies/blob/a92adf1ab80dcaeba0b2aebc15628f014475dd61/error.txt). I'm using "PAKCS Version 2 with type classes," installed per instructions [here](https://packages.ps.informatik.uni-kiel.de/curry/index.html). – Jeffrey Benjamin Brown Nov 22 '18 at 05:12
  • Per responses to [my mailing list post](https://www.informatik.uni-kiel.de/~curry/listarchive/1325.html), I ran `apt remote pakcs` and then installed by making `pakcs-2.0.2-amd64-Linux.tar.gz`. `currypp` is still installed from the apt version of pakcs. Is that bad? [test.curry](https://github.com/JeffreyBenjaminBrown/curry-studies/blob/740cdd8c0d5f45caa597c3bd128426642dcadba8/test.curry) now produces a ["no frontend" error](https://github.com/JeffreyBenjaminBrown/curry-studies/blob/740cdd8c0d5f45caa597c3bd128426642dcadba8/error-2.txt) – Jeffrey Benjamin Brown Nov 24 '18 at 02:49
  • Since the tar.gz version is probably installed in some local directory, you have to install currypp again. Moreover, the `pakcs/bin` directory should be in your path so that currypp can find the front end. – Michael Hanus Nov 25 '18 at 16:44
2

If you delete the option "-F", then the preprocessor is not invoked which explains the behavior.

The permission error is due to the fact that not all possible intermediate representations of a Curry program are precompiled in the Ubuntu package. Unfortunately, the "default rule translator" of CurryPP requires one of these intermediate representations.

The Ubuntu/Debian package is intended only for using the kernel of Curry. For extensions and more advanced tools, I recommend to install PAKCS manually, e.g., the current release from https://www.informatik.uni-kiel.de/~pakcs/download.html If you already have Ubuntu, a simple make should be sufficient.