<div>Hi all,<br>
<br>
You are talking about parsing and data type, so I want to ask you a
question relating to my data type. I have a data types and a function
as follows: <br>
<br>
\begin{code}<br>
<br>
data Type a = C1 <br>
{ x :: [String] <br>
,y :: Type a} <br>
| C2 {x1 :: String} <br>
| C3 {y1 :: Bool} deriving Show <br>
<br>
showType :: Type a -> String<br>
showType (C2 a) = show a<br>
showType (C3 a) = show a<br>
showType (C1 a b) = show "(" ++ (show a) ++ (showType b) ++ (show ")")<br>
<br>
r1 = C1 { <br>
x = ["x","y"] <br>
, y = C2{x1 = "x^2 + y^2 < 20"}<br>
}<br>
<br>
r2=C3{y1=False}<br>
<br>
r3=C2{x1="x+y=z"}<br>
<br>
\end{code}<br>
<br>
--Result:<br>
<br>
Main>showType r1<br>
<br>
"\"(\"[\"x\",\"y\"]\"x^2 + y^2 < 20\"\")\""<br>
<br>
Main>showType r2<br>
"False"<br>
<br>
Main>showType r3<br>
"\"x+y=z\""<br>
<br>
Now, I want to write a function to see the result in its original type, for example :<br>
input: "False"<br>
output: False<br>
<br>
input:"\"x+y=z\""<br>
output:"x+y=z"<br>
<br>
input:"\"(\"[\"x\",\"y\"]\"x^2 + y^2 < 20\"\")\""<br>
output:(["x","y"]x^2 + y^2 < 20")<br>
<br>
So, how I should write the function ? Do you have any idea about this ? Thanks.<br>
<br>
<br>
From: <a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:oleg@pobox.com">oleg@pobox.com</a> <<a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:oleg@pobox.com">oleg@pobox.com</a>
><br>
Date: Oct 16, 2005 8:50 PM<br>
Subject: [Haskell-cafe] Generic parser without GADTs<br>
To: <a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:haskell-cafe@haskell.org">haskell-cafe@haskell.org</a><br>
Cc: <a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:ralf@informatik.uni-bonn.de">ralf@informatik.uni-bonn.de</a><br>
<br>
<br>
<br>
Also inspired by Ralf Hinze's post, I thought of removing GADTs from<br>
that code. The result is Haskell98! code, which works well in<br>
Hugs. The code seems to be a bit simpler too. Like the original code,<br>
the function 'parseAny' correctly discriminates between the list of<br>
characters (i.e., strings) and the list of other things.<br>
<br>
{-- Haskell98! --}<br>
<br>
class Type a where<br>
parse :: ReadS a<br>
<br>
<br>
> hugs /tmp/h.hs<br>
Haskell 98 mode: Restart with command line option -98 to enable extensions<br>
<br>
Main> parseAny "4711"<br>
[(ValInt 4711,"")]<br>
Main> parseAny "\"4711\""<br>
[(ValString "4711","")]<br>
Main> parseAny "[4711, 0]"<br>
[(ValList [ValInt 4711,ValInt 0],"")]<br>
Main> parseAny "[4711, 'a']"<br>
[(ValList [ValInt 4711,ValChar 'a'],"")]<br>
Main> parseAny "[\"hello world\"] x"<br>
[(ValList [ValString "hello world"]," x")]<br>
______________________________
<div id="mb_0">_________________<br>Haskell-Cafe mailing list<br><a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br><a onclick="return top.js.OpenExtLink(window,event,this)" href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">
http://www.haskell.org/mailman/listinfo/haskell-cafe</a></div>
</div>
<span class="q">
</span>