-
Notifications
You must be signed in to change notification settings - Fork 70
Support for pickling of finite automata #125
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
Support for pickling of finite automata #125
Conversation
@caleb531 On a side note, I feel like the behavior for dfa_1 = DFA(
states={0, 1},
input_symbols={0, 1},
transitions={0: {0: 1, 1: 0}},
initial_state=0,
final_states={1},
allow_partial=True,
) The code throws an error one tries to do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@EduardoGoulart1 Wow, great catch—these are definitely cases that need to be considered.
However, it looks like the code coverage has decreased—can you please add tests for these new conditionals? Probably under tests/test_config.py
would be the best place to add them.
@caleb531 will do it later today. I will also to try to come up with a solution for the |
For the 1- Iterating over all inputs symbols without checking whether they are part of the state's transition function: dfa_2 = DFA(
states={0, 1},
input_symbols={0, 1},
transitions={0: {0: 1}},
initial_state=0,
final_states={1},
allow_partial=True,
)
intersect = dfa_2 & dfa_2 The code above fails because somewhere we do 2- States that have no transition function dfa_2 = DFA(
states={0, 1},
input_symbols={0, 1},
transitions={0: {0: 1, 1:1}},
initial_state=0,
final_states={1},
allow_partial=True,
)
intersect = dfa_2 & dfa_2 The code above fails because somewhere we do The first is a bug and we must rework the methods to correctly handle partial functions. The second is more on our internal representation and I would rather fix it by requiring that all states have a transition function (which could be empty). My suggestion is to create two separate issues. Then I can pick them up later when I have time |
@EduardoGoulart1 So, after having given it some thought, I am actually seriously considering removing @eliotwrobson When you have a moment, what are your thoughts on this? Here is the original issue that prompted the introduction of |
I think I'm in agreement, the allow partial option is really just a minor convenience that can be removed. If we wanted to instead, we could have the constructor add an extra dump state that represents the same operation as allow partial for missing transitions, but even that may be overkill. Using two different assumptions for the internal representation of the DFA causes more problems than it fixes. |
See the discussion at <#125 (comment)>.
@eliotwrobson All good points, and agreed. I think a sufficient replacement might be something like a However, that gets tricky when the user can make up any state names they want, so we would need some algorithm to determine the name of the throwaway state that doesn't conflict with any of the states they've defined. But maybe we could have Unless we do something like @EduardoGoulart1 please feel free to chime in as well, if you so desire. |
I think having an optional parameter for a throwaway state is fine. By default, we can just use the smallest nonnegative integer that doesn't conflict with the existing state names (I think something similar is used in some of the DFA algorithms) |
Related to this, @EduardoGoulart1 do you think there's a way to enable pickling support for immutable automatons? I think it would be nice to always have, especially if people are making large ones. |
This PR should address exactly that... I changed it so that you don't need to set |
@caleb531 and @eliotwrobson I don't think that the library should be guided by my specific needs, but On the other hand, I'm not too deep into the codebase to know how much pain |
My suggestion is to focus on this PR (pickling of FAs) and make an issue where we can discuss support for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@EduardoGoulart1 Looks pretty good! Just need a few docstrings for consistency, and we'll be set.
Hopefully it's good to go now |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@EduardoGoulart1 Yep, looks good to me. Thank you!
The initial use-case that I stumbled upon was to serialize DFAs with pickle. I would get an error because it was calling setattr, which is disabled even though I was setting the
allow_mutable_automata
flag.If we allow for mutable automata, then one should be able to modify its attributes.
Edit: After some consideration, I decided to disable set/get attributes as this will mess with the
validate
function. Instead serialization is implemented directly with the setattribute/getattribute functions