title: Generating and running Haskell code from Python
body: <p>We are writing a python program that attempts to synthesize a (simple) haskell-function given input-output pairs. Throughout the run of the program, we generate haskell code and check its correctness against the user-supplied examples. 
  Suppose we get as input "1 2" and expected output "3". We would (eventually)
  come up with the plus function. We would then run 
  <code>(\x y -&gt; x + y) 1 2</code> in haskell and check if it evaluates to 3.</p>
  
  <p>The way we currently do things is by running the following python code:</p>
  
  <pre><code>from subprocess import Popen, PIPE, STDOUT
  proccess = Popen(f'ghc -e "{haskell_code}"', shell=True, stdout=PIPE, stderr=STDOUT) 
  haskell_output = proc.stdout.read().decode('utf-8').strip('\n')
  </code></pre>
  
  <p>As neither of us is familiar with ghc, haskell, processes, or really anything to do with any of this, we were hoping someone could help us with preforming this task in a (much) more efficient manner, as this is currently very slow.</p>
  
  <p>Additionally, we would like to be able to perform more than a single statement. For example, we would like to import Data.Char so that our function can use “toUpper”. However, the way we are currently doing this is by sending a single lambda function and the inputs appended to it, and we aren't sure how to add an import statement above that (adding "\n" did not seem to work).</p>
  
  <p>To summarize, we would like the fastest (runtime) solution that would allow us to test haskell functions from python (where we don’t have the code for all haskell functions in advance or at one point in time, but rather test as we generate the code), while allowing us to use more than a single statement (for example, importing).</p>
  
  <p>Apologies if any of this is trivial or stupid, any help would be highly appreciated. </p>