Live data from Hacker News

What is the enlightenment I'm supposed to attain after studying finite automata?

cstheory.stackexchange.com

51–60 of 66 posts

Re: What is the enlightenment I'm supposed to attain after studying finite automata?

#51
"Aha! moment". Well, I have to say that, for me, this came with the practice, not the study, of FSM's and, in particular Turing Machines. I could not imagine a world without them. Look around you.

While I studied FA formally it never really clicked until years later. Sometimes you don't really learn until you are holding a cat by the tail.

I should say that I've always called them "FSM" when, in most cases, in reality I was using a TM. A TM, of course, is also an FSM, so I'm at peace with my loose use of the term.

I really started to use FSM's with great frequency while working on developing hardware with FPGA's. Everything from reset modules to FIFO and SD/DD RAM controllers and more benefit from FSM's. They greatly reduce complexity and allow you to express very complex logic in code that can actually be followed and understood. For some reason FSM's feel more "at home" in hardware rather than software for me (although I use them extensively in software as well). There's something about looking at the definition of an FSM's states and realizing that it's a bunch of single bits (flip-flops) that makes it real:

    // Fictitious example
    parameter STATE_RESET      = 5'b00001;
    parameter STATE_STOP       = 5'b00010;
    parameter STATE_FORWARD    = 5'b00100;
    parameter STATE_TURN_RIGHT = 5'b01000;
    parameter STATE_TURN_LEFT  = 5'b10000;
Later, when you code the FSM --and if you've been doing this for a while-- you can look at the code and almost literally see the flip-flops and latches forming the structure:

    always @ (posedge SOME_CLK) begin
        case(state)
            STATE_RESET: begin
                // Do something in this state
            end
            STATE_STOP: begin
                // Do something in this state
            end
            STATE_FORWARD: begin
                // Do something in this state
            end
            STATE_TURN_RIGHT: begin
                // Do something in this state
            end
            STATE_TURN_LEFT: begin
                // Do something in this state
            end
        endcase
    end
It's really cool. Geeking-out just thinking about it.

In the software front, everything from communications packet processors to email address validation and menu processors benefit from using FSM's. I like the application of FSM's to web app controllers where you can encode a lot of knowledge and sophistication into your control module and not end-up with a huge rats-nest of procedural code that is nearly impossible to understand and maintain.

I have to admit being guilty of "thinking like an FSM" at times to the extent that I see the opportunity to use them in almost every project, hardware or software.

Again, today I could not imagine designing hardware or writing software without FSM's.

Re: What is the enlightenment I'm supposed to attain after studying finite automata?

#52

"Aha! moment". Well, I have to say that, for me, this came with the practice, not the study, of FSM's and, in particular Turing Machines. I could not imagine a world without them. Look around you. While I studied FA formally it never really clicked until years later. Sometimes you don't really learn until you are holding a cat by the tail. I should say that I've always called them "FSM" when, in most cases, in realit…

What's the logic behind defining your states as bitflags instead of enums? I would typically write STATE_RESET = 0, STATE_STOP = 1, STATE_FORWARD = 2, STATE_TURN_RIGHT = 3, STATE_TURN_LEFT = 4...

Unless, of course, you're doing an NFA and have a reason to superimpose states (i.e. be in STATE_TURN_RIGHT and STATE_TURN_LEFT simultaneously).

Re: What is the enlightenment I'm supposed to attain after studying finite automata?

#54
post #52

"Aha! moment". Well, I have to say that, for me, this came with the practice, not the study, of FSM's and, in particular Turing Machines. I could not imagine a world without them. Look around you. While I studied FA formally it never really clicked until years later. Sometimes you don't really learn until you are holding a cat by the tail. I should say that I've always called them "FSM" when, in most cases, in realit…

What's the logic behind defining your states as bitflags instead of enums? I would typically write STATE_RESET = 0, STATE_STOP = 1, STATE_FORWARD = 2, STATE_TURN_RIGHT = 3, STATE_TURN_LEFT = 4... Unless, of course, you're doing an NFA and have a reason to superimpose states (i.e. be in STATE_TURN_RIGHT and STATE_TURN_LEFT simultaneously).

Ah, remember, this is hardware and, this is Verilog, not C.

What you are looking at is called "one-hot" encoding.

If you enumerate your states you are asking Verilog to infer a register to hold your states. This also means that you have to have additional (slow) combinatorial logic to identify which state you are in.

With one-hot encoding each state is represented by a single and discrete flip-flop (FF) and no combinatorial logic is required. If you have 33 states the state machine has 33 FF's and only one of them can be "hot" or set to "1" at any given time.

One-hot encoding is faster, and offer a lot of other advantages (ease of debugging, ease of optimization, timing closure, etc.).

Here's a good resource:

http://www.xilinx.com/txpatches/pub/documentation/xactstep6/...

Go directly to Appendix A.

Re: What is the enlightenment I'm supposed to attain after studying finite automata?

#55
post #7

Earlier quoted context omitted.

Consider me extremely jealous that you are studying under Prof. Aho, he's one of the greats. Statemachines are an incredibly useful tool. I've spent the better part of last year untangling a huge pile of code and the solution in the end was to split it all up into statemachines that communicate with each other using simple synchronous messages. That and that alone made the problem tractable. (Tractable to me, that is…

Would love to get your recommendations for good reading on state machines, please. Getting curation from a someone who understands them well and uses them in practice often would be fantastic.

This book[1] seems to really help people (don't be put off by the title):

Practical UML Statecharts in C/C++: Event-Driven Programming for Embedded Systems

In particular, it discusses the main implementation strategies and why you'd want to use them. Also, the specific codebase described in the book is excellent. I use it all the time in implementing server software.

[1] http://www.amazon.com/Practical-UML-Statecharts-Second-Event...

Re: What is the enlightenment I'm supposed to attain after studying finite automata?

#56

Earlier quoted context omitted.

Hm, I never thought there would be anybody interested in this stuff so it's all over the place. Basically what I've done is to model state machines using a bunch of C macros, added a simple event system to drive the state transitions (receipt of message translates into an event) which allows you to run any number of statemachines in parallel inside a single C thread. The events are prioritized to make sure that urgen…

It sounds like we've had similar experiences. I converted a handful of ad hoc "state machines" in a telephony system to FA. The original code was loaded with exceptions and special cases (and bugs). Converting to FA required detailed analysis of the existing system to extract the distinct events and states. I used a tool I wrote [1] to generate C state tables and an event loop from a description of the states+events.…

Indeed, it really sounds like a very similar experience, up to and including the code generation.

One more thing I ended up adding (that isn't on your list above) is a true string type to the 'C' language (backwards compatible, works with pre-defined strings as well), including garbage collection. I found that in order to get the right level of abstraction the C string type was simply inadequate, it kept forcing things into the foreground that should not even be visible at that level.

The one thing still missing is copy-on-write for string duplication, but that's a pretty tricky thing to implement in a thread safe and portable way. This would give a huge performance boost, but at the moment that would be just another case of premature optimization.

Re: What is the enlightenment I'm supposed to attain after studying finite automata?

#57

Earlier quoted context omitted.

Read the theory again. Finite automata are quite weak.

Where is the infinite tape? Finite automata are as capable, in the real world, as Turing-like machines, because it is impossible to fabricate a machine with infinite storage.

Turing machines have infinite storage by definition, no such machine has ever been built (we're using approximations), real world restrictions do not apply to imaginary constructs.

If a Turing machine were not defined that way then you'd have to set some upper limit to the size of the tape and that in turn would have odd implications for what would be considered 'computable'. By making the tape infinite by definition you get much more meaningful answers about what is in principle computable and what is not.

Re: What is the enlightenment I'm supposed to attain after studying finite automata?

#58
post #10

Apparently if you understand finite automata, you'll be more enlightened than Stack Overflow moderators. My bitter example for this is http://stackoverflow.com/questions/11314077/algorithm-for-ex... which question was deleted by moderators, then undeleted by HN members. See http://meta.stackoverflow.com/questions/138678/can-we-please... for the discussion on meta about the question, and http://news.ycombinator.com/it…

I find there's no point in participating in SO beyond a certain level of ability. If you ask a truly difficult question, the odds of getting an answer from someone who knows what they are talking about is not very good. Meanwhile, any sort of discussion question gets closed. The remainder is basic to intermediate questions that get answered with "this is a duplicate of X".

Yes,

It seems like both Wikipedia and Stack Overflow are done, at least approximately. It is just that Wikipedia has at least some structures that have adapted to being done while it seem that SO framework never considered the possibility of the "frontier of general questions" being settled.

Re: What is the enlightenment I'm supposed to attain after studying finite automata?

#59
post #52

Earlier quoted context omitted.

What's the logic behind defining your states as bitflags instead of enums? I would typically write STATE_RESET = 0, STATE_STOP = 1, STATE_FORWARD = 2, STATE_TURN_RIGHT = 3, STATE_TURN_LEFT = 4... Unless, of course, you're doing an NFA and have a reason to superimpose states (i.e. be in STATE_TURN_RIGHT and STATE_TURN_LEFT simultaneously).

Ah, remember, this is hardware and, this is Verilog, not C. What you are looking at is called "one-hot" encoding. If you enumerate your states you are asking Verilog to infer a register to hold your states. This also means that you have to have additional (slow) combinatorial logic to identify which state you are in. With one-hot encoding each state is represented by a single and discrete flip-flop (FF) and no combin…

I've never actually done hardware design. I last looked at it a few years ago and it seemed really confusing.

What I really want is an FPGA on a board like the Raspberry Pi, where I can just plug in USB, network and power, copy-paste some code from a tutorial, have it run, and start learning HDL from that point.

I want to be able to develop an FPGA program that can maybe do a specific bitwise computation faster than a CPU or GPU (like SHA hashes for bitcoin mining, but this is just one example) and send inputs and outputs back and forth with a regular computer. Without also having to design and solder my own circuit board in order to do it.

It seems, though, like as soon as you say the word "FPGA," people automatically assume you have a ton of experience and equipment for building your own circuit boards. Which I really don't. And learning this on the same project learning HDL seems like it would really make things kinda hard. "It doesn't work" could mean anything from a solder blob shorting two wires, to some screw-up earlier in the process having destroyed a component and now it'll never work even if you do the rest of it correctly (and you have to increase your budget to replace the now-fried chip), to a failure to understand something about HDL, to some electromagnetic bug in the design of the circuit (like "you need a capacitor here, you need an inductor there, your resistor is 100x too big or too small")...a bug in the HDL of course...or it could be a bug in the USB interface hardware/software I made on the board...or in the custom USB driver I wrote (I've also never actually written a hardware driver before) running on the general-purpose computer...In short, it seems like, to get started teaching yourself hardware design, you have to have approximately one university degree worth of hardware design knowledge already.

Learning HDL, I want to be able to eliminate all of the above sources of error but "if it doesn't work, it must be a bug in my HDL code."

Bootstrapping software programming through self-learning is easy enough that probably at least tens of thousands of middle and high school kids do it on their own every year (I was one of them). Bootstrapping GPU programming is a little harder but still seems doable (I haven't actually done more than dabbling, though). Bootstrapping FPGA should be just as easy, but it isn't.

I haven't been able to find what hardware I should buy to do this, and I haven't even been able to figure out what search terms I should be using. It's been a few years since I looked, though, so maybe I should look again.

Re: What is the enlightenment I'm supposed to attain after studying finite automata?

#60

Earlier quoted context omitted.

I find there's no point in participating in SO beyond a certain level of ability. If you ask a truly difficult question, the odds of getting an answer from someone who knows what they are talking about is not very good. Meanwhile, any sort of discussion question gets closed. The remainder is basic to intermediate questions that get answered with "this is a duplicate of X".

Yes, It seems like both Wikipedia and Stack Overflow are done, at least approximately. It is just that Wikipedia has at least some structures that have adapted to being done while it seem that SO framework never considered the possibility of the "frontier of general questions" being settled.

Wikipedia hasn't adapted as well to being "done" as you might think. For instance, there's apparently at least one editor who just goes around Wikipedia deleting random sections from articles that appear to be uncited. He never adds anything, any contributions come from actual subject area experts who have to take time away from editing other articles to deal with the mess he leaves behind. The Wikipedia bureaucracy seems to think he's doing a great job though! And this is just something I stumbled across a couple of days ago by accident without following Wikipedia behind-the-scenes stuff; from what I've heard they have problems with favouring policy wonks over content contributers everywhere.
Post reply on HN