Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
lfa:lab02-dfa [2021/10/07 10:34] pdmatei |
lfa:lab02-dfa [2021/10/13 16:13] (current) ioana.georgescu [Classes in Python] |
||
---|---|---|---|
Line 10: | Line 10: | ||
class Example: | class Example: | ||
# the class constructor. | # the class constructor. | ||
- | def __init___(self,param1,param2): | + | def __init__(self,param1,param2): |
# the keyword self is similar to this from Java | # the keyword self is similar to this from Java | ||
# it is the only legal mode of initialising and referring class member variables | # it is the only legal mode of initialising and referring class member variables | ||
Line 43: | Line 43: | ||
===== 2.1. The class Dfa (Python) ===== | ===== 2.1. The class Dfa (Python) ===== | ||
- | 2.1.1 Define the class ''Dfa'' which encodes deterministic finite automata. It must store: | + | **2.1.1.** Define the class ''Dfa'' which encodes deterministic finite automata. It must store: |
* the alphabet | * the alphabet | ||
* the delta function | * the delta function | ||
Line 51: | Line 51: | ||
**Optional:** you may consider defining a class ''State'' to encode more general Dfas where states are not confined to integers. This will be useful later in your project. However, you will need to define a hash-function in order to use dictionaries over states. More details, google ''hashing in Python''. | **Optional:** you may consider defining a class ''State'' to encode more general Dfas where states are not confined to integers. This will be useful later in your project. However, you will need to define a hash-function in order to use dictionaries over states. More details, google ''hashing in Python''. | ||
- | 2.1.2. Define a constructor for Dfas, which takes a multi-line string of the following form: | + | **2.1.2.** Define a constructor for Dfas, which takes a multi-line string of the following form: |
<code> | <code> | ||
Line 62: | Line 62: | ||
where: | where: | ||
- | - the initial state is given on the first line of the input | + | * the initial state is given on the first line of the input |
- | - each of the subsequent lines encode transitions (states are integers) | + | * each of the subsequent lines encode transitions (states are integers) |
- | - the last line encodes the set of final states. | + | * the last line encodes the set of final states. |
Example: | Example: | ||
Line 75: | Line 75: | ||
</code> | </code> | ||
- | 2.1.3. Implement a member function which takes a **configuration** (pair of state and rest of word) and returns the next configuration. | + | **2.1.3.** Implement a member function which takes a **configuration** (pair of state and rest of word) and returns the next configuration. |
- | 2.1.4. Implement a member function which verifies if a word is **accepted** by a Dfa. | + | **2.1.4.** Implement a member function which verifies if a word is **accepted** by a Dfa. |
===== 2.2. The Dfa Algebraic Datatype (Haskell) ===== | ===== 2.2. The Dfa Algebraic Datatype (Haskell) ===== | ||
Line 90: | Line 90: | ||
How can we **group** all these constraints and support for future state operations? | How can we **group** all these constraints and support for future state operations? | ||
- | Finally, in order to encode transitions, we need the ''Map'' and ''Set'' datatypes which are available in their own modules: | + | Finally, in order to encode transitions and sets, we need the ''Map'' and ''Set'' datatypes which are available in their own modules: |
<code haskell> | <code haskell> | ||
{- We do import the data constructor Map and the infix function (!) as unqualified, to make them easier to use (Map.Map and Map.(!) is not very legible) -} | {- We do import the data constructor Map and the infix function (!) as unqualified, to make them easier to use (Map.Map and Map.(!) is not very legible) -} | ||
Line 104: | Line 104: | ||
import qualified Data.Set as Set | import qualified Data.Set as Set | ||
</code> | </code> | ||
+ | |||
+ | <blockquote>** How to choose between lists and sets during the implementation?** | ||
+ | * Do you need to make sure elements are unique? (go for sets) | ||
+ | * Do you need to iterate a lot over elements, and the collection size is not really big (go for lists) | ||
+ | </blockquote> | ||
+ | **2.2.1.** Implement the datatype ''DFA''. It must store: | ||
+ | * the alphabet | ||
+ | * the delta function | ||
+ | * the initial state | ||
+ | * the set of final states | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | **2.2.2.** Define a function which takes a string of the following form showed below, and returns a DFA **with states as integers**: | ||
+ | <code> | ||
+ | <initial_state> | ||
+ | <state> <char> <state> | ||
+ | ... | ||
+ | ... | ||
+ | <final_state_1> <final_state_2> ... <final_state_n> | ||
+ | </code> | ||
+ | |||
+ | where: | ||
+ | * the initial state is given on the first line of the input | ||
+ | * each of the subsequent lines encode transitions (states are integers) | ||
+ | * the last line encodes the set of final states. | ||
+ | |||
+ | Example: | ||
+ | <code> | ||
+ | 0 | ||
+ | 0 a 1 | ||
+ | 1 b 2 | ||
+ | 2 a 0 | ||
+ | 1 2 | ||
+ | </code> | ||
+ | |||
+ | **Hint:** Use ''splitBy'' from PP. | ||
+ | ---- | ||
+ | |||
+ | |||
+ | **2.2.3.** Enroll the DFA type in class Show. | ||
+ | |||
+ | **2.2.4.** Implement function which takes a DFA and a **configuration** (pair of state and rest of word) and returns the next configuration. | ||
+ | |||
+ | **2.2.5.** Implement a function which verifies if a word is **accepted** by a Dfa. What kind of general list-operation best matches the accepting process? | ||
+ | |||
===== 2.3. Dfa practice ===== | ===== 2.3. Dfa practice ===== | ||
Line 109: | Line 156: | ||
Write Dfas and test them using your implementation, for the following languages: | Write Dfas and test them using your implementation, for the following languages: | ||
- | * 2.2.2. $ L=\{w \in \{0,1\}^* \text{ | w contains an odd number of 1s} \} $ | + | * **2.3.1.** $ L=\{w \in \{0,1\}^* \text{ | w contains an odd number of 1s} \} $ |
- | * 2.2.3. The language of binary words which contain **exactly** two ones | + | * **2.3.2.** The language of binary words which contain **exactly** two ones |
- | * 2.2.4. The language of binary words which encode odd numbers (the last digit is least significative) | + | * **2.3.3.** The language of binary words which encode odd numbers (the last digit is least significative) |
- | * 2.2.5. (hard) The language of words which encode numbers divisible by 3. | + | * **2.3.4.** (hard) The language of words which encode numbers divisible by 3. |