Add files via upload

master
Nuño Sempere 6 years ago committed by GitHub
parent 92245197b1
commit 48fb1a487e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

Binary file not shown.

@ -0,0 +1,62 @@
#include <stdlib.h>
#include <stdio.h>
#include "turing.h"
#include "states.h"
#include "const.h"
#define REJECT -1
#define ACCEPT -2
// gcc -g main.c turing.c states.c turing.h states.h const.h -o main
int main(){
int List[] = {0,10,10,10,10,5,1,8,2,9,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,42};
// 0 == |- ; leftmost character. This TM is only infinite in one direction.
// 5: A character to detect whether the first divisor of n is m.
// 10 ==A: Every time it finds a prime, it changes an A to a B. When it runs out of As, it stops.
// 11 ==B. Wie gesagt.
// At every step, it checks whether a number n, represented by a number of 1s followed by an 8.
// For example: 10= 1111111118; 1x9, and 8x1.
// divides a number m, represented by a number of 2s followed by a 9.
// 12: Blanks. Out Turing Machine should have infinite of those to the right.
// m will be moved to the right, to make room for 7s between n and m.
// 7s: Between n and m, regulate the maximum size of the divisors of the current number.
int longitud=length(List);
fprintf(stdout,"\nEsta función acepta cuando ha encontrado el enésimo primo\n");
int state=0;
int previous_state=0;
int position=0;
int symbol= List[position];
int previous_symbol=List[position];
int movement =0; // undefined.
int placeholder;
fprintf(stdout,"\nIt starts at State=0, Symbol =0, Position =0, \n");
print_lista(List, longitud, position);
fprintf(stdout, "\n");
while(state!=ACCEPT && state!=REJECT){
fscanf(stdin,"\n%d", &placeholder);
previous_state= state;
previous_symbol=symbol;
carry_out_step(&symbol,&state,&movement);
List[position]=symbol;
position = position +movement;
symbol=List[position];
print_linea();
print_variables(previous_state, state,position,previous_symbol, symbol, movement);
print_lista(List, longitud, position);
}
return 1;
}

@ -0,0 +1,446 @@
#include <stdlib.h>
#include <stdio.h>
#include "const.h"
#include "states.h"
// States of a Turing Machine that checks whether a number DOESN'T divide another number
// NTS: Check that the first number is smaller? Actually not necessary: see state 2.
/*
Initially, I defined a triplet, which was what the different states, which are functions returned.
typedef struct {
int state; // Finite list of states: initial (0), state 1, state 2, state 3, ..., state 7.
int symbol; // Finite list of symbols: 0,1,2,3,4,5,6,7,8,9
int movement; // Left = -1, Right =1;
} ssm;
But I thought that modifying pointers was easier and more elegant.
*/
void state0(int *symbol, int *state, int *movement);
void state1(int *symbol, int *state, int *movement);
void state2(int *symbol, int *state, int *movement);
void state3(int *symbol, int *state, int *movement);
void state4(int *symbol, int *state, int *movement);
void state5(int *symbol, int *state, int *movement);
void state6(int *symbol, int *state, int *movement);
void state7(int *symbol, int *state, int *movement);
void state8(int *symbol, int *state, int *movement);
void state9(int *symbol, int *state, int *movement);
void state10(int *symbol, int *state, int *movement);
void state11(int *symbol, int *state, int *movement);
void state12(int *symbol, int *state, int *movement);
void state13(int *symbol, int *state, int *movement);
void state14(int *symbol, int *state, int *movement);
// This function is the delta of which Kozen speaks of in p.210 &ss in Automata and Computability:
void carry_out_step(int *symbol, int *state, int *movement){ // I could make this a bigger if/else tree, but I don't want to.
// I thought to use pointers to functions, https://stackoverflow.com/questions/840501/how-do-function-pointers-in-c-work, but this proved unnecessary.
if(*state==0){
state0(symbol,state, movement);
}
else if(*state==1){
// Nótese que los else ifs son necesarios, porque después de state0, *state es =1, pero queremos que se de cuenta en la siguiente vuelta
state1(symbol,state, movement);
}
else if(*state==2){
state2(symbol,state, movement);
}
else if(*state==3){
state3(symbol,state, movement);
}
else if(*state==4){
state4(symbol,state, movement);
}
else if(*state==5){
state5(symbol,state, movement);
}
else if(*state==6){
state6(symbol,state, movement);
}
else if(*state==7){
state7(symbol,state, movement);
}
else if(*state==8){
state8(symbol,state, movement);
}
else if(*state==9){
state9(symbol,state, movement);
}
else if(*state==10){
state10(symbol,state, movement);
}
else if(*state==11){
state11(symbol,state, movement);
}
else if(*state==12){
state12(symbol,state, movement);
}
else if(*state==13){
state13(symbol,state, movement);
}
else if(*state==14){
state14(symbol,state, movement);
}
}
void state0(int *symbol, int *state, int *movement){
if(*symbol ==0){
*symbol=0; // This is the left endmarker. It isn't changed. This line could be ommited.
*movement= 1; // Move to the right (0 would be to the left).
*state=1; // Change to state 1.
}
else{
*state = REJECT; // This is defined as a constant in const.h
// The program ends, because it has unspecified behaviour
*movement= 0;
}
}
void state1(int *symbol, int *state, int *movement){
if(*symbol ==1){
*symbol=3; // Another symbol.
*movement= 1; // Move to the right (-1 would be to the left).
*state=2; // Change to state 2.
}
else if(*symbol ==3){ // Note that we need an else because otherwise after it changes a 1 to a 3, symbol=3 and it goes back to state1.
// Note that we have inadvertently created a function to see whether m is(n't) k modulo n; we would only have to start with n-k 3s at the beginning
*symbol=3;
*movement= 1;
*state=1;
}
else if(*symbol ==8){ // 8 = X
*symbol=8;
*movement= 1;
*state=4;
}
else{
*symbol=*symbol;
*movement= 1;
*state=1;
}
}
void state2(int *symbol, int *state, int *movement){
if(*symbol ==2){
*symbol=4;
*movement= -1;
*state=3;
}
else if(*symbol ==9){
*symbol=9;
*movement= -1;
*state=7;
}
else{
*symbol=*symbol;
*movement= 1;
*state=2;
}
}
void state3(int *symbol, int *state, int *movement){
if(*symbol ==0){
*symbol=0;
*movement= 1;
*state=1;
}
else {
*symbol=*symbol;
*movement = -1;
*state= 3;
}
}
void state4(int *symbol, int *state, int *movement){
if(*symbol ==2){
*symbol=4;
*movement=-1;
*state=5;
}
else if(*symbol ==9){
*symbol=9;
*movement=-1;
*state = 6;
}
else{
*symbol = *symbol;
*movement =1;
*state=4;
}
}
void state5(int *symbol, int *state, int *movement){
// Note that after state 4, the TM is in m-territory.
if(*symbol ==3){
*symbol=1;
*movement=-1;
*state=5;
}
else if(*symbol ==5){
*symbol=6;
*movement= -1;
*state = 5;
}
else if(*symbol ==0){
*symbol=0;
*movement= 1;
*state = 1;
}
else{
*symbol = *symbol;
*movement =-1;
*state=5;
}
}
void state6(int *symbol, int *state, int *movement){
if(*symbol ==5){
*symbol=5;
*movement=-1;
*state=9; // It now goes to see whether this is the nth prime.
}
else if(*symbol ==6){
*symbol=5;
*movement= -1;
*state = 10; // The number isn't a prime. Move on to the next number.
fprintf(stdout, "\nIt rejects because m is bigger than n and n | m.\n");
}
else{
*symbol = *symbol;
*movement =-1;
*state=6;
}
}
void state7(int *symbol, int *state, int *movement){
if(*symbol ==4){
*symbol=2;
*movement=-1;
*state=7;
}
else if(*symbol ==3){
*symbol=1;
*movement= -1;
*state = 7;
}
else if(*symbol ==6){
*symbol=5;
*movement= -1;
*state = 7;
}
else if(*symbol ==0){
*symbol=0;
*movement= 1;
*state = 8;
}
else{
*symbol=*symbol;
*movement= -1;
*state = 7;
}
}
void state8(int *symbol, int *state, int *movement){
if(*symbol ==8){
*symbol=1;
*movement=1;
*state=8;
}
else if(*symbol ==7){
*symbol=8;
*movement= -1;
*state = 3;
}
else if(*symbol ==2){
*symbol=2;
*movement= -1;
*state = 9;
}
else{
*symbol=*symbol;
*movement= 1;
*state = 8;
}
}
void state9(int *symbol, int *state, int *movement){
if(*symbol ==10){
*symbol=11;
*movement=1;
*state=10;
}
else if(*symbol ==0){
*symbol=0;
*movement= 0;
*state = ACCEPT;
fprintf(stdout,"\nIt accepts because it has found the nth prime\n");
}
else{
*symbol=*symbol;
*movement= -1;
*state = 9;
}
}
void state10(int *symbol, int *state, int *movement){
if(*symbol ==1){
*symbol=1;
*movement=1;
*state=11;
}
else if(*symbol ==3){
*symbol=1;
*movement=1;
*state=11;
}
else{
*symbol=*symbol;
*movement= 1;
*state = 10;
}
}
void state11(int *symbol, int *state, int *movement){
if(*symbol ==1){
*symbol=8;
*movement=1;
*state=12;
}
else if(*symbol ==3){
*symbol=8;
*movement=1;
*state=12;
}
else if(*symbol ==8){
*symbol=8;
*movement=1;
*state=12;
}
else{
*symbol=*symbol;
*movement= 0;
*state = REJECT;
// But print warning message, because it should't be here.
fprintf(stdout,"\nWarning: Unpredicted behaviour in state 11");
}
}
void state12(int *symbol, int *state, int *movement){
if(*symbol ==1){
*symbol=7;
*movement=1;
*state=12;
}
else if(*symbol ==3){
*symbol=7;
*movement=1;
*state=12;
}
else if(*symbol ==8){
*symbol=7;
*movement=1;
*state=12;
}
else if(*symbol ==2){
*symbol=7;
*movement=1;
*state=13;
}
else if(*symbol ==4){
*symbol=7;
*movement=1;
*state=13;
}
else if(*symbol ==7){
*symbol=7;
*movement=1;
*state=12;
}
else{
*symbol=*symbol;
*movement= 0;
*state = REJECT;
// But print warning message, because it should't be here.
fprintf(stdout,"\nWarning: Unpredicted behaviour in state 12");
}
}
void state13(int *symbol, int *state, int *movement){
if(*symbol ==4){
*symbol=2;
*movement=1;
*state=13;
}
if(*symbol ==9){
*symbol=2;
*movement=1;
*state=13;
}
else if(*symbol ==12){
*symbol=2;
*movement=1;
*state=14;
}
else{
*symbol=*symbol;
*movement= 1;
*state = 13;
}
}
void state14(int *symbol, int *state, int *movement){
if(*symbol ==12){
*symbol=9;
*movement=-1;
*state=3;
}
else{
*symbol=*symbol;
*movement= 0;
*state = REJECT;
// But print warning message, because it should't be here.
fprintf(stdout,"\nWarning: Unpredicted behaviour in state 14");
}
}

@ -0,0 +1,6 @@
#ifndef __STATES_H_
#define __STATES_H_
void carry_out_step(int *symbol, int *state, int *movement); // This is the only function I'm going to need to use outside of states.c
#endif

Binary file not shown.

@ -0,0 +1,150 @@
#include <stdlib.h>
#include <stdio.h>
#include "turing.h"
#include "const.h"
// Turing print defines functions that take a list, take a position in that list and print the list with a pointer to that position.
// It prints this vertically
int length(int *List){
int i =0;
while (List[i]<42){ // Por cómo construimos nuestras listas.
i=i+1;
}
/*Anecdóticamente, el siguiente código da error.
i=0;
while (List[i]<=9){
i=i+1;
}
Y lo hace porque al asignar un trozo de memoria,
si la lista tiene, p.ej., longitud 6,
List[0] hasta Lista[5] son la lista de verdad,
pero Lista[6] <9, y la sigue contando.
*/
//fprintf(stdout, "\n%d\n",i+1);
return i;
}
void print_lista(int*L, int length, int position){
if(L==NULL || length <= position){
fprintf(stdout, "\nError en prettyprint");
}
else{
int i=0;
for(i=0; i<length; i++){
if(i%20==0){
fprintf(stdout, "\n");
}
if(i==position){
fprintf(stdout, "{ ");
}
else{
fprintf(stdout, " ");
}
if( L[i]==0){
fprintf(stdout, ">");
}
else if(L[i]==8){
fprintf(stdout, "X");
}
else if(L[i]==10){
fprintf(stdout, "A");
}
else if(L[i]==11){
fprintf(stdout, "B");
}
else if(L[i]==12){
fprintf(stdout, "_");
}
else if(L[i]==9){
fprintf(stdout, "W");
}
else{
fprintf(stdout, "%d",L[i]);
}
if(i==position){
fprintf(stdout, " } ");
}
else{
fprintf(stdout, " ");
}
}
fprintf(stdout, "\n\n");
}
}
void print_lista2(int*L, int length, int position){
if(L==NULL || length <= position){
fprintf(stdout, "\nError en prettyprint");
}
else{
int i=0;
char tabs[]="\t\t\t\t\t\t\t\0";
for(i=0; i<length; i++){
if(i==position){
fprintf(stdout, "\n%s{",tabs);
}
else{
fprintf(stdout, "\n%s",tabs);
}
if( L[i]==0){
fprintf(stdout, "T");
}
else if(L[i]==8){
fprintf(stdout, "X");
}
else if(L[i]==9){
fprintf(stdout, "W");
}
else{
fprintf(stdout, "%d",L[i]);
}
if(i==position){
fprintf(stdout, "}\n");
}
else{
fprintf(stdout, "\n");
}
}
}
}
void print_variables(int previous_state, int state, int position, int previous_symbol, int symbol, int movement){
char tabs[]="\t\t\t\t\t\0";
fprintf(stdout,"\n%sPrevious State = %d -> Current state = %d\n",tabs,previous_state, state);
fprintf(stdout,"\n%sPrevious Symbol = %d -> Current Symbol = %d\n",tabs,previous_symbol, symbol);
fprintf(stdout,"\n%sPrevious Position = %d -> Current Position = %d \n",tabs,position-movement,position);
fprintf(stdout,"\n%sMovement =%d\n\n",tabs,movement);
}
void print_variables2(int state, int position, int symbol, int movement){
char tabs[]="\t\t\t\t\t\t\t\0";
fprintf(stdout,"\n%sState =%d\n",tabs,state);
fprintf(stdout,"\n%sPosition =%d\n",tabs,position);
fprintf(stdout,"\n%sSymbol =%d\n",tabs,symbol);
fprintf(stdout,"\n%sMovement =%d\n\n",tabs,movement);
}
void print_linea(){
char tabs[]="\t\t\0";
fprintf(stdout,"\n%s__________________________________________________________________________________________\n",tabs);
}

@ -0,0 +1,8 @@
#ifndef __CONST_H_
#define __CONST_H_
void print_lista(int*L, int length, int position);
void print_variables(int previous_state, int state, int position, int previous_symbol, int symbol, int movement);
int length(int *List);
void print_linea();
#endif
Loading…
Cancel
Save