 Felly, ydych chi'n gweithio. First of all, can you all hear me? Good, okay, second question. Who here is a fan of trains and railways? Yep, and who here is a fan of computers? Yep, same number, roughly same number. And so how many of you have thought about if there's a connection between these two things? Well, if you have done, some hands here, great, great, great, then you are in the right place. So this is my talk, Riddles on Rails. My name is Dan Hagan and it's going to be a fairly fast-paced talk. So we're going to start off talking a little bit about safety. So safety for computers, safety for railways. And then it's going to get a little bit weird for a little while, but stick with it because at the end we're going to go and look at some fun puzzles and games. So just a little bit about me. So I design a wireless control system as a battery-powered system for model railways. And I've been coming to hacker camps now since 2011 or so. And it was kind of a surprise to me when I looked at the history of hacking and hacker culture that the original hackers, the people that actually called themselves hackers originally, were actually model railway enthusiasts. So you can read about it in Stephen Levy's book here. It's called The Tech Model Railway Club. It still exists at MIT. But they were really, really into using computers to control them all railways, basically. So let's talk, first of all, about how we do safety, but not just safety for railways, but also for computers. And actually there's a very strong relationship between the two. So I want to take you back now to 1861. And imagine it's sort of a bank holiday weekend. And people have been to go and visit the pavilion. It's a very nice place in Brighton to visit. And so lots of people are going to get on their train, trains and go home near to London or wherever. And so just outside of Brighton, there is a tunnel. It's called the Clayton Tunnel. It's quite an ornate tunnel, as most Victorian sort of buildings are. And on this particular morning, there were three trains that were due to leave. And as it is on one of these busy mornings, they all were sort of very late to leave. And so they left within a very short succession. And at the time, they were using sort of time intervals to... You can see the timings of the departures. Now they're using time intervals to make sure that when a train is gone, don't send the next one until you've got enough time between it. So let's have a look at the tunnel itself. At the tunnel, you can't quite see all the way through the tunnel. So it has a slight kink at the end of it. So they have to have a signal at each end of the tunnel. And so they basically ensure entry and exit of the tunnel. And now at the entrance of the south side, as you can see here, there was a ball, basically. And it rose up to indicate that a train could proceed. And then it was dropped to indicate that... Well, once a train had gone past it, it was dropped automatically. So let's just reset that and see what happened on this particular incident. So our first train goes past. And then the signal on the south side signals to the north side and says the train is now in the tunnel. Nothing wrong with that. But then he notices something about the ball. So that ball hasn't dropped. So that's our first failure that's happened. A mechanical failure. So what he does then, he rushes out with his flag. And he's not quite quick enough because the next train is coming very, very shortly after. So he's not quite quick enough. He's not quite sure if that second train has seen that flag. But he's quick enough to actually stop the third train that comes along. And then he can... Well, he has a choice now. Does he send this third train into the tunnel? Or does he wait? So what he does then, he sends a message to the other signal and says, is the tunnel clear? Just at that moment, the first train that went into the tunnel exits the tunnel. Now, that north side signaler now tells the other signaler via a bell, a telegraph system. He says, yeah, yeah, the tunnel's clear. However, I think you can probably guess what was going to happen here, that second train is still in the tunnel and that second train knows that flag was there and he actually decides to reverse. That is also a failure that he should have thought about the consequences of that. So what happens is fairly obvious that the two trains collide, one reversing into the other. Actually, it's one of the most disastrous rail accidents actually in rail history, in UK rail history. Many three people actually died in that particular incident. The thing about it is there are lots and lots of different ways you could actually correct this and you can think about where the failure happened with this. So I'll leave that with you to kind of think about where you would correct the protocol that was going on there. Okay, so let's just have a look at a more modern way of doing signalling. Oh yeah, so there's a little link here for, if you're interested in more of this, there's lots of data on this particular site that tells you about other accidents and why they occurred and various things like that. Anyway, so let's think about in a station. So a signal box in a station can see the whole of the station and he or she can basically look at all the trains that are there and determine whether or not they're going to collide and switch the points appropriately. But when you get to a tunnel, the situation is different. You basically, as we've seen, you can't actually see through the tunnel and so you have to have a principle that you use to ensure that trains don't collide. So we've got two stations A and B and we use what's known as the absolute block system. So we absolutely make sure that there is only ever one train in that block section. Okay, so how do we do this? Well, the signal at station B basically has control over the line that precedes it and the signal at station A has to request permission for trains to come into that section. Okay, and how do they do that? Well, as we've seen, there's a telegraph system between them, so what we call a block instrument, as you can see here, and various sort of protocols for various bells that they use to say whether or not to request to come into a section and whether something has cleared the section and so on. Okay, so then what does the train driver, the engine driver see? So at the entrance to the section you've got a signal and it's actually called a starter for fairly obvious reasons and you also have another signal at the exit to the block called the home, so when you get home basically and we can see actually what they look like. Now notice something about the way that they are designed. So basically the arm is raised for the precede aspect but if it fails, for instance, then it will fail in the failsafe mode. So that's what's called upper quadrant. And the signal knows that the train has left the section because you can see here at the end of the train there is a red light that's really important. If the train breaks halfway through, then if it doesn't seal that red light, then he knows not to allow another train into that section. Okay, so we've gone and seen how the railway operates and ensures safe operation from collision. How can we apply this to software? So here are two processes. They're basically running infinite loops and they have two different sections. So they have a critical section and they have a non-critical section and then protecting the entry and exit from that critical section are two primitives called signal and weight. So very, very similar to the sort of block section that we've seen with the railway. Now this was actually designed by a Dutch computer scientist called Edgar Dijkstra and he actually used the names he originally used weren't signal and weight but P and V and they, I'm told, stand for equivalent concepts directly taken from the railway analogy, essentially. So a semifor, in this case, similar to the signal, allows entry and exit to the critical section, but it is just an integer. So basically at the point the integer becomes zero, another process can take, gain entry to that critical section and this basically, the critical section means that only one process is executing that, which means if you've got a shared memory region, that only one process can read or write to it, so that ensures consistency. So you can imagine if somebody's just read from a memory area and they try to read from it immediately, something else tries to read from it immediately, what will happen is that they will have inconsistent views of that data. So that's semifor's. Now let's look at another kind of safety issue. What we were discussing there was kind of safety from a sort of policy level, but this is more about sort of what does the kind of human operator do in the system. So the trolley problem basically. So imagine there's a trolley that's barreling down the tracks and it's out of control. And then there's some people on the tracks now. It's not quite explained why they're on the tracks, but bear with it for now. So you then have control of a point. So it's a fairly easy decision to make. You pull that point except, of course, there's always a catch. There's somebody else on the other track. Now there's only one person on the other track. So the decision process here as well, what do you do? Do you sort of say, well if I wasn't here and that train was just coming down that track anyway, well, you know, those five people would be killed, right? So if I wasn't here, that would happen anyway. So if I just leave the handle, it's the same thing. Or is it? And then the other thing is, well if I pull that handle, I've definitely made that conscious decision that one person is going to be killed and that was my action that's done that. So there's no right answer to this, but it's one of these problems which, in sort of immoral ethics and philosophy and the rest of it, has kind of created a lot of interest and debate basically. So I was kind of curious, actually, this idea of people on tracks and it being this sort of trope that you get. I mean you think of silent movies, but actually it kind of goes back a lot further. It actually goes back to a play by a guy called Augustine Daley, sort of a Victorian melodrama. And the trolley problem itself is sort of a 60s problem, although you'll see it crop up in sort of internet memes. There's lots of quantum mechanics based ones anyway. And so basically there's lots of variations on this idea. Where does it get application? Well, autonomous vehicles. You think about AI in autonomous vehicles, right? So at some stage that vehicle has got to make a decision. Do I swerf to not hit a group of pedestrians, but what then happens to the passengers in my car? So this does kind of have applications. Of course you could of course put the vehicle on some rails and make it a bit safer that way, but anyway we'll use AI. That's the way to do it. So now it's going to get a little bit weird. So we're going to talk about time. So look at this clock here. What can you notice about it? It has two minute hands, minute arms. So this clock is off the side of the corn exchange in Bristol. And these two minute arms are there because one is for London time and one is for Bristol's time. Now this is a time where all over the country everyone was setting their clock by the sun basically. So it was completely inconsistent timing amongst all the different locations. So it was complete chaos basically. You have things like people would make their watch run a bit faster when they were going from east to west versus, or slower, I forgot the right way around, one of those way around anyway. So they're correct for these timings and you can imagine all sorts of things like accidents and people just missing trains. So they had to figure out a way of getting around this. So what they came up with was a time distribution system known as the Chronifer. Basically this ran along the telegraph wires. So you're using the pre-existing telegraph wires for signaling and so forth. And it basically became known as railway time as a result of this actually, but it was used for more than just railway timings. So this is a sort of schematic of the timing of the Chronifer system itself. So you can see on the top left hand side you can see the incoming signal coming from Greenwich. This is the Greenwich mean time. And then there are five signals going out to various locations and you can see the relays and the batteries at the bottom. So this time signal was sent at 10am every day. The telegraph wires were used for normal traffic but they had to clear that line at 10am every day to make sure that they got this time signature coming through. I'm reliably informed even up to the 60s this was still being used. And so basically they'd listen out for this time signature and then they'd relay this time signature out to many, many other locations. So distributing it out. Now we actually have a very similar situation actually in computing. So we've got something known as the network time protocol and you can see the sort of tree-like structure so you can see it being distributed from a sort of essentially a sort of central source so you have multiple what they call stratum, zero clocks and various stratum, one, two and three and so on. So in the UK for instance we've got the national physical of our tree and on camp here we actually have a couple of time servers as well. So basically you can see very similar kind of principles being used here. So how do we actually use this to synchronise our clocks? Well basically time stamp messages will be sent and received between two different servers and you would use that to determine various things like offset and drift and so forth and something known as Missoula's algorithm or slight variation on that is used to combine time signatures from multiple clocks so basically to get rid of outliers as it were. Okay so that was sort of the 19th century view of time which we kind of saw used today actually but back in the early 20th century chap called Einstein came along and he thought a lot about time, time was kind of important to him so he has various thought experiments and one of them involves a train he envisaged somebody on a train with a stone or a pebble or whatever and they throw that pebble and it hits the embankment. Now to the person on the train the pebble is basically I mean they don't throw it but they let go of it so through gravity the pebble is just going vertically down from their perspective okay so to the person on the embankment though they're seeing as a parabola so he asked a question basically which is the one true path this pebble is or the stone is taking and so he thinks this through and basically his conclusion is there isn't actually a one true path so there is a path for each of these what's known as these reference frames so if a reference frame basically has so I'll move on to the next there we go so basically every reference frame the laws of physics within each reference frame are the same so there's no special reference frame so a lot of people look at this problem and they think well obviously the bank is bolted to the earth it's the one true reference frame and everything else is relative to that but his conclusion is no that all these different reference frames are equally their own particular reference frame and one particular part of this is that the speed of light in each reference frame is the same so we'll need that in just a second so how does he use this then to think about time well he imagines lightning striking either end of the train he actually says hitting the rails but it's the same thing hitting the train effectively so he's asking the question are these events simultaneous do these events occur at exactly the same time and in particular do they occur at the same time for the person stood on the bank the embankment as for the person stood on the train so basically let's think about the personal embankment so let's imagine them midway between the two locations so we know that the speed of light is the same in that reference frame as any other reference frame so he's in the middle of the two lightning strikes and the light clearly is going to take the same amount of time to reach her basically because it's got the same distance to travel so for her if she sees those as simultaneous if they really are simultaneous in her reference frame then she's basically going to arrive at the same time because they've had to traverse the same distance so basically we can use this principle for a given reference frame to say all these clocks within that reference frame are all synchronised we can look at the position of the hand and we can say all these hands are pointing to the same position now let's think about the other reference frame the frame that's within the train that's going along so basically if you consider the midpoint along the train so that basically when the lightning strikes occur that is the same as the midpoint for the embankment frame but crucially it's moving along it's moving relative to the embankment so actually the light that's coming along the train from behind and the light that's coming from in front will take different amounts of time to reach that person because by the time that person has got further on the track the time that the light has reached them they've got further on the track and there's less time it takes for that light to get to them in front of them than it does behind them so there's a really profound conclusion for this which is that actually to say that events are simultaneous you cannot say that unless you say they are simultaneous in this frame of reference that's a really really crucial conclusion from what's known as special relativity so you might think well this is very esoteric it doesn't really apply to everyday things like speed of light and the rest of it well actually GPS if you didn't correct for special and general relativity within a day you basically that your position would drift so far that it would be next to useless so that's an everyday usage of these principles but even on a network we've got a differential time through the network delays in buffers and different line speeds and so forth so within a distributed system different events occur at different times and you can't really have a consistent view of timing and also you could have someone that purposefully says well this thing occurred at that time or somebody along the way can maliciously change timestamps and so forth so when you start thinking about it time in distributed systems on the network is actually a really slippery concept to deal with anyway so people have looked at this and one of the concepts they've come up with is what's known as logical clocks or lamport timestamps or lamport clocks so basically we've got three processes here and we look at the middle one here we've got various events that occur within a process and it's fairly easy to work out that they'll occur in time order there's no problem there now when we send a message between two different processes I think we'll all agree that a message is sent before it's received so then we've got some time ordering there so what we have then is what's known as a partial order so there are some events for instance these two here which we can't actually say well that one occurred before that one or the same time as that one and so forth so it's a slightly weaker notion of ordering events in a system so variations on this theme are what's in use at the moment for timing within distributed systems so that was the weird stuff that was out of the way now I'm going to skip one slide there and I'm going to talk here about I will come back to that other slide by the way I'm going to talk about shunting yards and the basically there's a kind of puzzle that's known as a shunting yard puzzle there's lots and lots of these this is one example, it's kind of the most famous example so in this one you have basically you've got a shunter and then you have eight wagons, coaches they're given numbers and you have to well basically you've got different capacities on the two sidings so the three sidings and on the spur, so the top two have three, the bottom one has five and the spur has four but one note is for the shunter shunt just using the rails that you've got there basically you've got to shunt the wagons so you get the wagons in the correct order one to five so it's a fun kind of puzzle and there's lots of variations on this thing but you might notice a kind of similarity here with a puzzle that's kind of very often used in early programming teaching and things like that it's not quite the same puzzle but it's a very similar puzzle and as I said there's all sorts of other variations on this thing so we can also think about shunting yards in terms of basically grammars so to generate a language you need a grammar to say how you compose the various elements that are the parts that make up words in that language so normally you'd use something like Bakker's Gnar form but this type of diagram is actually fairly used to be more commonly used but not so much these days for instance Pascal used to use it as a way of defining their language but this is exactly the context free language that you get with BNF form so how do we use it so let me give you an example this one comes from the JSON specification so I'm sure a lot of you aren't fairly familiar with this now and again so one example of this so these are defining floating point numbers so I've got one here which I can't quite remember but anyway that number there whichever one is I think 0-0.24 has the following you trace it through this path basically to get to find that number in that grammar so let me give you a slightly more complicated example that takes a different path through that so that's how we define that particular number so let's think about now I want to go in two different directions so I can define the strings in my grammar in my language using the grammar let's take a string and I want to decompose it and actually evaluate it so first of all let's define a grammar that we can use to do that that operation so we're going to do something based on single digit arithmetic expressions you'll notice this one does something that most shunting yards can't do which is it's recursive so you can take this entire diagram and place it in each of the two expressions either side of the operator in the middle so let's give you an example from this so this example I've put the numbers in order that will be important a little bit later on so this example we can't directly evaluate this it's not easy for it to do so but there is a form known as reverse polish notation postfix notation the one at the top is actually infix notation and this is actually an easier form to evaluate because you can basically take the numbers and the operators in the order that they come so for instance we'll have a stack and we'll push our first value on the stack and then we'll take our next value push that on to the stack and then we'll take our operator and then we'll combine the two top values on the stack to create the new value and then push that back on to the stack and so then we can do this many times over and then we've evaluated the whole expression and in this case it's two so we've got a problem here that we've got an expression in infix notation and we want to need to get it into postfix notation so in order to do that we use something up so we need a way of doing that and notice one thing first of all the numbers when we convert between them are in the same order but sometimes the operators are in a different order so we're going to do some sort of shunting to get them into the right order so how do we do that well again this comes from Dykstra and basically his mechanism of doing this basically uses a shunting yard so let me quickly go through this so we shunt our operators across to the siding and then by doing so by doing so we can get it into the right format and then just the final thing about Dykstra he is very famous for this thing called the Dykstra's algorithm it turns out that in the UK people working on the UK rail network back in the 50s came up with the algorithm just two or three years prior to him and that is a very fascinating kind of story and I have looked more into that but certainly a lot of these things crop up again and again now I think I've run out of time there were a couple of videos here but I shall how I did now I think yeah are we first one we'll do this one here and that so this is a little puzzle here so you might notice that the tracks in this particular orientation they look like they are different lengths but I can show you they are exactly the same length so go and get some track and try and figure out why that's the case but I think that's the end ok thank you much