Skip to content

Refactor string functions, rewrite tests #2235

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

Closed

Conversation

dignissimus
Copy link

Resolves #2172

@Z-snails
Copy link
Collaborator

Z-snails commented Dec 31, 2021

You need to add the test to tests/Main.idr for it to actually run.
Edit: I would suggest adding a new TestPool, eg using testsInDir

Comment on lines 136 to 137
unlines [""] = "\n"
unlines = join "\n"
Copy link
Collaborator

Choose a reason for hiding this comment

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

All patterns in pattern matching must have the same count of arguments they are matching at, so you'd better to have unlines ss = join "\n" ss for the second line.

Anyway, this implementation is not equivalent to the current unlines implementation. For example, unlines ["", ""] now gives "\n\n", your implementation gives "\n".

Copy link
Collaborator

Choose a reason for hiding this comment

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

Just to further the point made in the above comment, here's a reference to when the semantics of lines/unlines were most recently changed: #1585

That is, the current semantics were intentional instead of accidental so we should honor them in the changes made in this PR. That might make the suggestion of using join less obvious of a win, at least for unlines, though I wish I'd thought to mention that on the open GitHub ticket in the first place.

Copy link
Author

Choose a reason for hiding this comment

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

Should unlines be written to use join? I ended up writing the following which doesn't include join so I'm considering reverting this specific change since it won't be any different to the original code.

unlines : List String -> String
unlines [] = ""
unlines (x::xs) = (x ++ "\n") ++ (unlines xs)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Since the function isn't publicly exported and it is transformed to fastUnlines at runtime, I suppose it's neither here nor there whether it unpacks and runs on characters or not. I think I slightly prefer your new approach personally.

@@ -1,5 +1,6 @@
module Data.String

import Data.String.Extra
Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks like a dependency of base to contrib which should not be. I think it's better to move join function from contrib to base for that case.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Big +1 from me on moving join from contrib to base.

Copy link
Author

Choose a reason for hiding this comment

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

||| Remove the first `n` characters from a string. Returns the empty string if
||| the input string is too short.
public export
drop : (n : Nat) -> (input : String) -> String
drop n str = substr n (length str) str
||| Concatenate the strings from a `Foldable` containing strings, separated by
||| the given string.
public export
join : (sep : String) -> Foldable t => (xs : t String) -> String
join sep xs = drop (length sep)
(foldl (\acc, x => acc ++ sep ++ x) "" xs)

How's this?

@mattpolzin
Copy link
Collaborator

You've gone a different direction with your test file -- making a single file and name spacing it by folder in a way that reminds me of unit tests instead of sequentially increasing test folders.

Your approach to the rename both makes sense to me and at the same time makes me wonder if it forces an awkward middle ground between the golden testing we have today and the unit testing we would ideally want for a module in the base library but cannot have yet because our test harness only supports golden.

So, I'm on the fence about the file renames and relocation within the test folders, but aside from that (as noted above by @Z-snails) , you'll need to add the test to the Main file in the test harness for it to be picked up (it isn't running right now).

@mattpolzin
Copy link
Collaborator

Right now you've got some build failures in the compiler where you've added functions to Data.String that were previously added to the compiler's internal Libraries.Data.String.Extra.

We can't remove the definitions from Libraries.Data.String.Extra until after the next release because the compiler depends on base and needs to be able to build against the previous version of base (before you added those functions to base).

This is awkward, but not uncommon when working in the compiler codebase. You should be able to add %hide Data.String.join (and for drop) to the top of files that currently use those functions. You could also modify those files in the compiler to fully qualify their calls to the functions as Libraries.Data.String.Extra.join.

After the next release, that workaround can be removed (as is done in another currently open PR for a similar reason: #2230).

@mattpolzin
Copy link
Collaborator

I'm immediately thinking the circumstance is different here and the compiler should I think complain if you try to hide the version you're adding now before it's available in the previous version of base.

You'll probably need to use the second approach I mentioned of fully qualifying the calls to Libraries.Data.String.Extra.

@@ -63,19 +63,19 @@ drop n str = substr n (length str) str
||| the input string is too short.
public export
dropLast : (n : Nat) -> (input : String) -> String
dropLast n str = reverse (drop n (reverse str))
dropLast n str = reverse (Libraries.Data.String.Extra.drop n (reverse str))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry my previous comment was unclear. This fully qualified call change is necessary inside the compiler codebase but in contrib (this file), there won't be ambiguity -- more importantly, the Libraries.Data.String.Extra module is not accessible from contrib.

In short, just revert the changes to this file.

@gallais gallais force-pushed the main branch 2 times, most recently from 20718fd to 2c9bf24 Compare February 3, 2022 18:41
@ProofOfKeags
Copy link
Contributor

Is this dead? Should I revive the effort in a different PR?

@andrevidela
Copy link
Collaborator

Given that this has gone stale for quite a while I'm going to close this. If someone else wants to try again, feel free (@ProofOfKeags)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor string functions using join
6 participants