fix issue with SECD machine post

This commit is contained in:
Rachel Lambda Samuelsson 2022-01-27 20:06:52 +01:00
parent a21faac1e0
commit f644309ae0
2 changed files with 32 additions and 18 deletions

View File

@ -3,9 +3,12 @@ layout: post
title: "A first look at SECD machines"
---
The SECD machine is a virtual machine designed to evaluate lambda expressions. It's purpose is to be a more natural goal for compilers than assembly while maintaining reasonable execution speed. In this blog post you will get a quick intro to SECD machines and an overview of a simple implementation.
<!--more-->
{% katexmm %}
## The SECD Machine
SECD stands for Stack Environment Control Dump, all of which but the environment are stacks in the SECD machine. The machine operates by reading instructions from the control stack which operate on itself and the other stacks. A lambda is implemented as its body coupled with its environment. Application is done with the "apply" instruction which pops a lambda off the stack and adds the next element of the stack to the lambdas environment environment, binding the variable of the lambda. The previous stacks are then pushed onto the dump stack. When the lambda has been fully reduced the return instruction is used to save the top of the stack, the result of the reduction, and restores the stacks from the dump stack.
@ -33,14 +36,14 @@ In this section a SECD machine implemented in Haskell will be introduced bit by
### Data structures
The machine will only be able to recognise two types of values. Integers and Closures.
```
```haskell
data Val
= I Int
| Clo Int [Val] [Inst] -- number of arguments, environment, instructions
deriving (Eq, Show)
```
The machine comes with the following instruction set.
```
```haskell
data Inst
= Const Int
| Global Int
@ -78,6 +81,7 @@ All stacks grow from right to left, that is, the left most element is at the top
<table>
<tr>
<th align=center colspan="3">Before</th><th align=center colspan="3">After</th>
</tr>
<tr>
<th>Control</th><th>Env</th><th>Stack</th><th>Control</th><th>Env</th><th>Stack</th>
</tr>
@ -129,14 +133,14 @@ Reading this (or one of the previous lambda expressions) backwards you should be
This can now directly be translated into a list of instructions for the Haskell machine implementation.
```
```haskell
[ Const 0, Closure 1 4, Const 1, Local 0, Add, Ret, Dup
, Closure 3 6, Local 0, Local 1, App, Local 2, App, Ret
, App, App, App]
```
Which can then be evaluated in order to yield the normalized expression.
```
```haskell
λ eval [] [ Const 0, Closure 1 4, Const 1, Local 0, Add, Ret, Dup
, Closure 3 6, Local 0, Local 1, App, Local 2, App, Ret
, App, App, App]
@ -154,7 +158,7 @@ In the future I plan to create a specification for an instruction set which allo
The implementation itself is trivial and is little more than a Haskell translation of the instruction table above. It's ugly, it doesn't do error handling and is little more than a demonstration piece.
```
```haskell
module SECD where
data Inst
@ -201,6 +205,8 @@ eval globals insts = go (SEC [] [] insts globals)
Add -> let (x1:x2:st) = stack in go (SEC (vadd x1 x2:st) env xs global)
```
{% endkatexmm %}
## Licensing Information
Feel free to use any of the code or concepts here in your own projects! The code can be considered free of copyright and all notions of liability and/or warranty.

View File

@ -76,10 +76,18 @@ pre code {
display: block;
}
p, li {
p, li, th, td {
color: $text;
}
table, th, td {
border: 1px solid $text;
}
th, td {
padding: 5px;
}
.bigger {
font-size: 1.2em;
}