0

My problem is probably a format error but i can't find what error it is, when i use readFile and then I try to read that string into a data type, i receive that error.

I'm implementing a simple interpreter of a programming language in Haskell like this post:

Implementing a language interpreter in haskell

Then, I am trying to read from a file some datatype from user and files, when it is from user, it correctrly works, but not for the types read from file. My datatypes are the following:

infixr 1 :=
infix 4 :<, :>, :==, :>=, :<= 
infixl 6 :+, :-
infixl 7 :*, :/

data Valuable = I Int --Entero
                | V Variable --Variable
                | Valuable :+ Valuable  
                | Valuable :- Valuable  
                | Valuable :* Valuable  
                | Valuable :/ Valuable  
                deriving (Read, Show); 


data Boolval = B Bool --Dato booleano
               | Valuable :>= Valuable 
               | Valuable :<= Valuable 
               | Valuable :== Valuable  
               | Valuable :> Valuable  
               | Valuable :< Valuable  
               deriving (Read, Show);

data Instruccion = Valuable := Valuable 
                   | While Boolval Instruccion 
                   | Cond Boolval Instruccion  
                   | Varias [Instruccion] 
                   deriving (Read, Show);

And when I'm trying to get the input with this procedure:

mst::[VarVal]->String -- Varval is [String, int]
mst [] = ""
mst[(a,b)] = "('" ++ a ++"', "++ (show b) ++ ")"
mst((a,b):xs) = "('" ++ a ++"', "++ (show b) ++ "), " ++ mst xs

main = do 
  putStrLn "Introduce el nombre del programa a cargar (Por ejemplo arch.txt)"
  file <- getLine  --Se toma el nombre de archivo
  putStrLn "Introduce el estado de las variables con comillas dobles, (ej [(`X`,3)])"
  user_state <- getLine   --Se toma la entrada
  arch <- readFile file
  let real_state = read user_state
  let my_program_file = read arch
  putStrLn arch
  let lst = ejecutar my_program_file real_state --problemas
  putStrLn (mst lst)

The problem comes when is used the "read file" instruction, because the input format is not correct(I'm not sure) and i get the message: *** Exception: Prelude.read: no parse. this is the input format

Varias [(V "Y" := (V "X"), 
              (V "R" := I 1), 
               (While  (I 0 :< V "Y")  (Varias [
                       (V "R" := (V "R" :* V "Y")), 
                       (V "Y" := (V "Y" :- I 1))
                       ])
                ) 
            ]

What am I doing wrong?

Community
  • 1
  • 1
Kaostias
  • 321
  • 4
  • 24
  • 1
    You have a problem with parentheses. You have to write `While (I 0 :< V "Y") (Varias [V "R" := (V "R" :* V "Y"), V "Y" := (V "Y" :- I 1)])`. You can omit the parentheses around multiplication and subtraction if you set the fixities correctly (ie `infixr 1 :=`): `While (I 0 :< V "Y") (Varias [V "R" := V "R" :* V "Y", V "Y" := V "Y" :- I 1]) ` – user2407038 Jun 05 '14 at 09:19
  • I have declared as infixr, and i have prooved with parentheses too, I'll change the question, thanks anyway – Kaostias Jun 05 '14 at 09:34
  • 1
    In your update, the parentheses aren't matching. – user2407038 Jun 05 '14 at 10:06
  • Thanks, it works, i added the infixr := that i hadn't before and removed the wrong pharenhteses and it worked, if you post all of this as an answer i'll give you the answer. – Kaostias Jun 05 '14 at 10:30
  • @Kaostias Isn't the due date 2014/6/6 12:05 ?:) – rpax Jun 05 '14 at 17:46

0 Answers0