|
| 1 | + |
| 2 | +; state machine to DOT graph file format |
| 3 | + |
| 4 | +; REFERENCES: |
| 5 | +; https://oeis.org/A060843 |
| 6 | +; https://bbchallenge.org |
| 7 | +; https://turingmachine.io |
| 8 | +; https://morphett.info/turing/turing.html |
| 9 | +; https://groups.google.com/g/busy-beaver-discuss |
| 10 | +; https://turbotm.de/~heiner/BB/ |
| 11 | +; https://webusers.imj-prg.fr/~pascal.michel/ |
| 12 | +; https://googology.fandom.com/wiki/Googology_Wiki |
| 13 | +; |
| 14 | +; Rado (1962) |
| 15 | +; 1RB1LB_1LA1RH |
| 16 | +; |
| 17 | +; Lin and Rado (1965) |
| 18 | +; 1RB1RH_1LB0RC_1LC1LA |
| 19 | +; 1RB1RH_0RC1RB_1LC1LA |
| 20 | +; |
| 21 | +; Brady (1983) |
| 22 | +; 1RB1LB_1LA0LC_1RH1LD_1RD0RA |
| 23 | +; |
| 24 | +; Marxen and Buntrock (1990) |
| 25 | +; 1RB1LC_1RC1RB_1RD0LE_1LA1LD_1RH0LA |
| 26 | +; |
| 27 | +; BB(6,2) t15 2022 May 30, Pavel Kropitz |
| 28 | +; 1RB0LD_1RC0RF_1LC1LA_0LE1RH_1LF0RB_0RC0RE |
| 29 | +; |
| 30 | +; "Wythagoras" (2014) |
| 31 | +; 1RB---_1RC0LG_1LD1RB_1LF1LE_1RH1LF_1RG0LD_1LB0RF |
| 32 | +; |
| 33 | +; Lafitte and Papazian (2007) |
| 34 | +; 1RB2LB1RH_2LA2RB1LB |
| 35 | +; |
| 36 | +; Terry and Shawn Ligocki (2007) |
| 37 | +; 1RB2LA1LC_0LA2RB1LB_1RH1RA1RC |
| 38 | +; |
| 39 | +; Terry and Shawn Ligocki (2008) |
| 40 | +; 1RB1RH2RC_2LC2RD0LC_1RA2RB0LB_1LB0LD2RC |
| 41 | +; |
| 42 | +; Terry and Shawn Ligocki (2005) |
| 43 | +; 1RB2LA1RA1RA_1LB1LA3RB1RH |
| 44 | +; |
| 45 | +; Terry and Shawn Ligocki (2007) |
| 46 | +; 1RB1RA2LB3LA_2LA0LB1LC1LB_3RB3RC1RH1LC |
| 47 | +; |
| 48 | +; Terry and Shawn Ligocki (2007) |
| 49 | +; 1RB2LA1RA2LB2LA_0LA2RB3RB4RA1RH |
| 50 | +; |
| 51 | +; Terry and Shawn Ligocki (2008) |
| 52 | +; 1RB2LA1RH5LB5LA4LB_1LA4RB3RB5LB1LB4RA |
| 53 | +; |
| 54 | +; 15-state 2-symbol Turing machine M(15,2) that halts if and only if Erdős’ conjecture is false. |
| 55 | +; 0RB1RC_1RD0RA_0RA1LI_0RE0RF_0RD0LG_1RA1RA_1RE1RM_0LI1LJ_0LK1LH_0LH1LN_0LL1LM_0LK1LK_0LK1RF_1LO0RM_0RZ0RG |
| 56 | + |
| 57 | + |
| 58 | +match =PARAMS,PARAMS |
| 59 | + display 10,'expected usage examples:',10,\ |
| 60 | + 9, 'fasmg sm_dot.g -i "PARAMS equ 1RB0LD_1RC0RF_1LC1LA_0LE1RH_1LF0RB_0RC0RE"',10,\ |
| 61 | + 9, 'dot -Tpng sm_dot.dot -o "BB(6,2).png"',10 |
| 62 | + err 'PARAMS missing' |
| 63 | +else match Q=,S=,I,PARAMS |
| 64 | + file_gather <'states = ',`Q,' symbols = ',`S,' index = ',`I>,\ |
| 65 | + <IndexForm Q,S,I> |
| 66 | +else match any,PARAMS |
| 67 | + file_gather <`any>, <CompactLinearForm `any> |
| 68 | +end match |
| 69 | + |
| 70 | +macro file_gather note*, body* |
| 71 | + format binary as 'dot' |
| 72 | + db '###',10 |
| 73 | + db '### generated file',10 |
| 74 | + db '### ',note,10 |
| 75 | + db '###',10 |
| 76 | + db 'digraph statemachine {',10 |
| 77 | + db ' rankdir=LR;',10 |
| 78 | + db ' node [shape = circle fixedsize=true fillcolor=lightgray style=filled];',10 |
| 79 | + body |
| 80 | + db ' Halt [shape = doublecircle];',10 |
| 81 | + db '}',10 |
| 82 | +end macro |
| 83 | + |
| 84 | +macro IndexForm states*,symbols*,index* |
| 85 | + local reduce,state,value,target,write,direction,attributes |
| 86 | + reduce = index |
| 87 | + |
| 88 | + struc StateName I* |
| 89 | + local result,chars,i |
| 90 | + i = I mod (states+1) |
| 91 | + if i = states |
| 92 | + . = 'Halt' |
| 93 | + else |
| 94 | + if I eqtype 0 |
| 95 | + else |
| 96 | + I = I / states |
| 97 | + end if |
| 98 | + result = 0 |
| 99 | + chars = 0 |
| 100 | + while 1 |
| 101 | + result = (result shl 8)+'A'+(i mod 26) |
| 102 | + chars = chars + 1 |
| 103 | + i = i/26 |
| 104 | + if i = 0 |
| 105 | + break |
| 106 | + end if |
| 107 | + end while |
| 108 | + . = string (result bswap chars) |
| 109 | + end if |
| 110 | + end struc |
| 111 | + |
| 112 | + struc SymbolName I* |
| 113 | + repeat 1, n:I mod symbols |
| 114 | + . = `n |
| 115 | + end repeat |
| 116 | + if I eqtype 0 |
| 117 | + else |
| 118 | + I = I / symbols |
| 119 | + end if |
| 120 | + end struc |
| 121 | + |
| 122 | + repeat states |
| 123 | + state StateName %-1 |
| 124 | + ; NOTE: this ordering creates a unique decode of index: |
| 125 | + repeat symbols |
| 126 | + value SymbolName %-1 |
| 127 | + |
| 128 | + target StateName reduce |
| 129 | + write SymbolName reduce |
| 130 | + if reduce and 1 |
| 131 | + direction = 'L' |
| 132 | + attributes reequ '' |
| 133 | + else |
| 134 | + direction = 'R' |
| 135 | + attributes reequ ' color = "red"' |
| 136 | + end if |
| 137 | + reduce = reduce shr 1 |
| 138 | + if symbols = 2 ; decorate edges based on binary tape writing |
| 139 | + if write = '0' |
| 140 | + attributes reequ attributes,' arrowhead = onormal' |
| 141 | + end if |
| 142 | + end if |
| 143 | + |
| 144 | + db ' ',state,' -> ',target,\ |
| 145 | + ' [ label = "',value,'; ',write,', ',direction,'"',\ |
| 146 | + attributes,' ];',10 |
| 147 | + end repeat ; symbols |
| 148 | + end repeat ; states |
| 149 | + purge StateName,SymbolName |
| 150 | +end macro |
| 151 | + |
| 152 | +; WARNING: assume valid input: compact linearized transition matrix |
| 153 | +; see: https://www.sligocki.com/2022/10/09/standard-tm-format.html |
| 154 | + |
| 155 | +macro CompactLinearForm matrix* |
| 156 | + local reduce,symbols,symbol,states |
| 157 | + states = 1 |
| 158 | + symbol = 0 |
| 159 | + reduce = 0+matrix |
| 160 | + ; precompute so halt state is known |
| 161 | + while reduce > 0 |
| 162 | + if reduce and 0xFF = '_' |
| 163 | + reduce = reduce shr 8 |
| 164 | + ; next row of transition table |
| 165 | + if ~ definite symbols |
| 166 | + symbols := symbol |
| 167 | + else if symbols <> symbol |
| 168 | + err 'invalid compact linearized form' |
| 169 | + end if |
| 170 | + states = states + 1 |
| 171 | + symbol = 0 |
| 172 | + else |
| 173 | + transitions =: reduce and 0xFF_FF_FF |
| 174 | + reduce = reduce shr 24 |
| 175 | + symbol = symbol + 1 |
| 176 | + end if |
| 177 | + end while |
| 178 | + state = 'A' |
| 179 | + symbol = 0 |
| 180 | + irpv transition, transitions |
| 181 | + if transition = '---' ; undefined transition |
| 182 | + else |
| 183 | + target = transition shr 16 |
| 184 | + value = symbol + '0' |
| 185 | + write = 0xFF and transition |
| 186 | + direction = 0xFF and (transition shr 8) |
| 187 | + if target > states + 'A' - 1 |
| 188 | + target = 'Halt' |
| 189 | + end if |
| 190 | + if direction = 'R' |
| 191 | + attributes reequ ' color = "red"' |
| 192 | + else |
| 193 | + attributes reequ '' |
| 194 | + end if |
| 195 | + if write = '0' |
| 196 | + attributes reequ attributes,' arrowhead = onormal' |
| 197 | + end if |
| 198 | + db ' ',state,' -> ',target,\ |
| 199 | + ' [ label = "',value,'; ',write,', ',direction,'"',\ |
| 200 | + attributes,' ];',10 |
| 201 | + end if |
| 202 | + symbol = symbol + 1 |
| 203 | + if symbol = symbols |
| 204 | + state = state + 1 |
| 205 | + symbol = 0 |
| 206 | + end if |
| 207 | + end irpv |
| 208 | +end macro |
0 commit comments