These are chat archives for evhub/coconut

10th
Aug 2016
Chuck Daniels
@chuckwondo
Aug 10 2016 15:34

@evhub I love the ability to add function patterns via the @addpattern decorator. In the name of brevity, I think it would be nice to be able to eliminate the explicit use of the decorator and have the compiler automatically detect additional patterns. However, I don't know if that's possible, nor if it is otherwise undesirable. For example, rather than writing the following (taken from your docs):

def size(Empty()) = 0

@addpattern(size)
def size(Leaf(_)) = 1

@addpattern(size)
def size(Node(left, right)) = size(left) + size(right)

I'd like to be able to write the following (perhaps the leading match keyword would be helpful/necessary):

match def size(Empty()) = 0
match def size(Leaf(_)) = 1
match def size(Node(left, right)) = size(left) + size(right)

This would make it arguably cleaner (and closer to the simpler Haskell syntax). What say you? :-)

Chuck Daniels
@chuckwondo
Aug 10 2016 17:20

@evhub In the generated python files, the following appears:

import __coconut__
_coconut_sys.path.remove(_coconut_file_path)
for name in dir(__coconut__):
    if not name.startswith("__"):
        globals()[name] = getattr(__coconut__, name)

Unfortunately, this makes such items added to the globals unresolvable within IDEs, such as PyCharm. For instance, _coconut and _coconut_MatchError cannot be resolved in an IDE. Is there any reason you don’t simply use from __coconut__ import *, which would allow IDEs to resolve such references? Doesn’t that have the same effect as the code block above? (I’m not a Python expert, so I could very well be missing something.)

Chuck Daniels
@chuckwondo
Aug 10 2016 17:43
@evhub I see part of the problem with my suggestion regarding the * import, which is that the “protected” members (i.e., anything with a name starting with an underscore) won’t be imported that way. However, why bother with the underscore prefixes if you’re having to do your trick above (i.e., explicitly modifying the globals)?
Evan Hubinger
@evhub
Aug 10 2016 22:41
@chuckwondo The original point of the underscore was to let people know that those variables were "reserved" for the compiler and not to use them in their own code. Although considering that every single one starts with "coconut" regardless, I doubt anyone is using those variable names anyway. Honestly, it might be worth removing the underscore prefix, actually, if doing so would help IDEs.
@chuckwondo As for your addpattern question, you may be interested in reading the discussion on #125, which was a ticket for doing something very similar. That ticket ended up getting closed because none of the other possible options seemed better.