Package: lstar
L*Implementation of the discriminant tree L* algorithm DFA learning algorithm
provided in [^1].Table of ContentsInstallationUsageLabel QueriesEquivalence QueriesAll togetherLearning Moore Machines and DFA-labelersTestingFootnotesInstallationIf you just need to uselstar, you can just run:$ pip install lstarFor developers, note that this project uses thepoetrypython package/dependency
management tool. Please familarize yourself with it and then
run:$ poetry installUsageThe main entry point for using this library is thelearn_dfafunction.fromlstarimportlearn_dfaThis function requires the arguments:dfa=learn_dfa(inputs=..,#  Inputs over which the target concept is over.#  Note: Sequence of Hashables.label=..,#  Function answering whether a given word is in the target#  language.##  Tuple[Alphabet] -> boolfind_counter_example=..,#  Function which takes a hypothesis DFA#  and either returns None or a counter example,#  i.e., an element misclassified by hypothesis#  DFA.##  DFA -> Union[Tuple[Alphabet], None])Below is an example of learning following language over{0, 1}:The number of 1's in the word is a multiple of 4.Label QueriesWe start by defining the label query function.Notethat this implementation oflstarassumes that this
function is either cheap (O(1)-ish) to call or is memoized.fromfunctoolsimportlru_cache@lru_cache(maxsize=None)# Memoize member queriesdefis_mult_4(word):"""Want to learn 4 state counter"""return(sum(word)%4)==0Equivalence QueriesNext you need to define a function which given a candidateDFAreturns either a counter example that thisDFAmislabels orNone.Note that theDFAtype used comes from thedfapackage
(link).lstarprovides two functions to make writing counterexample oracles
easier.validate_ce: Takes a counterexample oracle and retries
if returned "counterexample" is not actually a counterexample.
Useful if using heuristic solver or asking a human.fromlstarimportvalidate_ce@validate_ce(is_mult_4,retry=True)defask_human(dfa):...iterative_deeping_ce: This function performs an iterative
deepening traversal of the candidate dfa and see's if it matches
the labeler on all tested words.fromlstarimportiterative_deeping_cefind_ce=iterative_deeping_ce(is_mult_4,depth=10)All togetherdfa=learn_dfa(inputs={0,1},#  Possible inputs.label=is_mult_4,#  Does this sequence belong in the language.find_counter_example=iterative_deeping_ce(is_mult_4,depth=10))assertnotdfa.label(())assertnotdfa.label((1,))assertnotdfa.label((1,1,))assertdfa.label((1,1,1))assertdfa.label((1,1,0,1))Learning Moore Machines and DFA-labelersBy default,learn_dfalearns as Deterministic Finite Acceptor;
however, by specifying theoutputsparameter and adjusting thelabelfunction, one can learn a Deterministic Finite Labeler
(which is isomorphic to a Moore Machine).For example, the 4 state counter from before can be modified to output
the current count rather than whether or not the word sums to a
multiple of 4.defsum_mod_4(state):returnsum(state)%4dfl=learn_dfa(inputs={0,1},label=sum_mod_4,find_counter_example=ask_human,outputs={0,1,2,3},)# Returns a Deterministic Finite Labeler.assertdfl.label(())==0assertdfl.label((1,))==1assertdfl.label((1,1,))==2assertdfl.label((1,1,1))==3assertdfl.label((1,1,0,1))==3assertdfl.label((1,1,1,1))==0The deterministic labeler can be interpreted as a moore machine by
using thetransducemethod rather thanlabel.assertdfl.transduce(())==()assertdfl.transduce((1,))==(0,)assertdfl.transduce((1,1,))==(0,1)assertdfl.transduce((1,1,1))==(0,1,2)assertdfl.transduce((1,1,0,1))==(0,1,2,2)assertdfl.transduce((1,1,1,1,1))==(0,1,2,3,0)TestingThis project uses pytest. Simply run$ poetry run pytestin the root of the repository.Similar LibrariesPython Based1. https://github.com/steynvl/inferrer : DFA learning
   library supporting active and passive dfa learning. Active
   learning is based on L* with an observation table. Also
   supports learning NFAs.https://gitlab.lis-lab.fr/dev/scikit-splearn/: Library for learning
weighted automata via the spectral method.https://pypi.org/project/pylstar/: Another L* based DFA
learning library.Java Basedhttps://learnlib.de/: State of the art automata learning
toolbox. Supports passive and active learning algorithms for DFAs,
Mealy Machines, and Visibly Push Down Automata.https://github.com/lorisdanto/symbolicautomata: Library for
symbolic automata and symbolic visibly pushdown automata.Footnotes[^1]: Kearns, Michael J., Umesh Virkumar Vazirani, and Umesh Vazirani. An introduction to computational learning theory. MIT press, 1994.