Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TDDwI: Minor changes to chapter10 #3433

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions docs/source/typedd/typedd.rst
Original file line number Diff line number Diff line change
Expand Up @@ -281,28 +281,23 @@ in the type:
.. code-block:: idris

snocListHelp : {input : _} ->
(snoc : SnocList input) -> (rest : List a) -> SnocList (input +
(snoc : SnocList input) -> (rest : List a) -> SnocList (input ++ rest)

It's no longer necessary to give ``{input}`` explicitly in the patterns for
``snocListHelp``, although it's harmless to do so.

In ``IsSuffix.idr``, the matching has to be written slightly differently. The
recursive with application in Idris 1 probably shouldn't have allowed this!
Note that the ``Snoc`` - ``Snoc`` case has to be written first otherwise Idris
generates a case tree splitting on ``input1`` and ``input2`` instead of the
``SnocList`` objects and this leads to a lot of cases being detected as missing.
``IsSuffix.idr`` also has to be adjusted to match the new ``Snoc`` arguments.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment probably can be improved. I'm especially confused as to why the new code doesn't need a | ysrec at line 300.


.. code-block:: idris

total
isSuffix : Eq a => List a -> List a -> Bool
isSuffix input1 input2 with (snocList input1, snocList input2)
isSuffix _ _ | (Snoc x xs xsrec, Snoc y ys ysrec)
= (x == y) && (isSuffix _ _ | (xsrec, ysrec))
isSuffix _ _ | (Empty, s) = True
isSuffix _ _ | (s, Empty) = False

This doesn't yet get past the totality checker, however, because it doesn't
know about looking inside pairs.
isSuffix input1 input2 with (snocList input1)
isSuffix [] input2 | Empty = True
isSuffix (xs ++ [x]) input2 | (Snoc x xs xsrec) with (snocList input2)
isSuffix (xs ++ [x]) [] | (Snoc x xs xsrec) | Empty = False
isSuffix (xs ++ [x]) (ys ++ [y]) | (Snoc x xs xsrec) | (Snoc y ys ysrec)
= if x == y then isSuffix xs ys | xsrec else False

For the ``VList`` view in the exercise 4 after Chapter 10-2 ``import Data.List.Views.Extra`` from ``contrib`` library.

Expand Down
13 changes: 7 additions & 6 deletions tests/typedd-book/chapter10/IsSuffix.idr
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import Data.List.Views

-- total
total
isSuffix : Eq a => List a -> List a -> Bool
isSuffix input1 input2 with (snocList input1, snocList input2)
isSuffix _ _ | (Snoc x xs xsrec, Snoc y ys ysrec)
= (x == y) && (isSuffix _ _ | (xsrec, ysrec))
isSuffix _ _ | (Empty, s) = True
isSuffix _ _ | (s, Empty) = False
isSuffix input1 input2 with (snocList input1)
isSuffix [] input2 | Empty = True
isSuffix (xs ++ [x]) input2 | (Snoc x xs xsrec) with (snocList input2)
isSuffix (xs ++ [x]) [] | (Snoc x xs xsrec) | Empty = False
isSuffix (xs ++ [x]) (ys ++ [y]) | (Snoc x xs xsrec) | (Snoc y ys ysrec)
= if x == y then isSuffix xs ys | xsrec else False
Loading