Skip to content

Commit

Permalink
Merge pull request #10 from jakewilliami/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
jakewilliami committed Oct 25, 2020
2 parents 953195f + 50a0c45 commit 4540a0d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 76 deletions.
47 changes: 8 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,27 @@ This is a minimal package for a pure Julia implementation of tools used in [Comp
julia> pair_tuple(5,7) # code pair of natural numbers as a natural number
83

julia> pair_tuple(5,7,20)
5439

julia> π(83, 2) # code a natural number into a 2-tuple
(5, 7)

julia> π(83)
julia> π(83) # code a natural number into a 2-tuple
(5, 7)

julia> π(83, 2, algebraic)
julia> π(83, 2, algebraic) # use algebraic method of depairing rather than search (much faster)
(5, 7)

julia> π(83, 3, algebraic) # code a natural number into a 3-tuple
(2, 0, 7)

julia> π(83, 2, 1) # code a natural number into a 2-tuple and get the the number in the tuple indexed by 1 (index starting from zero)
7

julia> cℤ(-10) # code integer as a natural number
19

julia> cℤ((-1,2))
julia> cℤ((-1,2)) # cℤ pairs the code of each
16

julia> cℤ(-1, 2)
(1, 4)

julia> cℤ⁻¹(19)
-10

julia> Sequence(121).length
1

julia> Sequence(972292871301644916468488152875266508938968846389326007980307063346008398713128885682044504108288931767348821063618087715644933567266540511345568504718733339523678538338052787779884557674350959673597803113281693069940562881722205193604550737455583875504348606989700013337656597740101535).instructions
(328, 4, 531, 4, 5, 0, 14)

julia> Sequence(121).instructions
(14,)

julia> Instruction(7).instruction
(1, 2)

julia> Instruction(14).instruction
(4, 0)

julia> Instruction(58).instruction
(3, (1, 2))

julia> Instruction((3, (1, 2))).I
58

Expand All @@ -75,20 +48,16 @@ julia> Programme(121).instructions
julia> show_programme(121)
0 halt

julia> show_programme(972292871301644916468488152875266508938968846389326007980307063346008398713128885682044504108288931767348821063618087715644933567266540511345568504718733339523678538338052787779884557674350959673597803113281693069940562881722205193604550737455583875504348606989700013337656597740101535)
0 if R1 = 0 goto 5
1 R1 := R1 - 1
2 if R1 = 0 goto 6
3 R1 := R1 - 1
4 goto 0
5 R0 := R0 + 1
6 halt

julia> run_goto_programme(363183787614755732766753446033240)
(1, 0)

julia> run_goto_programme(363183787614755732766753446033240, Register(0, 0, 0 ,0))
(1, 0, 0, 0)

julia> show_programme(rand(GoToProgramme), 3) # a reasonably small random programme with 3 lines
0 R3 := R3 + 1
1 if R0 = 0 goto 1
2 halt
```

[code-style-img]: https://img.shields.io/badge/code%20style-blue-4495d1.svg
Expand Down
12 changes: 1 addition & 11 deletions examples/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,6 @@ function programme_ui()

end

function test_random(d::Integer)
# random = abs(rand(Int)) + 121
random = rand(121:2000)

println("The programme coded by the number $random is shown in $d instructions as follows:\n")

show_programme(pair_tuple(d, pair_tuple(random, pair_tuple(4, 0))))
end


# helper
function binary_search(a::BigInt, b::BigInt) # useful for bug fixing
while a != b
Expand All @@ -110,4 +100,4 @@ function binary_search(a::BigInt, b::BigInt) # useful for bug fixing
return a
end

programme_ui()
# programme_ui()
6 changes: 5 additions & 1 deletion src/ComputabilityTheory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@

module ComputabilityTheory

include(joinpath(dirname(@__FILE__), "abstract_types.jl"))
include(joinpath(dirname(@__FILE__), "coding.jl"))
include(joinpath(dirname(@__FILE__), "machines.jl"))
include(joinpath(dirname(@__FILE__), "goto.jl"))

export Machine, TuringMachine, MachineComponent, Programme,
ProgrammeComponent

export , pair_tuple, algebraic, π, cℤ, cℤ⁻¹

export Tape, Left, Stay, Right, MachineState, Rule, TMProgramme,
run_turing_machine, RegisterMachine, run_goto_programme

export Sequence, Instruction, GoToProgramme, increment, decrement,
goto, ifzero_goto, halt, show_programme
goto, ifzero_goto, halt, show_programme, rand

end # end module
11 changes: 11 additions & 0 deletions src/abstract_types.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash
#=
exec julia --project="$(realpath $(dirname $0))" --color=yes --startup-file=no -e 'include(popfirst!(ARGS))' \
"${BASH_SOURCE[0]}" "$@"
=#

abstract type Machine end
abstract type TuringMachine <: Machine end
abstract type MachineComponent end
abstract type Programme end
abstract type ProgrammeCompoment end
44 changes: 25 additions & 19 deletions src/goto.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ It should also be noted that the sequence code for some base cases is defined as
- if d = 1, for a ∈ ℕ, the sequence code [a] for the sequence (a) of length 1, is <1, a>.
=#

include(joinpath(dirname(@__FILE__), "abstract_types.jl"))
include(joinpath(dirname(@__FILE__), "coding.jl"))

using Printf: @printf
Expand All @@ -33,7 +34,7 @@ const __goto_identifier = 2
const __ifzero_goto_identifier = 3
const __halt_identifier = (4, 0)

struct Instruction
struct Instruction <: ProgrammeCompoment
I::Integer
first::Integer
second::Integer
Expand Down Expand Up @@ -82,7 +83,7 @@ struct Instruction
Instruction(i::Integer, j::Integer...) = Instruction((i, j...))
end # end struct

struct Sequence
struct Sequence <: ProgrammeCompoment
q::Integer
seq_length::Integer
instructions::Tuple
Expand All @@ -97,30 +98,23 @@ struct Sequence
function Sequence(t::Tuple)
q = nothing
seq_length, instructions = t[1], t[2]

sequence_length_error = "The first number in your sequence should match the length of your sequence."
seq_length length(instructions) && throw(error("$(sequence_length_error)"))

if eltype(instructions) <: Integer
q = pair_tuple(t[1], t[2]...)
end

if eltype(instructions) <: Tuple
q = pair_tuple(t[1], pair_tuple(pair_tuple.(t[2])))
# q = pair_tuple(t[1], pair_tuple(pair_tuple.(t[2])))
q = pair_tuple(t[1], pair_tuple(t[2]))
end



# if isone(length(t[2]))
# q = pair_tuple(t[1], t[2]...)
# else
# q = pair_tuple(t[1], pair_tuple(t[2]))
# end

# sequence_length_error = "The first number in your sequence should match the length of your sequence."
# seq_length ≠ length(instructions) && throw(error("$(sequence_length_error)"))

new(q, seq_length, instructions)
end

Sequence(i::Integer, j::Tuple) = Sequence((i, j))
Sequence(i::Integer, j::Tuple) = Sequence(pair_tuple(i, pair_tuple(j...)))
Sequence(i::Integer, j::Integer...) = Sequence((i, tuple(j...)))
end # end struct

Expand All @@ -131,7 +125,7 @@ ifzero_goto(t::Tuple) = Instruction(__ifzero_goto_identifier, (t[1], t[2])...)
ifzero_goto(n::Integer, k::Integer) = Instruction(__ifzero_goto_identifier, (n, k)...)
halt() = Instruction(__halt_identifier...)

struct GoToProgramme
struct GoToProgramme <: Programme
P::Integer
programme_length::Integer
instructions::Vector{<:Tuple}
Expand All @@ -149,9 +143,7 @@ struct GoToProgramme
programme_length = sequence_dump.seq_length

# construct list of codes for each instruction
if iszero(programme_length) instructions = 0 end
instructions = [i.instruction for i in snapshot]

instructions = iszero(programme_length) ? 0 : [i.instruction for i in snapshot]
# check that the programme halts at the end
instructions[end] halt().instruction && throw(error("Goto programmes neccesarily have a halting instruction."))

Expand Down Expand Up @@ -224,3 +216,17 @@ show_programme(io::IO, P::Integer) = show_programme(io::IO, GoToProgramme(P))
# Fall back to standard output
show_programme(P::GoToProgramme) = show_programme(stdout, P)
show_programme(P::Integer) = show_programme(stdout, GoToProgramme(P))

function Base.rand(::Type{T}, d::Integer; upper_bound::Integer=200) where T <: GoToProgramme
return pair_tuple(d, pair_tuple(rand(1:upper_bound), halt().I))
return Sequence((d, (rand(1:upper_bound), halt().I))).q
end

function Base.rand(::Type{T}) where T <: GoToProgramme
while true
try
return rand(T, rand(2:10))
catch
end
end
end
13 changes: 7 additions & 6 deletions src/machines.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
exec julia --project="$(realpath $(dirname $0))" --color=yes --startup-file=no -e 'include(popfirst!(ARGS))' \
"${BASH_SOURCE[0]}" "$@"
=#


include(joinpath(dirname(@__FILE__), "abstract_types.jl"))
include(joinpath(dirname(@__FILE__), "coding.jl"))
include(joinpath(dirname(@__FILE__), "goto.jl"))
include(joinpath(dirname(@__FILE__), "utils.jl"))
Expand All @@ -15,21 +16,21 @@ include(joinpath(dirname(@__FILE__), "utils.jl"))
@enum Move Left=1 Stay Right

# defing structs
mutable struct MachineState
mutable struct MachineState <: MachineComponent
state::String
tape::Dict{Int, String}
headpos::Int
end

struct Rule
struct Rule <: MachineComponent
instate::String
outstate::String
s1::String
s2::String
move::Move
end
struct TMProgramme

struct TMProgramme <: TuringMachine
title::String
initial::String
final::String
Expand Down Expand Up @@ -85,7 +86,7 @@ end

#--Register Machines--------------------------------------------------------------------------

mutable struct RegisterMachine
mutable struct RegisterMachine <: Machine
contents::AbstractArray#Vector{<:Integer}

function RegisterMachine(contents::AbstractArray)
Expand Down

0 comments on commit 4540a0d

Please sign in to comment.