|
Make a tool to create languages...
|
|
|
|
|
I've already created a lot of tools for building compiler front-ends. I even developed my own language (actually a subset of C#) for reasons. This however, would be too complicated for the examples I intend to present.
Real programmers use butterflies
|
|
|
|
|
You said you were bored. Think greater! A program for generating any language! An 'inverse-parser' if you will! It could spit out anything, and each syntax could take experts hundreds of years to decipher (or you could make that your next project!)
|
|
|
|
|
I'll assume you mean finite state machines...
honey the codewitch wrote: I don't want to lead with something contrived
They're all contrived.
I dunno, but now you have me wondering how close to a finite state machine my JSON reader is.
|
|
|
|
|
Heh @ implementing non-finite state machines inside an LBA finite state machine AKA a computer.
I don't know if they're all contrived. I bet your JSON reader uses a state machine if it chunks (rather than reading the entire JSON stream at once) - mine is - I wrote one too because I wanted something fast for bulk processing.
Real programmers use butterflies
|
|
|
|
|
Most of the FSMs I've written were for low level communications protocols (think HDLC/X.25, SDLC/SNA). Never did implement the new-fangled TCP/IP.
One interesting side-effect of implementing some of the older ones (Bisync flavours, anyone?) was proving that the protocols as documented were incomplete. They needed a catch-all state "Human intervention required".
Maybe a toy poll/response protocol? Two interacting FSMs, one for master, one for slave.
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
That's an interesting idea. I'll consider it. It also made me think of another idea involving an asynchronous implementation of an HTTP request/response cycle.
Real programmers use butterflies
|
|
|
|
|
There are quite a few real-world examples if you think in overview what a FSM requires:
1) Inputs that brought the machine to this state
2) The current state of the machine and the outputs that arriving there generated
3) The new state the machine will have as a response to new inputs
So: navigating a vehicle on an small island could be modeled as an FSM. A drinks vending machine. Traffic lights (sooooo simple, very small set of states). Voting systems (could get humorous).
Good luck!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
As Griff already suggested, a vending machine would probably fit the bill.
|
|
|
|
|
There was a QA a while ago about 'decoding' some data - the eventual solution was proposed using a RegEx - I was in two minds about it, I personally would have used a state-machine, or maybe it was a 'borderline case' (as opposed to me, 'nut case')
|
|
|
|
|
honey the codewitch wrote: a simple example of a state machine that is real world at all
Do you own a car ? Think about any car part that is based on electronics or electrical actors -> It runs a state machine. Embedded is full of state machines. Actually, state machines are the AI of embedded world.
|
|
|
|
|
An example that could grow into something non-trivial, but that everyone understands, is a set of four traffic lights.
|
|
|
|
|
If you can't think of a real world example then maybe it's not something people particularly need to know about?
|
|
|
|
|
When I hear "state machine" I think of games.
Maybe you could write something about that?
Also, don't know if I used it right, but I once used a state machine for order processing.
The order could go from "ordered" to "paid" and "paid" to shipped, but never "ordered" to "shipped", or something like that.
It was a bit more complicated than that, but it's been a while so I don't remember, but it was something like that.
|
|
|
|
|
Unity uses FSM to manage AI and general game play flow.
|
|
|
|
|
How about an elevator.
Or a set of elevators in a building, programmed to most efficiently seek a state of best efficiency to service the next floor request when at rest.
...er *puff* yeah... something like that...
|
|
|
|
|
That takes me back a LONG time to my student days. It was an assignment I remember, writing something exactly like this in assembly on some 8 bit processor (6809 rings a vague bell). Can't remember how, but our programs could then be loaded into a board based computer connected to a miniature elevator for everyone to have a go at defeating other people's software.
The tricky bit was deciding when to accept and when to ignore a request from a button push on a given floor or if e.g. someone presses a button to request a lift to take the up, then gets in and presses the button for a lower floor. Decisions would then be based on all the "in-lift" requests plus the "out of lift" requests plus the current direction of travel.
Surprising fun and games for such a simple system.
|
|
|
|
|
Yep - needs rules real quick. Always going to the closest floor request would not be a good idea, e.g.
|
|
|
|
|
Speaking of elevators, I've always wanted to ask if repeated button pushes get precedence. It would explain why when I'm standing there and the button is already lit, others feel the need to press it again, sometimes repeatedly. Do they know something I don't?
"Go forth into the source" - Neal Morse
|
|
|
|
|
That obviously depends on the software.
Many years ago, I was working on a timesharing minimachine (16 bits CPU, half a megabyte of RAM) serving 20 simultanous users, so the CPU was quite heavily loaded. Even a moderate size student program took minutes to compile.
Some guys (students, of course) discovered that when you hit a key on the keyboard, the OS would raise the priority of that user process for a brief period. This was to give good interactive response. If the process was still in the running state after a couple of seconds, it was probably doing some lengthy processing, such as a compilation, and given lower priority that those processes in an active user dialog.
What these students discovered: Priority was raised on keyboard interrups even when the process was not waiting for keyboard input. If they had a lengthy compilation, they could rest a finger on the space bar for a continous stream of keyboard interrupt, so that their compilation was done at "interactive" priority, pushing the jobs of the 19 other users aside, in effect taking all of the capacity of the CPU. This worked as long as you were the only one knowing this trick. Of course it leaked out, and soon all 20 users ran their compilations competing for the CPU at interactive priority, no different from when they had been competing a "background" priority earlier. But if there were one user actually trying to get some interactive work done, it was almost impossible, with heavy compilations running at the same high priority.
We reported this problem to the vendor, and in the next release of the OS, the priority was raised only if the process was actually waiting for keyboard input, not when it was busy compiling.
The students frequently commented that the OS textbook was wrong: It stated that timesharing makes it appear as if you have the entire machine to yourself (that was a common claim in OS textbook of that age). The students argued that truth is exactly the opposite: It makes you realize that you do not have the machine to yourself! The situation with you as the only one trying to edit a source file with 19 compile jobs artificially raised to the same priority is an extreme example of that.
For the elevator: Pressing again will certainly not speed up the motors pulling the carriage. If it were to have any effect, it would be because the control logic said something like: "Sure, I was on my way up to pick up that person on floor 12, but those guys down at floor 2 are nagging me so much that I'll turn around and leave that 12th floor guy to himself for a while and rather take care of those naggers".
I have never seen, or hear of, any elevator with such a behaviour. There are reasons why textbooks call one disk scheduling method the "elevator algorithm": Your arm movement (or elevator carriage) sticks to the same direction of movement as before, processing any request to the positions it passes by, until there are no more requests in that direction. Then it turns around and processes all the request up to the opposite outermost requested position. The disk arm (or elevator carriage) swings back and forth like a pendlum, serving any request to the positions it passes (those are request that arrived since last time this position was passed).
A difference from the pendlum is that the disk arm / elevator carriage does not necessarily go to the very end of their range. If there are no requests beyond floor 12, the elevator turns around there. If, a split second after the elevator has turned around, someone at floor 14 pushes the button, they will have to wait until the carriage has gone all the way to the bottom and returning back up, serving requests both on the way down and the way up. No elevator will on its way from floor 12 to 11 stop and say "We better return to pick up this 14th floor guy before we go down".
The only cases where I have experienced an elevator on the way to my destination floor stopping to go the other way is when some elevator serviceman uses his special override key to force the carriage to a specific floor. That sets the entire elevator algorithm aside, forcing one single destination floor, dropping all other requests. That is an exceptional situation. In normal operation, an elevator runs the elevator algorithm, which will not be disabled by repeated keypresses.
|
|
|
|
|
Tim Deveaux wrote: How about an elevator.
I second that: if my memory serves me well, Knuth used it as an example in TAOCP (The Art of Computer Programming) and he didn't use FSMs (I'll have to go review that).
If Knuth himself used it, it must be good.
Mircea
|
|
|
|
|
Surely elevators don't use state machines? The basics could be done with a state machine, but what about if multiple floors are selected from multiple people in the lift? What if I go from 2 to 20 and someone on 15 presses up? What if they press down? It's surely too complex?
|
|
|
|
|
Maybe simplify it - each 'floor' (like a cell on a Turing machine) has a signalled state - off, on wanting an up pickup, on wanting a down pickup, or on as a destination.
And the elevator is always moving through all floors, in one of the two directions.
If the elevator polls each floor state as it approaches it and reacts accordingly, it doesn't matter whether the signals are set inside or outside the elevator.
Lots of subtleties to add on of course (bloody feature creep) but...
|
|
|
|
|
Reading input devices like a mouse, keyboard, joystick - without a fancy event based framework is a good example.
Given that the hardware only gives you the state of pressed/not-pressed events like click, tap, double-click, etc. require and abstract state machine on top of the state of the physical buttons, and best of all most beginner programmers are going to understand the examples without needing any other domain knowledge.
|
|
|
|
|
Way back in the stone-age, I a state machine and a timer interrupt to implement a Serial port on a micro controller. Due to the efficiency of the state machine, I was able to handle 9600 baud in the background while the software handled its main task, probably driving a printer. I think that was state driven as well.
It’s amazing what you could do in lees than 4K ROM and 120 byes of Ram, including stack.
One of the big advantage of some state machine implementation is they take very little RAM.
"Time flies like an arrow. Fruit flies like a banana."
|
|
|
|