🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.functional
Article #59

Questions on converting Miranda code to Haskell

#59
From: Junxian J Liu
Date: Mon, 20 Jul 1992 15:57
94 lines
3553 bytes
Hello,

I have written a program in Miranda (about 2000 lines), and I want to
convert it to Haskell. The program has following features and structure:

       	1) Read source data from a file;
	2) Manipulate the source data and generate some global values;
	3) There are a number of computation modules which all access
	   the global values obtained from the source data, and the 
	   results of some computation modules are further used in
	   other computation modules.
    
I list the code of a small Miranda program which captures the same features 
as of my original program, except that my original program got more global
values and much more computation:
     
| ns    = map numval (lines (read "data"))

| n     = # ns

| sqrts = map sqrt ns

| sum_sqrts = sum sqrts

| avg_sqrts = sum_sqrts / n

| men_sqrts = sum (map minus_avg_sqr sqrts) / n
|             where
|             minus_avg_sqr x = (x - avg_sqrts) * (x - avg_sqrts)

| result  = "number of values = " ++ show n ++ "\n"
|           ++ "sum_sqrts = " ++ show sum_sqrts ++ "\n"
|           ++ "avg_sqrts = " ++ show avg_sqrts ++ "\n"
|           ++ "men_sqrts = " ++ show men_sqrts ++ "\n"

| main    = [Stdout result]


The approaches which I could think of were:

	1) Add the input stream as an extra argument to every functions 
	   and variables, replace the expression (read "data") by the 
	   argument (i.e. the input stream), and change the main function
	   accordingly as outlined below:

		> main = readChan "data" abort 
		>	 (\ s -> appendChan "stdout" (result s) abort done)
		
		> ns s = map read (lines s)
		
		> n  s = length (ns s)
		... ...

	2) Replace the expression (read "data") by the input stream, 
	   change the main function accordingly and put all other function
	   and variable definitions into a BIG where expression:

		> main = readChan "data" abort
		>        (\ s -> appendChan "stdout" (result s) abort done
		>		 where
		>		 ns = map read (lines s)
		>		 n  = length ns
		>		 ... ...
		>	 )

	3) Or do something between the above two approaches, but not
	   really add the input stream as extra argument.

Though the first two approaches are very simple and straightforward, the
approach 1 lose the sharing of global variables completely and the approach
2 can only be applied very very small program. While using the last approach
needs much more thought and may need to change the original code quite lot 
depending on the program to be converted.


My questions then are: 

	How can the Miranda program be easily converted to Haskell?

	Will the performance be affected in the resulting Haskell program
	in terms of preserving sharing?

	Can the approach be applied to large programs?

Thanks in advance for any help.

Please mail me and I'll summarise if there is enough interest.

Liu.
-------
Dr J Liu,   Department of Computing, Imperial College, 
            180 Queens Gate, London SW7 2BZ, United Kingdom.
Telephone : (+44 71/071) 589 5111 ext. 5033  Fax : (+44 71/071) 581 8024
Email:      jjl@doc.ic.ac.uk
Technical Details
Message-ID: <13833.9207201557@peaberry.doc.ic.ac.uk>
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!uknet!glasgow!daemon

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!uknet!glasgow!daemon
From: jjl@doc.ic.ac.uk (Junxian J Liu)
Newsgroups: comp.lang.functional
Subject: Questions on converting Miranda code to Haskell
Message-ID: <13833.9207201557@peaberry.doc.ic.ac.uk>
Date: 20 Jul 92 15:57:57 GMT
Sender: daemon@dcs.glasgow.ac.uk
Organization: Glasgow University Computing Science Dept.
Lines: 94
Approved: usenet@dcs.glasgow.ac.uk
X-Mailer: mail-news 2.0.5