Thrift: Haskell library and codegen

Summary: It's thrift for haskell. The codegen is complete. The library has binary protocol, io channel transport, and a threaded server.
Reviewed by: mcslee
Test plan: Yes
Revert plan: yes


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665174 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/compiler/cpp/Makefile.am b/compiler/cpp/Makefile.am
index 6465e56..8ddb161 100644
--- a/compiler/cpp/Makefile.am
+++ b/compiler/cpp/Makefile.am
@@ -16,8 +16,8 @@
                  src/generate/t_xsd_generator.cc \
                  src/generate/t_perl_generator.cc \
 		 src/generate/t_ocaml_generator.cc \
-                 src/generate/t_erl_generator.cc
-
+                 src/generate/t_erl_generator.cc \
+		 src/generate/t_hs_generator.cc 
 
 thrift_CXXFLAGS = -Wall -Isrc $(BOOST_CPPFLAGS)
 thrift_LDFLAGS = -Wall $(BOOST_LDFLAGS)
diff --git a/compiler/cpp/src/generate/t_hs_generator.cc b/compiler/cpp/src/generate/t_hs_generator.cc
new file mode 100644
index 0000000..c46d70d
--- /dev/null
+++ b/compiler/cpp/src/generate/t_hs_generator.cc
@@ -0,0 +1,1344 @@
+// Copyright (c) 2006- Facebook
+// Distributed under the Thrift Software License
+//
+// See accompanying file LICENSE or visit the Thrift site at:
+// http://developers.facebook.com/thrift/
+
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sstream>
+#include "t_hs_generator.h"
+using namespace std;
+
+/*
+ * This is necessary because we want typedefs to appear later,
+ * after all the types have been declared.
+ */
+void t_hs_generator::generate_program() {
+  // Initialize the generator
+  init_generator();
+
+  // Generate enums
+  vector<t_enum*> enums = program_->get_enums();
+  vector<t_enum*>::iterator en_iter;
+  for (en_iter = enums.begin(); en_iter != enums.end(); ++en_iter) {
+    generate_enum(*en_iter);
+  }
+
+  // Generate structs
+  vector<t_struct*> structs = program_->get_structs();
+  vector<t_struct*>::iterator st_iter;
+  for (st_iter = structs.begin(); st_iter != structs.end(); ++st_iter) {
+    generate_struct(*st_iter);
+  }
+
+  // Generate xceptions
+  vector<t_struct*> xceptions = program_->get_xceptions();
+  vector<t_struct*>::iterator x_iter;
+  for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
+    generate_xception(*x_iter);
+  }
+
+  // Generate typedefs
+  vector<t_typedef*> typedefs = program_->get_typedefs();
+  vector<t_typedef*>::iterator td_iter;
+  for (td_iter = typedefs.begin(); td_iter != typedefs.end(); ++td_iter) {
+    generate_typedef(*td_iter);
+  }
+
+  // Generate services
+  vector<t_service*> services = program_->get_services();
+  vector<t_service*>::iterator sv_iter;
+  for (sv_iter = services.begin(); sv_iter != services.end(); ++sv_iter) {
+    service_name_ = get_service_name(*sv_iter);
+    generate_service(*sv_iter);
+  }
+
+  // Generate constants
+  vector<t_const*> consts = program_->get_consts();
+  generate_consts(consts);
+
+  // Close the generator
+  close_generator();
+}
+
+
+/**
+ * Prepares for file generation by opening up the necessary file output
+ * streams.
+ *
+ * @param tprogram The program to generate
+ */
+void t_hs_generator::init_generator() {
+  // Make output directory
+  mkdir(T_HS_DIR, S_IREAD | S_IWRITE | S_IEXEC);
+
+  // Make output file
+
+  string pname = capitalize(program_name_);
+  string f_types_name = string(T_HS_DIR)+"/"+pname+"_Types.hs";
+  f_types_.open(f_types_name.c_str());
+
+  string f_consts_name = string(T_HS_DIR)+"/"+pname+"_Consts.hs";
+  f_consts_.open(f_consts_name.c_str());
+
+  // Print header
+  f_types_ <<
+    hs_autogen_comment() << endl <<
+    "module " << pname <<"_Types where" << endl <<
+    hs_imports() << endl;
+
+  f_consts_ <<
+    hs_autogen_comment() << endl <<
+    "module " << pname <<"_Consts where" << endl <<
+    hs_imports() << endl <<
+    "import " << pname<<"_Types"<< endl;
+
+}
+
+
+/**
+ * Autogen'd comment
+ */
+string t_hs_generator::hs_autogen_comment() {
+  return
+    std::string("-----------------------------------------------------------------\n") +
+    "-- Autogenerated by Thrift                                     --\n" +
+    "--                                                             --\n" +
+    "-- DO NOT EDIT UNLESS YOU ARE SURE YOU KNOW WHAT YOU ARE DOING --\n" +
+    "-----------------------------------------------------------------\n";
+}
+
+/**
+ * Prints standard thrift imports
+ */
+string t_hs_generator::hs_imports() {
+  return "import Thrift\nimport Data.Generics\nimport Control.Exception\nimport qualified Data.Map as Map\nimport qualified Data.Set as Set\nimport Data.Int";
+}
+
+/**
+ * Closes the type files
+ */
+void t_hs_generator::close_generator() {
+  // Close types file
+  f_types_.close();
+  f_consts_.close();
+}
+ 
+/**
+ * Generates a typedef. Ez.
+ *
+ * @param ttypedef The type definition
+ */
+void t_hs_generator::generate_typedef(t_typedef* ttypedef) {
+  f_types_ <<
+    indent() << "type "<< capitalize(ttypedef->get_symbolic()) << " = " << render_hs_type(ttypedef->get_type()) << endl << endl;
+}
+
+/**
+ * Generates code for an enumerated type. 
+ * the values.
+ *
+ * @param tenum The enumeration
+ */
+void t_hs_generator::generate_enum(t_enum* tenum) {
+  indent(f_types_) << "data "<<capitalize(tenum->get_name())<<" = ";
+  indent_up();
+  vector<t_enum_value*> constants = tenum->get_constants();
+  vector<t_enum_value*>::iterator c_iter;
+  bool first = true;
+  for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
+    string name = capitalize((*c_iter)->get_name());
+    if(first)
+      first=false;
+    else
+      f_types_ << "|";
+    f_types_ << name;
+  }
+  indent(f_types_) << "deriving (Show,Eq, Typeable, Data, Ord)" << endl;
+  indent_down();
+
+  int value = -1;
+  indent(f_types_) << "instance Enum " << capitalize(tenum->get_name()) << " where" << endl;
+  indent_up();
+  indent(f_types_) << "fromEnum t = case t of" << endl;
+  indent_up();
+  for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
+    if ((*c_iter)->has_value()) {
+      value = (*c_iter)->get_value();
+    } else {
+      ++value;
+    }
+    string name = capitalize((*c_iter)->get_name());
+    
+    f_types_ <<
+      indent() << name << " -> " << value << endl;
+  }
+  indent_down();
+
+  indent(f_types_) << "toEnum t = case t of" << endl;
+  indent_up();
+  for(c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
+    if ((*c_iter)->has_value()) {
+      value = (*c_iter)->get_value();
+    } else {
+      ++value;
+    }
+    string name = capitalize((*c_iter)->get_name());
+    
+    f_types_ <<
+      indent() << value << " -> " << name << endl;
+  }
+  indent(f_types_) << "_ -> throwDyn Thrift_Error" << endl;
+  indent_down();
+  indent_down();
+}
+
+/**
+ * Generate a constant value
+ */
+void t_hs_generator::generate_const(t_const* tconst) {
+  t_type* type = tconst->get_type();
+  string name = decapitalize(tconst->get_name());
+  t_const_value* value = tconst->get_value();
+
+  indent(f_consts_) << name << " = " << render_const_value(type, value) << endl << endl;
+}
+
+/**
+ * Prints the value of a constant with the given type. Note that type checking
+ * is NOT performed in this function as it is always run beforehand using the
+ * validate_types method in main.cc
+ */
+string t_hs_generator::render_const_value(t_type* type, t_const_value* value) {
+  std::ostringstream out;
+  if (type->is_base_type()) {
+    t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+    switch (tbase) {
+    case t_base_type::TYPE_STRING:
+      out << "\"" << value->get_string() << "\"";
+      break;
+    case t_base_type::TYPE_BOOL:
+      out << (value->get_integer() > 0 ? "True" : "False");
+      break;
+    case t_base_type::TYPE_BYTE:
+    case t_base_type::TYPE_I16:
+    case t_base_type::TYPE_I32:
+    case t_base_type::TYPE_I64:
+      out << value->get_integer();
+      break;
+    case t_base_type::TYPE_DOUBLE:
+      if (value->get_type() == t_const_value::CV_INTEGER) {
+        out << value->get_integer();
+      } else {
+        out << value->get_double();
+      }
+      break;
+    default:
+      throw "compiler error: no const of base type " + tbase;
+    }
+  } else if (type->is_enum()) {
+    t_enum* tenum = (t_enum*)type;
+    vector<t_enum_value*> constants = tenum->get_constants();
+    vector<t_enum_value*>::iterator c_iter;
+    int val = -1;
+    for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
+      if ((*c_iter)->has_value()) {
+        val = (*c_iter)->get_value();
+      } else {
+        ++val;
+      }
+      if(val == value->get_integer()){
+        indent(out) << capitalize((*c_iter)->get_name());
+        break;
+      }
+    }
+  } else if (type->is_struct() || type->is_xception()) {
+    string cname = type_name(type);
+    indent(out) << cname << "{";
+    const vector<t_field*>& fields = ((t_struct*)type)->get_members();
+    vector<t_field*>::const_iterator f_iter;
+    const map<t_const_value*, t_const_value*>& val = value->get_map();
+    map<t_const_value*, t_const_value*>::const_iterator v_iter;
+    bool first = true;
+    for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
+      t_type* field_type = NULL;
+      for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+        if ((*f_iter)->get_name() == v_iter->first->get_string()) {
+          field_type = (*f_iter)->get_type();
+        }
+      }
+      if (field_type == NULL) {
+        throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
+      }
+      string fname = v_iter->first->get_string();
+      if(first)
+        first=false;
+      else
+        out << ",";
+      out << "f_" << cname << "_" << fname << " = Just (" << render_const_value(field_type, v_iter->second) << ")";
+
+    }
+    indent(out) << "}";
+  } else if (type->is_map()) {
+    t_type* ktype = ((t_map*)type)->get_key_type();
+    t_type* vtype = ((t_map*)type)->get_val_type();
+    const map<t_const_value*, t_const_value*>& val = value->get_map();
+    map<t_const_value*, t_const_value*>::const_iterator v_iter;
+    out << "(Map.fromList [";
+    bool first=true;
+    for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
+      string key = render_const_value(ktype, v_iter->first);
+      string val = render_const_value(vtype, v_iter->second);
+      if(first)
+        first=false;
+      else
+        out << ",";
+      out << "(" << key << ","<< val << ")";
+    }
+    out << "])";
+  } else if (type->is_list()) {
+    t_type* etype;
+    etype = ((t_list*)type)->get_elem_type();
+    out << "[";
+    const vector<t_const_value*>& val = value->get_list();
+    vector<t_const_value*>::const_iterator v_iter;
+    bool first=true;
+    for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
+      if(first)
+        first=false;
+      else
+        out << ",";
+      out << render_const_value(etype, *v_iter);
+    }
+    out << "]";
+  } else if (type->is_set()) {
+    t_type* etype = ((t_set*)type)->get_elem_type();
+    const vector<t_const_value*>& val = value->get_list();
+    vector<t_const_value*>::const_iterator v_iter;
+    out << "(mkSet [";    
+    for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
+      string val = render_const_value(etype, *v_iter);
+      out << val; 
+    }
+    out << "])";
+  }
+  return out.str();
+}
+
+/**
+ * Generates a "struct"
+ */
+void t_hs_generator::generate_struct(t_struct* tstruct) {
+  generate_hs_struct(tstruct, false);
+}
+
+/**
+ * Generates a struct definition for a thrift exception. Basically the same
+ * as a struct, but also has an exception declaration.
+ *
+ * @param txception The struct definition
+ */
+void t_hs_generator::generate_xception(t_struct* txception) {
+  generate_hs_struct(txception, true);  
+}
+
+/**
+ * Generates a Haskell struct
+ */
+void t_hs_generator::generate_hs_struct(t_struct* tstruct,
+                                              bool is_exception) {
+  generate_hs_struct_definition(f_types_,tstruct, is_exception,false);
+}
+
+/**
+ * Generates a struct definition for a thrift data type. 
+ *
+ * @param tstruct The struct definition
+ */
+void t_hs_generator::generate_hs_struct_definition(ofstream& out,
+                                                   t_struct* tstruct,
+                                                   bool is_exception,
+                                                   bool helper) {
+  string tname = type_name(tstruct);
+  string name = tstruct->get_name();
+  const vector<t_field*>& members = tstruct->get_members();
+  vector<t_field*>::const_iterator m_iter; 
+
+  indent(out) << "data "<<tname<<" = "<<tname;
+  if (members.size() > 0) {
+    out << "{";
+    bool first=true;
+    for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+      if(first)
+        first=false;
+      else
+        out << ",";
+      string mname = (*m_iter)->get_name();
+      out << "f_" << tname << "_" << mname << " :: Maybe (" << render_hs_type((*m_iter)->get_type()) << ")";
+    }
+    out << "}";
+  }
+
+  out << " deriving (Show,Eq,Ord,Typeable)" << endl;
+
+  generate_hs_struct_writer(out, tstruct);
+
+  generate_hs_struct_reader(out, tstruct);
+  //f_struct_.close();
+}
+
+
+
+/**
+ * Generates the read method for a struct
+ */
+void t_hs_generator::generate_hs_struct_reader(ofstream& out, t_struct* tstruct) {
+  const vector<t_field*>& fields = tstruct->get_members();
+  vector<t_field*>::const_iterator f_iter;
+  string sname = type_name(tstruct);
+  string str = tmp("_str");
+  string t = tmp("_t");
+  string id = tmp("_id");
+
+  indent(out) << "read_" << sname << "_fields iprot rec = do" << endl;
+  indent_up(); // do
+    
+  // Read beginning field marker
+  indent(out) << "(_," << t <<","<<id<<") <- readFieldBegin iprot" << endl;
+  // Check for field STOP marker and break
+  indent(out) <<
+    "if " << t <<" == T_STOP then return rec else" << endl;
+  indent_up(); // if
+  indent(out) << "case " << id<<" of " << endl;
+  indent_up(); // case
+  // Generate deserialization code for known cases
+  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+    indent(out) << (*f_iter)->get_key() << " -> ";
+    out << "if " << t <<" == " << type_to_enum((*f_iter)->get_type()) << " then do" << endl;
+    indent_up(); // if
+    indent(out) << "s <- ";
+    generate_deserialize_field(out, *f_iter,str);
+    out << endl;
+    indent(out) << "read_"<<sname<<"_fields iprot rec{f_"<<sname<<"_"<< decapitalize((*f_iter)->get_name()) <<"=Just s}" << endl;
+    out <<
+      indent() << "else do" << endl;
+    indent_up();
+    indent(out) << "skip iprot "<< t << endl;
+    indent(out) << "read_"<<sname<<"_fields iprot rec" << endl;
+    indent_down(); // -do
+    indent_down(); // -if
+  }
+
+
+  // In the default case we skip the field
+  out <<
+    indent() << "_ -> do" << endl;
+  indent_up();
+  indent(out) << "skip iprot "<<t<< endl;
+  indent(out) << "readFieldEnd iprot" << endl;
+  indent(out) << "read_"<<sname<<"_fields iprot rec" << endl;
+  indent_down(); // -case
+  indent_down(); // -if
+  indent_down(); // -do
+  indent_down(); 
+
+  // read
+  indent(out) << "read_"<<sname<<" iprot = do" << endl;
+  indent_up();
+  indent(out) << "readStructBegin iprot" << endl;
+  indent(out) << "rec <- read_"<<sname<<"_fields iprot ("<<sname<<"{";
+  bool first = true;
+  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+    if(first)
+      first=false;
+    else
+      out << ",";
+    out << "f_" << sname << "_" << decapitalize((*f_iter)->get_name()) << "=Nothing";
+  }
+  out << "})" << endl;
+  indent(out) << "readStructEnd iprot" << endl;
+  indent(out) << "return rec" << endl;
+  indent_down();
+}
+
+void t_hs_generator::generate_hs_struct_writer(ofstream& out,
+                                               t_struct* tstruct) {
+  string name = type_name(tstruct);
+  const vector<t_field*>& fields = tstruct->get_members();
+  vector<t_field*>::const_iterator f_iter;
+  string str = tmp("_str");
+  string f = tmp("_f");
+
+  indent(out) <<
+    "write_"<<name<<" oprot rec = do" << endl;
+  indent_up();
+  indent(out) <<
+    "writeStructBegin oprot \""<<name<<"\"" << endl;
+  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+    // Write field header
+    string mname = (*f_iter)->get_name();
+    indent(out) <<
+      "case f_" << name << "_" << mname << " rec of {Nothing -> return (); Just _v -> do" << endl;
+    indent_up();
+    indent(out) << "writeFieldBegin oprot (\""<< (*f_iter)->get_name()<<"\","
+                <<type_to_enum((*f_iter)->get_type())<<","
+                <<(*f_iter)->get_key()<<")" << endl;
+
+    // Write field contents
+    out << indent();
+    generate_serialize_field(out, *f_iter, "_v");
+    out << endl;
+    // Write field closer
+    indent(out) << "writeFieldEnd oprot}" << endl;
+    indent_down();
+  }
+
+  // Write the struct map
+  out <<
+    indent() << "writeFieldStop oprot" << endl <<
+    indent() << "writeStructEnd oprot" << endl;
+
+  indent_down();
+}
+
+/**
+ * Generates a thrift service.
+ *
+ * @param tservice The service definition
+ */
+void t_hs_generator::generate_service(t_service* tservice) {
+  string f_service_name = string(T_HS_DIR)+"/"+capitalize(service_name_)+".hs";
+  f_service_.open(f_service_name.c_str());
+
+  f_service_ <<
+    hs_autogen_comment() << endl <<
+    "module " << capitalize(service_name_) << " where" << endl <<
+    hs_imports() << endl;
+
+
+  if(tservice->get_extends()){
+    f_service_ <<
+      "import qualified " << capitalize(tservice->get_extends()->get_name()) << endl;
+  }
+
+
+  f_service_ <<
+     "import " << capitalize(program_name_) << "_Types" << endl << 
+    "import qualified " << capitalize(service_name_) << "_Iface as Iface" << endl;
+
+ 
+  // Generate the three main parts of the service 
+  generate_service_helpers(tservice);
+  generate_service_interface(tservice);
+  generate_service_client(tservice);
+  generate_service_server(tservice);
+
+
+  // Close service file
+  f_service_.close();
+}
+
+/**
+ * Generates helper functions for a service.
+ *
+ * @param tservice The service to generate a header definition for
+ */
+void t_hs_generator::generate_service_helpers(t_service* tservice) {
+  vector<t_function*> functions = tservice->get_functions();
+  vector<t_function*>::iterator f_iter;
+
+  indent(f_service_) <<
+    "-- HELPER FUNCTIONS AND STRUCTURES --" << endl << endl;
+
+  for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+    t_struct* ts = (*f_iter)->get_arglist();
+    generate_hs_struct_definition(f_service_,ts, false);
+    generate_hs_function_helpers(*f_iter);
+  }
+}
+
+/**
+ * Generates a struct and helpers for a function.
+ *
+ * @param tfunction The function
+ */
+void t_hs_generator::generate_hs_function_helpers(t_function* tfunction) {
+  t_struct result(program_, decapitalize(tfunction->get_name()) + "_result");
+  t_field success(tfunction->get_returntype(), "success", 0);
+  if (!tfunction->get_returntype()->is_void()) {
+    result.append(&success);
+  }
+
+  t_struct* xs = tfunction->get_xceptions();
+  const vector<t_field*>& fields = xs->get_members();
+  vector<t_field*>::const_iterator f_iter;
+  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+    result.append(*f_iter);
+  }
+  generate_hs_struct_definition(f_service_,&result, false);
+}
+
+/**
+ * Generates a service interface definition.
+ *
+ * @param tservice The service to generate a header definition for
+ */
+void t_hs_generator::generate_service_interface(t_service* tservice) {
+  string f_iface_name = string(T_HS_DIR)+"/"+capitalize(service_name_)+"_Iface.hs";
+  f_iface_.open(f_iface_name.c_str());
+  indent(f_iface_) << "module " << capitalize(service_name_) << "_Iface where" << endl;
+
+  indent(f_iface_) <<
+    hs_imports() << endl << 
+    "import " << capitalize(program_name_) << "_Types" << endl << 
+    endl;
+  
+  if (tservice->get_extends() != NULL) {
+    string extends = type_name(tservice->get_extends());
+    indent(f_iface_) << "import " << extends <<"_Iface" << endl;
+    indent(f_iface_) << "class "<< extends << "_Iface a => " << capitalize(service_name_) << "_Iface a where" << endl;
+  } else {
+    f_iface_ << indent() << "class " << capitalize(service_name_) << "_Iface a where" << endl;
+  }
+  indent_up();
+
+  vector<t_function*> functions = tservice->get_functions();
+  vector<t_function*>::iterator f_iter; 
+  for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+    string ft = function_type(*f_iter,true,true,true);
+    f_iface_ <<
+      indent() << decapitalize((*f_iter)->get_name()) << " :: a -> " << ft  << endl;
+  }
+  indent_down();
+  f_iface_.close();
+
+}
+
+/**
+ * Generates a service client definition. Note that in Haskell, the client doesn't implement iface. This is because
+ * The client does not (and should not have to) deal with arguments being Nothing.
+ *
+ * @param tservice The service to generate a server for.
+ */
+void t_hs_generator::generate_service_client(t_service* tservice) {
+  string f_client_name = string(T_HS_DIR)+"/"+capitalize(service_name_)+"_Client.hs";
+  f_client_.open(f_client_name.c_str());
+
+  vector<t_function*> functions = tservice->get_functions();
+  vector<t_function*>::const_iterator f_iter;    
+
+  string extends = "";
+  string exports="";
+  bool first = true;
+  for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+    if(first)
+      first=false;
+    else
+      exports+=",";
+    string funname = (*f_iter)->get_name();
+    exports+=funname;
+  }
+  indent(f_client_) << "module " << capitalize(service_name_) << "_Client("<<exports<<") where" << endl;
+
+  if (tservice->get_extends() != NULL) {
+    extends = type_name(tservice->get_extends());
+    indent(f_client_) << "import " << extends << "_Client" << endl;
+  }
+  indent(f_client_) << "import Data.IORef" << endl;
+  indent(f_client_) << hs_imports() << endl;
+  indent(f_client_) << "import " << capitalize(program_name_) << "_Types" << endl;
+  indent(f_client_) << "import " << capitalize(service_name_) << endl;
+  // DATS RITE A GLOBAL VAR
+  indent(f_client_) << "seqid = newIORef 0" << endl;
+   
+
+  // Generate client method implementations
+
+  for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+    t_struct* arg_struct = (*f_iter)->get_arglist();
+    const vector<t_field*>& fields = arg_struct->get_members();
+    vector<t_field*>::const_iterator fld_iter;
+    string funname = (*f_iter)->get_name();
+
+    string fargs = "";
+    for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
+      fargs+= " arg_" + decapitalize((*fld_iter)->get_name());
+    }
+
+    // Open function
+    indent(f_client_) << funname << " (ip,op)" <<  fargs << " = do" << endl;
+    indent_up();
+    indent(f_client_) <<  "send_" << funname << " op" << fargs;
+
+    f_client_ << endl;
+    
+    if (!(*f_iter)->is_async()) {
+      f_client_ << indent();
+      f_client_ <<
+        "recv_" << funname << " ip" << endl;
+    }
+    indent_down();
+    
+    indent(f_client_) <<
+      "send_" << funname << " op" << fargs << " = do" << endl;
+    indent_up();
+    indent(f_client_) << "seq <- seqid" << endl;
+    indent(f_client_) << "seqn <- readIORef seq" << endl;
+    std::string argsname = capitalize((*f_iter)->get_name() + "_args");
+    
+    // Serialize the request header
+    f_client_ <<
+      indent() << "writeMessageBegin op (\"" << (*f_iter)->get_name() << "\", M_CALL, seqn)" << endl; 
+    f_client_ << indent() << "write_" << argsname << " op ("<<argsname<<"{";
+    bool first = true;
+    for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
+      if(first)
+        first=false;
+      else
+        f_client_ << ",";
+      f_client_ << "f_" << argsname <<"_" << (*fld_iter)->get_name() << "=Just arg_" << (*fld_iter)->get_name();
+    }
+    f_client_ << "})" << endl;
+    
+    // Write to the stream
+    f_client_ <<
+      indent() << "writeMessageEnd op" << endl <<
+      indent() << "pflush op" << endl;  
+    
+    indent_down();
+
+    if (!(*f_iter)->is_async()) {
+      std::string resultname = capitalize((*f_iter)->get_name() + "_result");
+      t_struct noargs(program_);
+
+      std::string funname = string("recv_") + (*f_iter)->get_name();
+      
+      t_function recv_function((*f_iter)->get_returntype(),
+                               funname,
+                               &noargs);
+      // Open function
+      f_client_ <<
+        indent() << funname << " ip = do" << endl;
+      indent_up(); // fun
+
+      // TODO(mcslee): Validate message reply here, seq ids etc.
+
+      f_client_ <<
+        indent() << "(fname, mtype, rseqid) <- readMessageBegin ip" << endl;
+      f_client_ <<
+        indent() << "if mtype == M_EXCEPTION then do" << endl <<
+        indent() << "  x <- readAppExn ip" << endl;
+      f_client_ <<
+        indent() << "  throwDyn x" << endl;
+      f_client_ <<
+        indent() << "  else return ()" << endl;
+
+      t_struct* xs = (*f_iter)->get_xceptions();
+      const std::vector<t_field*>& xceptions = xs->get_members();
+
+      f_client_ <<
+        indent() << "res <- read_" << resultname << " ip" << endl;
+      f_client_ <<
+        indent() << "readMessageEnd ip" << endl;
+
+      // Careful, only return _result if not a void function
+      if (!(*f_iter)->get_returntype()->is_void()) {
+        f_client_ <<
+          indent() << "case f_" << resultname << "_success res of" << endl;
+        indent_up(); // case
+        indent(f_client_) << "Just v -> return v" << endl;
+        indent(f_client_) << "Nothing -> do" << endl;
+        indent_up(); // none
+      }
+      
+      
+      vector<t_field*>::const_iterator x_iter;
+      for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
+        f_client_ <<
+          indent() << "case f_"<< resultname << "_" << (*x_iter)->get_name() << " res of" << endl;
+        indent_up(); //case
+        indent(f_client_) << "Nothing -> return ()" << endl;
+        indent(f_client_) << "Just _v -> throwDyn _v" << endl;
+        indent_down(); //-case
+      }
+      
+      // Careful, only return _result if not a void function
+      if ((*f_iter)->get_returntype()->is_void()) {
+        indent(f_client_) <<
+          "return ()" << endl;
+      } else {
+        f_client_ <<
+          indent() << "throwDyn (AppExn AE_MISSING_RESULT \"" << (*f_iter)->get_name() << " failed: unknown result\")" << endl;
+        indent_down(); //-none
+        indent_down(); //-case
+      }
+
+      // Close function
+      indent_down(); //-fun
+    }
+  }
+  f_client_.close();
+
+
+}
+
+/**
+ * Generates a service server definition.
+ *
+ * @param tservice The service to generate a server for.
+ */
+void t_hs_generator::generate_service_server(t_service* tservice) {
+  // Generate the dispatch methods
+  vector<t_function*> functions = tservice->get_functions();
+  vector<t_function*>::iterator f_iter; 
+
+  // Generate the process subfunctions
+  for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+    generate_process_function(tservice, *f_iter);
+  }
+
+
+  indent(f_service_) << "proc handler (iprot,oprot) (name,typ,seqid) = case name of" << endl;
+  indent_up();
+  for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+    string fname = (*f_iter)->get_name();
+    indent(f_service_) << "\""<<fname<<"\" -> process_" << decapitalize(fname) << " (seqid,iprot,oprot,handler)" << endl;
+  }
+  indent(f_service_) << "_ -> ";
+  if(tservice->get_extends() != NULL){
+    f_service_ << type_name(tservice->get_extends()) << ".proc handler (iprot,oprot) (name,typ,seqid)" << endl;
+  } else {
+    f_service_ << "do" << endl;
+    indent_up();
+    indent(f_service_) << "skip iprot T_STRUCT" << endl;
+    indent(f_service_) << "readMessageEnd iprot" << endl;
+    indent(f_service_) << "writeMessageBegin oprot (name,M_EXCEPTION,seqid)" << endl;
+    indent(f_service_) << "writeAppExn oprot (AppExn AE_UNKNOWN_METHOD (\"Unknown function \" ++ name))" << endl;
+    indent(f_service_) << "writeMessageEnd oprot" << endl;
+    indent(f_service_) << "pflush oprot" << endl;
+    indent_down();
+  }
+  indent_down();
+
+  // Generate the server implementation
+  indent(f_service_) <<
+    "process handler (iprot, oprot) = do" << endl;
+  indent_up();
+
+  f_service_ <<
+    indent() << "(name, typ, seqid) <- readMessageBegin iprot" << endl;
+  f_service_ << indent() << "proc handler (iprot,oprot) (name,typ,seqid)" << endl;
+  indent(f_service_) << "return True" << endl;
+  indent_down();
+
+}
+
+/**
+ * Generates a process function definition.
+ *
+ * @param tfunction The function to write a dispatcher for
+ */
+void t_hs_generator::generate_process_function(t_service* tservice,
+                                               t_function* tfunction) {
+  // Open function
+  indent(f_service_) <<
+    "process_" << tfunction->get_name() << " (seqid, iprot, oprot, handler) = do" << endl;
+  indent_up();
+
+  string argsname = capitalize(tfunction->get_name()) + "_args";
+  string resultname = capitalize(tfunction->get_name()) + "_result";
+
+  // Generate the function call
+  t_struct* arg_struct = tfunction->get_arglist();
+  const std::vector<t_field*>& fields = arg_struct->get_members();
+  vector<t_field*>::const_iterator f_iter;
+
+
+  f_service_ <<
+    indent() << "args <- read_" << argsname << " iprot" << endl;
+  f_service_ <<
+    indent() << "readMessageEnd iprot" << endl;
+
+  t_struct* xs = tfunction->get_xceptions();
+  const std::vector<t_field*>& xceptions = xs->get_members();
+  vector<t_field*>::const_iterator x_iter;
+  int n = xceptions.size();
+  if (!tfunction->is_async()){
+    if(!tfunction->get_returntype()->is_void()){
+      n++;
+    }
+    indent(f_service_) << "rs <- return (" << resultname;
+      
+    for(int i=0; i<n;i++){
+      f_service_ << " Nothing";
+    }
+    f_service_ << ")" << endl;
+  }
+
+  indent(f_service_) << "res <- ";
+  // Try block for a function with exceptions
+  if (xceptions.size() > 0) {
+    for(unsigned int i=0;i<xceptions.size();i++){
+      f_service_ << "(catchDyn" << endl;
+      indent_up();
+      f_service_ << indent();
+    }
+  }
+
+  f_service_ << "(do" << endl;
+  indent_up();
+  f_service_ << indent();
+  if (!tfunction->is_async() && !tfunction->get_returntype()->is_void()){
+    f_service_ << "res <- ";
+  }
+  f_service_ << "Iface." << tfunction->get_name() << " handler";
+  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+    f_service_ <<  " (f_" << argsname <<  "_" << (*f_iter)->get_name() << " args)";
+  }
+
+
+  if (!tfunction->is_async() && !tfunction->get_returntype()->is_void()){
+    f_service_ << endl;
+    indent(f_service_) << "return rs{f_"<<resultname<<"_success= Just res}";
+  } else if (!tfunction->is_async()){
+    f_service_ << endl;
+    indent(f_service_) << "return rs";
+  }
+  f_service_ << ")" << endl;
+  indent_down();
+  
+  if (xceptions.size() > 0 && !tfunction->is_async()) {
+    for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
+      indent(f_service_) << "(\\e  -> " <<endl;
+      indent_up();
+      if(!tfunction->is_async()){
+        f_service_ <<
+          indent() << "return rs{f_"<<resultname<<"_" << (*x_iter)->get_name() << " =Just e}";
+      } else {
+        indent(f_service_) << "return ()";
+      }
+      f_service_ << "))" << endl;
+      indent_down();
+      indent_down();
+    }
+  }
+
+
+
+  // Shortcut out here for async functions
+  if (tfunction->is_async()) {
+    f_service_ <<
+      indent() << "return ()" << endl;
+    indent_down();
+    return;
+  }
+
+  f_service_ <<
+    indent() << "writeMessageBegin oprot (\"" << tfunction->get_name() << "\", M_REPLY, seqid);" << endl <<
+    indent() << "write_"<<resultname<<" oprot res" << endl <<
+    indent() << "writeMessageEnd oprot" << endl <<
+    indent() << "pflush oprot" << endl;
+
+  // Close function
+  indent_down();
+}
+
+/**
+ * Deserializes a field of any type.
+ */
+void t_hs_generator::generate_deserialize_field(ofstream &out,
+                                                   t_field* tfield,
+                                                   string prefix){
+  t_type* type = tfield->get_type();
+  generate_deserialize_type(out,type);
+}
+
+
+/**
+ * Deserializes a field of any type.
+ */
+void t_hs_generator::generate_deserialize_type(ofstream &out,
+                                                   t_type* type){
+  while (type->is_typedef()) {
+    type = ((t_typedef*)type)->get_type();
+  }
+
+  if (type->is_void()) {
+    throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE";
+  }
+
+
+  if (type->is_struct() || type->is_xception()) {
+    generate_deserialize_struct(out,
+                                (t_struct*)type);
+  } else if (type->is_container()) {
+    generate_deserialize_container(out, type);
+  } else if (type->is_base_type()) {
+    t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+    switch (tbase) {
+    case t_base_type::TYPE_VOID:
+      throw "compiler error: cannot serialize void field in a struct";
+      break;
+    case t_base_type::TYPE_STRING:        
+      out << "readString";
+      break;
+    case t_base_type::TYPE_BOOL:
+      out << "readBool";
+      break;
+    case t_base_type::TYPE_BYTE:
+      out << "readByte";
+      break;
+    case t_base_type::TYPE_I16:
+      out << "readI16";
+      break;
+    case t_base_type::TYPE_I32:
+      out << "readI32";
+      break;
+    case t_base_type::TYPE_I64:
+      out << "readI64";
+      break;
+    case t_base_type::TYPE_DOUBLE:
+      out << "readDouble";
+      break;
+    default:
+      throw "compiler error: no PHP name for base type " + tbase;
+    }
+    out << " iprot";
+  } else if (type->is_enum()) {
+    string ename = capitalize(type->get_name());
+    out << "(do {i <- readI32 iprot; return (toEnum i :: " << ename << ")})";
+  } else {
+    printf("DO NOT KNOW HOW TO DESERIALIZE TYPE '%s'\n",
+           type->get_name().c_str());
+  }
+}
+  
+
+/**
+ * Generates an unserializer for a struct, calling read()
+ */
+void t_hs_generator::generate_deserialize_struct(ofstream &out,
+                                                  t_struct* tstruct) {
+  string name = capitalize(tstruct->get_name());
+  out << "(read_" << name << " iprot)";
+
+}
+
+/**
+ * Serialize a container by writing out the header followed by
+ * data and then a footer.
+ */
+void t_hs_generator::generate_deserialize_container(ofstream &out,
+                                                    t_type* ttype) {
+  string size = tmp("_size");
+  string ktype = tmp("_ktype");
+  string vtype = tmp("_vtype");
+  string etype = tmp("_etype");
+  string con = tmp("_con");
+  
+  t_field fsize(g_type_i32, size);
+  t_field fktype(g_type_byte, ktype);
+  t_field fvtype(g_type_byte, vtype);
+  t_field fetype(g_type_byte, etype);
+
+  // Declare variables, read header
+  if (ttype->is_map()) {
+    out << "(let {f 0 = return []; f n = do {k <- ";
+    generate_deserialize_type(out,((t_map*)ttype)->get_key_type());
+    out << "; v <- ";
+    generate_deserialize_type(out,((t_map*)ttype)->get_val_type());
+    out << ";r <- f (n-1); return $ (k,v):r}} in do {("<<ktype<<","<<vtype<<","<<size<<") <- readMapBegin iprot; l <- f " << size << "; return $ Map.fromList l})";
+  } else if (ttype->is_set()) {
+    out << "(let {f 0 = return []; f n = do {v <- ";
+    generate_deserialize_type(out,((t_map*)ttype)->get_key_type());
+    out << ";r <- f (n-1); return $ v:r}} in do {("<<etype<<","<<size<<") <- readSetBegin iprot; l <- f " << size << "; return $ Set.fromList l})";
+  } else if (ttype->is_list()) {
+    out << "(let {f 0 = return []; f n = do {v <- ";
+    generate_deserialize_type(out,((t_map*)ttype)->get_key_type());
+    out << ";r <- f (n-1); return $ v:r}} in do {("<<etype<<","<<size<<") <- readListBegin iprot; f " << size << "})";
+  }
+}
+
+
+/**
+ * Serializes a field of any type.
+ *
+ * @param tfield The field to serialize
+ * @param prefix Name to prepend to field name
+ */
+void t_hs_generator::generate_serialize_field(ofstream &out,
+                                                 t_field* tfield,
+                                                 string name) {
+  t_type* type = tfield->get_type();
+  while (type->is_typedef()) {
+    type = ((t_typedef*)type)->get_type();
+  }
+
+  // Do nothing for void types
+  if (type->is_void()) {
+    throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " +
+      tfield->get_name();
+  }
+
+  if(name.length() == 0){
+    name = decapitalize(tfield->get_name());
+  }  
+
+  if (type->is_struct() || type->is_xception()) {
+    generate_serialize_struct(out,
+                              (t_struct*)type,
+                              name);
+  } else if (type->is_container()) {
+    generate_serialize_container(out,
+                                 type,
+                                 name);
+  } else if (type->is_base_type() || type->is_enum()) {
+    if (type->is_base_type()) {
+      t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+      switch (tbase) {
+      case t_base_type::TYPE_VOID:
+        throw
+          "compiler error: cannot serialize void field in a struct: " + name;
+        break;
+      case t_base_type::TYPE_STRING:
+        out << "writeString oprot " << name;
+        break;
+      case t_base_type::TYPE_BOOL:
+        out << "writeBool oprot " << name;
+       break;
+      case t_base_type::TYPE_BYTE:
+        out << "writeByte oprot " << name;
+        break;
+      case t_base_type::TYPE_I16:
+        out << "writeI16 oprot " << name;
+        break;
+      case t_base_type::TYPE_I32:
+        out << "writeI32 oprot " << name;
+        break;
+      case t_base_type::TYPE_I64:
+        out << "writeI64 oprot " << name;
+        break;
+      case t_base_type::TYPE_DOUBLE:
+        out << "writeDouble oprot " << name;
+        break;
+      default:
+        throw "compiler error: no hs name for base type " + tbase;
+      }
+    
+    } else if (type->is_enum()) {
+      string ename = capitalize(type->get_name());
+      out << "writeI32 oprot (fromEnum "<< name << ")";
+    }
+    
+  } else {
+    printf("DO NOT KNOW HOW TO SERIALIZE FIELD '%s' TYPE '%s'\n",
+           tfield->get_name().c_str(),
+           type->get_name().c_str());
+  }
+}
+
+/**
+ * Serializes all the members of a struct.
+ *
+ * @param tstruct The struct to serialize
+ * @param prefix  String prefix to attach to all fields
+ */
+void t_hs_generator::generate_serialize_struct(ofstream &out,
+                                               t_struct* tstruct,
+                                               string prefix) {
+  out << "write_" << type_name(tstruct) << " oprot " << prefix;
+}
+
+void t_hs_generator::generate_serialize_container(ofstream &out,
+                                                  t_type* ttype,
+                                                  string prefix) {
+  if (ttype->is_map()) {
+    string k = tmp("_kiter");
+    string v = tmp("_viter");
+    out << "(let {f [] = return (); f (("<<k<<","<<v<<"):t) = do {";
+    generate_serialize_map_element(out, (t_map*)ttype, k, v);
+    out << ";f t}} in do {writeMapBegin oprot ("<< type_to_enum(((t_map*)ttype)->get_key_type())<<","<< type_to_enum(((t_map*)ttype)->get_val_type())<<",Map.size " << prefix << "); f (Map.toList " << prefix << ");writeMapEnd oprot})";
+  } else if (ttype->is_set()) {
+    string v = tmp("_viter");
+    out << "(let {f [] = return (); f ("<<v<<":t) = do {";
+    generate_serialize_set_element(out, (t_set*)ttype, v);
+    out << ";f t}} in do {writeSetBegin oprot ("<< type_to_enum(((t_set*)ttype)->get_elem_type())<<",Set.size " << prefix << "); f (Set.toList " << prefix << ");writeSetEnd oprot})";
+  } else if (ttype->is_list()) {
+    string v = tmp("_viter");
+    out << "(let {f [] = return (); f ("<<v<<":t) = do {";
+    generate_serialize_list_element(out, (t_list*)ttype, v);
+    out << ";f t}} in do {writeListBegin oprot ("<< type_to_enum(((t_list*)ttype)->get_elem_type())<<",length " << prefix << "); f " << prefix << ";writeListEnd oprot})";
+  }
+
+}
+
+/**
+ * Serializes the members of a map.
+ *
+ */
+void t_hs_generator::generate_serialize_map_element(ofstream &out,
+                                                     t_map* tmap,
+                                                     string kiter,
+                                                     string viter) {
+  t_field kfield(tmap->get_key_type(), kiter);
+  out << "do {";
+  generate_serialize_field(out, &kfield);
+  out << ";";
+  t_field vfield(tmap->get_val_type(), viter);
+  generate_serialize_field(out, &vfield);
+  out << "}";
+}
+
+/**
+ * Serializes the members of a set.
+ */
+void t_hs_generator::generate_serialize_set_element(ofstream &out,
+                                                     t_set* tset,
+                                                     string iter) {
+  t_field efield(tset->get_elem_type(), iter);
+  generate_serialize_field(out, &efield);
+}
+
+/**
+ * Serializes the members of a list.
+ */
+void t_hs_generator::generate_serialize_list_element(ofstream &out,
+                                                      t_list* tlist,
+                                                      string iter) {
+  t_field efield(tlist->get_elem_type(), iter);
+  generate_serialize_field(out, &efield);
+}
+
+
+string t_hs_generator::function_type(t_function* tfunc, bool options, bool io, bool method){
+  string result="";
+  
+  const vector<t_field*>& fields = tfunc->get_arglist()->get_members();
+  vector<t_field*>::const_iterator f_iter;
+  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+    if(options)
+      result += "Maybe (";
+    result += render_hs_type((*f_iter)->get_type());
+    if(options)
+      result+=")";
+    result += " -> ";
+  }
+  if(fields.empty() && !method){
+    result += "() -> ";
+  }
+  if(io) result += "IO (";
+  result += render_hs_type(tfunc->get_returntype());
+  if(io) result += ")";
+  return result;
+}
+
+
+string t_hs_generator::type_name(t_type* ttype) {
+  string prefix = "";
+  t_program* program = ttype->get_program();
+  if (program != NULL && program != program_) {
+    if (!ttype->is_service()) {
+      prefix = capitalize(program->get_name()) + "_Types.";
+    }
+  }
+
+  string name = ttype->get_name();
+  if(ttype->is_service()){
+    name = capitalize(name);
+  } else {
+    name = capitalize(name);
+  }
+  return prefix + name;
+}
+
+/**
+ * Converts the parse type to a Protocol.t_type enum
+ */
+string t_hs_generator::type_to_enum(t_type* type) {
+  while (type->is_typedef()) {
+    type = ((t_typedef*)type)->get_type();
+  }
+  
+  if (type->is_base_type()) {
+    t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+    switch (tbase) {
+    case t_base_type::TYPE_VOID:
+      return "T_VOID";
+    case t_base_type::TYPE_STRING:
+      return "T_STRING";
+    case t_base_type::TYPE_BOOL:
+      return "T_BOOL";
+    case t_base_type::TYPE_BYTE:
+      return "T_BYTE";
+    case t_base_type::TYPE_I16:
+      return "T_I16";
+    case t_base_type::TYPE_I32:
+      return "T_I32";
+    case t_base_type::TYPE_I64:
+      return "T_I64";
+    case t_base_type::TYPE_DOUBLE:
+      return "T_DOUBLE";
+    }
+  } else if (type->is_enum()) {
+    return "T_I32";
+  } else if (type->is_struct() || type->is_xception()) {
+    return "T_STRUCT";
+  } else if (type->is_map()) {
+    return "T_MAP";
+  } else if (type->is_set()) {
+    return "T_SET";
+  } else if (type->is_list()) {
+    return "T_LIST";
+  }
+
+  throw "INVALID TYPE IN type_to_enum: " + type->get_name();
+}
+
+/**
+ * Converts the parse type to an haskell type
+ */
+string t_hs_generator::render_hs_type(t_type* type) {
+  while (type->is_typedef()) {
+    type = ((t_typedef*)type)->get_type();
+  }
+  
+  if (type->is_base_type()) {
+    t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+    switch (tbase) {
+    case t_base_type::TYPE_VOID:
+      return "()";
+    case t_base_type::TYPE_STRING:
+      return "[Char]";
+    case t_base_type::TYPE_BOOL:
+      return "Bool";
+    case t_base_type::TYPE_BYTE:
+      return "Int";
+    case t_base_type::TYPE_I16:
+      return "Int";
+    case t_base_type::TYPE_I32:
+      return "Int";
+    case t_base_type::TYPE_I64:
+      return "Int";
+    case t_base_type::TYPE_DOUBLE:
+      return "Double";
+    }
+  } else if (type->is_enum()) {
+    return capitalize(((t_enum*)type)->get_name());
+  } else if (type->is_struct() || type->is_xception()) {
+    return type_name((t_struct*)type);
+  } else if (type->is_map()) {
+    t_type* ktype = ((t_map*)type)->get_key_type();
+    t_type* vtype = ((t_map*)type)->get_val_type();
+    return "Map.Map ("+ render_hs_type(ktype)+") ("+render_hs_type(vtype)+")";
+  } else if (type->is_set()) {
+    t_type* etype = ((t_set*)type)->get_elem_type();
+    return "Set.Set ("+render_hs_type(etype)+")";
+  } else if (type->is_list()) {
+    t_type* etype = ((t_list*)type)->get_elem_type();
+    return "["+render_hs_type(etype)+"]";
+  }
+
+  throw "INVALID TYPE IN type_to_enum: " + type->get_name();
+}
diff --git a/compiler/cpp/src/generate/t_hs_generator.h b/compiler/cpp/src/generate/t_hs_generator.h
new file mode 100644
index 0000000..41a59b1
--- /dev/null
+++ b/compiler/cpp/src/generate/t_hs_generator.h
@@ -0,0 +1,144 @@
+// Copyright (c) 2006- Facebook
+// Distributed under the Thrift Software License
+//
+// See accompanying file LICENSE or visit the Thrift site at:
+// http://developers.facebook.com/thrift/
+
+#ifndef T_HS_GENERATOR_H
+#define T_HS_GENERATOR_H
+
+#include <string>
+#include <fstream>
+#include <iostream>
+#include <vector>
+
+#include "t_oop_generator.h"
+
+#define T_HS_DIR "gen-hs"
+
+/**
+ * Haskell code generator.
+ *
+ * @author Iain Proctor <iproctor@facebook.com>
+ */
+class t_hs_generator : public t_oop_generator {
+ public:
+  t_hs_generator(t_program* program) :
+    t_oop_generator(program) {}
+
+  /**
+   * Init and close methods
+   */
+
+  void init_generator();
+  void close_generator();
+
+  /**
+   * Program-level generation functions
+   */
+  void generate_program  ();
+  void generate_typedef  (t_typedef*  ttypedef);
+  void generate_enum     (t_enum*     tenum);
+  void generate_const    (t_const*    tconst);
+  void generate_struct   (t_struct*   tstruct);
+  void generate_xception (t_struct*   txception);
+  void generate_service  (t_service*  tservice);
+
+  std::string render_const_value(t_type* type, t_const_value* value);
+
+  /**
+   * Struct generation code
+   */
+
+  void generate_hs_struct(t_struct* tstruct, bool is_exception);
+  void generate_hs_struct_definition(std::ofstream &out,t_struct* tstruct, bool is_xception=false,bool helper=false);
+  void generate_hs_struct_reader(std::ofstream& out, t_struct* tstruct);
+  void generate_hs_struct_writer(std::ofstream& out, t_struct* tstruct);
+  void generate_hs_function_helpers(t_function* tfunction);
+
+  /**
+   * Service-level generation functions
+   */
+
+  void generate_service_helpers   (t_service*  tservice);
+  void generate_service_interface (t_service* tservice);
+  void generate_service_client    (t_service* tservice);
+  void generate_service_server    (t_service* tservice);
+  void generate_process_function  (t_service* tservice, t_function* tfunction);
+
+  /**
+   * Serialization constructs
+   */
+
+  void generate_deserialize_field        (std::ofstream &out,
+                                          t_field*    tfield,
+                                          std::string prefix);
+  
+  void generate_deserialize_struct       (std::ofstream &out,
+                                          t_struct*   tstruct);
+  
+  void generate_deserialize_container    (std::ofstream &out,
+                                          t_type*     ttype);
+  
+  void generate_deserialize_set_element  (std::ofstream &out,
+                                          t_set*      tset);
+
+
+  void generate_deserialize_list_element (std::ofstream &out,
+                                          t_list*     tlist,
+                                          std::string prefix="");
+  void generate_deserialize_type          (std::ofstream &out,
+                                           t_type* type);
+
+  void generate_serialize_field          (std::ofstream &out,
+                                          t_field*    tfield,
+                                          std::string name= "");
+
+  void generate_serialize_struct         (std::ofstream &out,
+                                          t_struct*   tstruct,
+                                          std::string prefix="");
+
+  void generate_serialize_container      (std::ofstream &out,
+                                          t_type*     ttype,
+                                          std::string prefix="");
+
+  void generate_serialize_map_element    (std::ofstream &out,
+                                          t_map*      tmap,
+                                          std::string kiter,
+                                          std::string viter);
+
+  void generate_serialize_set_element    (std::ofstream &out,
+                                          t_set*      tmap,
+                                          std::string iter);
+
+  void generate_serialize_list_element   (std::ofstream &out,
+                                          t_list*     tlist,
+                                          std::string iter);
+
+  /**
+   * Helper rendering functions
+   */
+
+  std::string hs_autogen_comment();
+  std::string hs_imports();
+  std::string type_name(t_type* ttype);
+  std::string function_type(t_function* tfunc, bool options = false, bool io = false, bool method = false);
+  std::string type_to_enum(t_type* ttype);
+  std::string render_hs_type(t_type* type);
+
+
+ private:
+
+  /**
+   * File streams
+   */
+
+  std::ofstream f_types_;
+  std::ofstream f_consts_;
+  std::ofstream f_service_;
+  std::ofstream f_iface_;
+  std::ofstream f_client_;
+
+};
+
+#endif
diff --git a/compiler/cpp/src/main.cc b/compiler/cpp/src/main.cc
index b2abb9b..4e8a27a 100644
--- a/compiler/cpp/src/main.cc
+++ b/compiler/cpp/src/main.cc
@@ -37,6 +37,7 @@
 #include "generate/t_perl_generator.h"
 #include "generate/t_ocaml_generator.h"
 #include "generate/t_erl_generator.h"
+#include "generate/t_hs_generator.h"
 
 using namespace std;
 
@@ -129,6 +130,7 @@
 bool gen_perl = false;
 bool gen_ocaml = false;
 bool gen_erl = false;
+bool gen_hs = false;
 bool gen_recurse = false;
 
 /**
@@ -308,6 +310,7 @@
   fprintf(stderr, "  -perl       Generate Perl output files\n");
   fprintf(stderr, "  -ocaml      Generate OCaml output files\n");
   fprintf(stderr, "  -erl        Generate Erlang output files\n");
+  fprintf(stderr, "  -hs         Generate Haskell output files\n");
   fprintf(stderr, "  -I dir      Add a directory to the list of directories \n");
   fprintf(stderr, "                searched for include directives\n");
   fprintf(stderr, "  -nowarn     Suppress all compiler warnings (BAD!)\n");
@@ -583,7 +586,12 @@
       erl->generate_program();
       delete erl;
     }
-
+    if (gen_hs) {
+      pverbose("Generating Haskell\n");
+      t_hs_generator* hs = new t_hs_generator(program);
+      hs->generate_program();
+      delete hs;
+    }
 
   } catch (string s) {
     printf("Error: %s\n", s.c_str());
@@ -653,6 +661,8 @@
         gen_ocaml = true;
       } else if (strcmp(arg, "-erl") == 0) {
         gen_erl = true;
+      } else if (strcmp(arg, "-hs") == 0) {
+        gen_hs = true;
       } else if (strcmp(arg, "-I") == 0) {
         // An argument of "-I\ asdf" is invalid and has unknown results
         arg = argv[++i];
@@ -673,7 +683,7 @@
   }
 
   // You gotta generate something!
-  if (!gen_cpp && !gen_java && !gen_php && !gen_phpi && !gen_py && !gen_rb && !gen_xsd && !gen_perl && !gen_ocaml && !gen_erl) {
+  if (!gen_cpp && !gen_java && !gen_php && !gen_phpi && !gen_py && !gen_rb && !gen_xsd && !gen_perl && !gen_ocaml && !gen_erl && !gen_hs) {
     fprintf(stderr, "!!! No output language(s) specified\n\n");
     usage();
   }
diff --git a/lib/hs/README b/lib/hs/README
new file mode 100644
index 0000000..d7b2232
--- /dev/null
+++ b/lib/hs/README
@@ -0,0 +1,22 @@
+Haskell Thrift Bindings
+
+Running: you need -fglasgow-exts.
+
+Enums: become haskell data types. Use fromEnum to get out the int value.
+
+Structs: become records. Field labels are ugly, of the form f_STRUCTNAME_FIELDNAME. All fields are Maybe types.
+
+Exceptions: identical to structs. Throw them with throwDyn. Catch them with catchDyn.
+
+Client: just a bunch of functions. You may have to import a bunch of client files to deal with inheritance.
+
+Interface: You should only have to import the last one in the chain of inheritors. To make an interface, declare a label:
+data MyIface = MyIface
+and then declare it an instance of each iface class, starting with the superest class and proceding down (all the while defining the methods).
+Then pass your label to process as the handler.
+
+Processor: Just a function that takes a handler label, protocols. It calls the superclasses process if there is a superclass.
+
+
+Note: Protocols implement flush as well as transports and do not have a getTransport method. This is because I couldn't get getTransport to typecheck. Shrug.
+
diff --git a/lib/hs/TODO b/lib/hs/TODO
new file mode 100644
index 0000000..1368173
--- /dev/null
+++ b/lib/hs/TODO
@@ -0,0 +1,2 @@
+The library could stand to be built up more.
+Many modules need export lists.
diff --git a/lib/hs/src/TBinaryProtocol.hs b/lib/hs/src/TBinaryProtocol.hs
new file mode 100644
index 0000000..dd5212d
--- /dev/null
+++ b/lib/hs/src/TBinaryProtocol.hs
@@ -0,0 +1,110 @@
+module TBinaryProtocol (TBinaryProtocol(..)) where
+    import Thrift
+    import Data.Bits
+    import Data.Int
+    import GHC.Exts
+    import GHC.Prim
+    import GHC.Word
+    import Control.Exception
+
+    data TBinaryProtocol a = (TTransport a) => TBinaryProtocol a
+
+    version_mask = 0xffff0000
+    version_1 = 0x80010000;
+    
+    getByte i b= 255 .&. (shiftR i (8*b))
+    getBytes i 0 = []
+    getBytes i n = (toEnum (getByte i (n-1)) :: Char):(getBytes i (n-1))
+    
+    floatBits :: Double -> Word64
+    floatBits (D# d#) = W64# (unsafeCoerce# d#)
+
+    floatOfBits :: Word64 -> Double
+    floatOfBits (W64# b#) = D# (unsafeCoerce# b#)
+
+    composeBytesH :: [Char] -> Int -> Word32
+    composeBytesH [] n = 0
+    composeBytesH (h:t) n = (shiftL (fromIntegral (fromEnum h) :: Word32) (8*n)) .|. (composeBytesH t (n-1))
+    compBytes :: [Char] -> Word32
+    compBytes b = composeBytesH b ((length b)-1)
+
+    composeBytes64H :: [Char] -> Int -> Word64
+    composeBytes64H [] n = 0
+    composeBytes64H (h:t) n = (shiftL (fromIntegral (fromEnum h) :: Word64) (8*n)) .|. (composeBytes64H t (n-1))
+    compBytes64 :: [Char] -> Word64
+    compBytes64 b = composeBytes64H b ((length b)-1)
+    instance TTransport a =>  Protocol (TBinaryProtocol a) where
+                       writeBool (TBinaryProtocol tr) b = twrite tr (if b then [toEnum 1::Char] else [toEnum 0::Char])
+                       writeByte (TBinaryProtocol tr) b = twrite tr (getBytes b 1)
+                       writeI16 (TBinaryProtocol tr) b = twrite tr (getBytes b 2)
+                       writeI32 (TBinaryProtocol tr) b = twrite tr (getBytes b 4)
+                       writeI64 (TBinaryProtocol tr) b = twrite tr (getBytes b 8)
+                       writeDouble (TBinaryProtocol tr) b = writeI64 (TBinaryProtocol tr) (fromIntegral (floatBits b) :: Int)
+                       writeString (TBinaryProtocol tr) s = do twrite tr (getBytes (length s) 4)
+                                                               twrite tr s
+                       writeBinary = writeString
+                       writeMessageBegin (TBinaryProtocol tr) (n,t,s) = do writeI32 (TBinaryProtocol tr) (version_1 .|. (fromEnum t))
+                                                                           writeString (TBinaryProtocol tr) n                                                                           
+                                                                           writeI32 (TBinaryProtocol tr) s
+                       writeMessageEnd (TBinaryProtocol tr) = return ()
+                       writeStructBegin (TBinaryProtocol tr) s = return ()
+                       writeStructEnd (TBinaryProtocol tr) = return ()
+                       writeFieldBegin a (n,t,i) = do writeByte a (fromEnum t)
+                                                      writeI16 a i
+                       writeFieldEnd a = return ()
+                       writeFieldStop a = writeByte a (fromEnum T_STOP)
+                       writeMapBegin a (k,v,s) = do writeByte a (fromEnum k)
+                                                    writeByte a (fromEnum v)
+                                                    writeI32 a s
+                       writeMapEnd a = return ()
+                       writeListBegin a (t,s) = do writeByte a (fromEnum t)
+                                                   writeI32 a s
+                       writeListEnd a = return ()
+                       writeSetBegin = writeListBegin
+                       writeSetEnd a = return ()
+                       readByte (TBinaryProtocol tr) = do b <- treadAll tr 1
+                                                          return $ (fromIntegral (fromIntegral (compBytes b) :: Int8) :: Int)
+                       readI16 (TBinaryProtocol tr) = do b <- treadAll tr 2
+                                                         return $ (fromIntegral (fromIntegral (compBytes b) :: Int16) :: Int)
+                       readI32 (TBinaryProtocol tr) = do b <- treadAll tr 4
+                                                         return $ (fromIntegral (fromIntegral (compBytes b) :: Int32) :: Int)
+                       readI64 (TBinaryProtocol tr) = do b <- treadAll tr 8
+                                                         return $ (fromIntegral (fromIntegral (compBytes64 b) :: Int64) :: Int)
+                       readDouble (TBinaryProtocol tr) = do b <- readI64 (TBinaryProtocol tr)
+                                                            return $ floatOfBits (fromIntegral b :: Word64)
+                       readBool (TBinaryProtocol tr) = do b <- readByte (TBinaryProtocol tr)
+                                                          return $ b == 1
+                       readString (TBinaryProtocol tr) = do l <- readI32 (TBinaryProtocol tr)
+                                                            treadAll tr l
+                       readBinary = readString
+                       readMessageBegin (TBinaryProtocol tr) = do ver <- readI32 (TBinaryProtocol tr)
+                                                                  if (ver .&. version_mask /= version_1) then
+                                                                      throwDyn (ProtocolExn PE_BAD_VERSION "Missing version identifier")
+                                                                      else do
+                                                                        s <- readString (TBinaryProtocol tr)                                                                  
+                                                                        sz <- readI32 (TBinaryProtocol tr)
+                                                                        return (s,toEnum (ver .&. 0xFF) :: Message_type,fromIntegral sz :: Int)
+                       readMessageEnd (TBinaryProtocol tr) = return ()
+                       readStructBegin (TBinaryProtocol tr) = return ""
+                       readStructEnd (TBinaryProtocol tr) = return ()
+                       readFieldBegin (TBinaryProtocol tr) = do t <- readByte (TBinaryProtocol tr)
+                                                                if (toEnum t :: T_type) /= T_STOP then
+                                                                    do s <- readI16 (TBinaryProtocol tr)
+                                                                       return ("",toEnum t :: T_type,fromIntegral s :: Int)
+                                                                    else return ("",toEnum t :: T_type,0)
+                       readFieldEnd (TBinaryProtocol tr) = return ()
+                       readMapBegin a = do kt <- readByte a
+                                           vt <- readByte a
+                                           s <- readI32 a
+                                           return (toEnum kt :: T_type,toEnum vt :: T_type,fromIntegral s :: Int)
+                       readMapEnd a = return ()
+                       readListBegin a = do b <- readByte a
+                                            s <- readI32 a
+                                            return (toEnum b :: T_type,fromIntegral s :: Int)
+                       readListEnd a = return ()
+                       readSetBegin = readListBegin
+                       readSetEnd = readListEnd
+                       pflush (TBinaryProtocol tr) = tflush tr
+
+                                                           
+
diff --git a/lib/hs/src/TChannelTransport.hs b/lib/hs/src/TChannelTransport.hs
new file mode 100644
index 0000000..df1aedc
--- /dev/null
+++ b/lib/hs/src/TChannelTransport.hs
@@ -0,0 +1,22 @@
+module TChannelTransport(TChannelTrans(..)) where
+import System.IO
+import IO
+import Thrift
+import Control.Exception
+data TChannelTrans = TChannelTrans (Handle)
+
+instance TTransport TChannelTrans where
+    tisOpen a = True
+    topen a = return a
+    tclose a = return a
+    tread a 0 = return []
+    tread (TChannelTrans h) i = Prelude.catch 
+                                 (do c <- hGetChar h
+                                     t <- tread (TChannelTrans h) (i-1)
+                                     return $ c:t)
+                                 (\e -> if isEOFError e then return [] else throwDyn (TransportExn "TChannelTransport: Could not read" TE_UNKNOWN))
+    twrite a [] = return ()
+    twrite (TChannelTrans h) (c:t) = do hPutChar h c
+                                        twrite (TChannelTrans h) t
+    tflush (TChannelTrans h) = hFlush h
+
diff --git a/lib/hs/src/TServer.hs b/lib/hs/src/TServer.hs
new file mode 100644
index 0000000..c71882c
--- /dev/null
+++ b/lib/hs/src/TServer.hs
@@ -0,0 +1,29 @@
+module TServer(run_basic_server,run_threaded_server) where
+
+import Network
+import Thrift
+import Control.Exception
+import TBinaryProtocol
+import TChannelTransport
+import Control.Concurrent
+
+proc_loop hand proc ps = do v <-proc hand ps
+                            if v then proc_loop hand proc ps
+                                else return ()
+
+accept_loop hand sock proc transgen iprotgen oprotgen = 
+    do (h,hn,_) <- accept sock
+       let t = transgen h 
+       let ip = iprotgen t
+       let op = oprotgen t
+       forkIO (handle (\e -> return ()) (proc_loop hand proc (ip,op)))
+       accept_loop hand sock proc transgen iprotgen oprotgen
+       
+run_threaded_server hand proc port transgen iprotgen oprotgen = 
+    do sock <- listenOn (PortNumber port)
+       accept_loop hand sock proc transgen iprotgen oprotgen
+       return ()
+       
+
+-- A basic threaded binary protocol socket server.
+run_basic_server hand proc port = run_threaded_server hand proc port TChannelTrans TBinaryProtocol TBinaryProtocol
diff --git a/lib/hs/src/TSocket.hs b/lib/hs/src/TSocket.hs
new file mode 100644
index 0000000..7e72878
--- /dev/null
+++ b/lib/hs/src/TSocket.hs
@@ -0,0 +1,33 @@
+module TSocket(TSocket(..)) where
+import Thrift
+import Data.IORef
+import Network
+import IO
+import Control.Exception
+data TSocket = TSocket{host::[Char],port::PortNumber,chan :: Maybe Handle}
+
+instance TTransport TSocket where
+    tisOpen a = case chan a of
+                  Just _ -> True
+                  Nothing -> False
+    topen a = do h <- connectTo (host a) (PortNumber (port a))
+                 return $ (a{chan = Just h}) 
+    tclose a = case chan a of 
+                 Just h -> do hClose h
+                              return $ a{chan=Nothing}
+                 Nothing -> return a
+    tread a 0 = return []
+    tread a n = case chan a of 
+                  Just h -> handle (\e -> throwDyn (TransportExn "TSocket: Could not read." TE_UNKNOWN))
+                      (do c <- hGetChar h 
+                          l <- tread a (n-1)
+                          return $ c:l)
+                  Nothing -> return []
+    twrite a s = case chan a of
+                   Just h -> hPutStr h s
+                   Nothing -> return ()
+    tflush a = case chan a of
+                 Just h -> hFlush h
+                 Nothing -> return ()
+
+
diff --git a/lib/hs/src/Thrift.hs b/lib/hs/src/Thrift.hs
new file mode 100644
index 0000000..4087b2b
--- /dev/null
+++ b/lib/hs/src/Thrift.hs
@@ -0,0 +1,298 @@
+module Thrift (TransportExn(..),TransportExn_Type(..),TTransport(..), T_type(..), Message_type(..), Protocol(..), AE_type(..), AppExn(..), readAppExn,writeAppExn,Thrift_exception(..), ProtocolExn(..), PE_type(..)) where
+  import Data.Generics
+  import Data.Int
+  import Control.Exception
+
+  data Thrift_exception = Thrift_Error deriving Typeable
+
+  data TransportExn_Type = TE_UNKNOWN
+                          | TE_NOT_OPEN
+                          | TE_ALREADY_OPEN
+                          | TE_TIMED_OUT
+                          | TE_END_OF_FILE
+                            deriving (Eq,Typeable,Show)
+
+  data TransportExn = TransportExn [Char] TransportExn_Type  deriving (Show,Typeable)
+
+  class TTransport a where
+      tisOpen :: a -> Bool
+      topen :: a -> IO a
+      tclose :: a -> IO a
+      tread :: a  -> Int -> IO [Char]
+      twrite :: a -> [Char] ->IO ()
+      tflush :: a -> IO ()
+      treadAll :: a -> Int -> IO [Char]
+      treadAll a 0 = return []
+      treadAll a len =
+          do ret <- tread a len
+             case ret of
+               [] -> throwDyn (TransportExn "Cannot read. Remote side has closed." TE_UNKNOWN)
+               _ -> do
+                   rl <- return (length ret)
+                   if len <= rl then
+                       return ret
+                       else do r <- treadAll a (len-rl)
+                               return (ret++r)
+            
+  
+  data T_type = T_STOP 
+              | T_VOID     
+              | T_BOOL
+              | T_BYTE
+              | T_I08 
+              | T_I16 
+              | T_I32 
+              | T_U64 
+              | T_I64 
+              | T_DOUBLE 
+              | T_STRING 
+              | T_UTF7   
+              | T_STRUCT    
+              | T_MAP       
+              | T_SET       
+              | T_LIST      
+              | T_UTF8      
+              | T_UTF16
+              | T_UNKNOWN
+                deriving (Eq)
+  instance Enum T_type where
+      fromEnum t = case t of
+                     T_STOP       -> 0
+                     T_VOID       -> 1
+                     T_BOOL       -> 2
+                     T_BYTE       -> 3
+                     T_I08        -> 3
+                     T_I16        -> 6
+                     T_I32        -> 8
+                     T_U64        -> 9
+                     T_I64        -> 10
+                     T_DOUBLE     -> 4
+                     T_STRING     -> 11
+                     T_UTF7       -> 11
+                     T_STRUCT     -> 12
+                     T_MAP        -> 13
+                     T_SET        -> 14
+                     T_LIST       -> 15
+                     T_UTF8       -> 16
+                     T_UTF16      -> 17
+                     T_UNKNOWN    -> -1                       
+      toEnum t = case t of 
+                   0 -> T_STOP      
+                   1 -> T_VOID      
+                   2 -> T_BOOL
+                   3 -> T_BYTE
+                   6->  T_I16       
+                   8 -> T_I32      
+                   9 -> T_U64      
+                   10 -> T_I64     
+                   4 -> T_DOUBLE   
+                   11 -> T_STRING
+                   12 -> T_STRUCT
+                   13 -> T_MAP   
+                   14 -> T_SET   
+                   15 -> T_LIST  
+                   16 -> T_UTF8  
+                   17 -> T_UTF16
+                   _ -> T_UNKNOWN
+  
+  
+  data Message_type = M_CALL
+                    | M_REPLY
+                    | M_EXCEPTION
+                    | M_UNKNOWN
+                      deriving Eq
+  instance Enum Message_type where
+                      
+      fromEnum t = case t of
+                     M_CALL -> 1
+                     M_REPLY -> 2
+                     M_EXCEPTION -> 3
+                     M_UNKNOWN -> -1
+                          
+      toEnum t = case t of
+                   1 -> M_CALL
+                   2 -> M_REPLY
+                   3 -> M_EXCEPTION
+                   _ -> M_UNKNOWN
+  
+  
+  
+  
+  class Protocol a where
+      pflush ::  a -> IO ()
+      writeMessageBegin :: a -> ([Char],Message_type,Int) -> IO ()
+      writeMessageEnd :: a -> IO ()
+      writeStructBegin :: a -> [Char] -> IO ()
+      writeStructEnd :: a -> IO ()
+      writeFieldBegin :: a -> ([Char], T_type,Int) -> IO ()
+      writeFieldEnd :: a -> IO ()
+      writeFieldStop :: a -> IO ()
+      writeMapBegin :: a -> (T_type,T_type,Int) -> IO ()
+      writeMapEnd :: a -> IO ()
+      writeListBegin :: a -> (T_type,Int) -> IO ()
+      writeListEnd :: a -> IO ()
+      writeSetBegin :: a -> (T_type,Int) -> IO ()
+      writeSetEnd :: a -> IO ()
+      writeBool :: a -> Bool -> IO ()
+      writeByte :: a -> Int -> IO ()
+      writeI16 :: a -> Int -> IO ()
+      writeI32 :: a -> Int -> IO ()
+      writeI64 :: a -> Int -> IO ()
+      writeDouble :: a -> Double -> IO ()
+      writeString :: a -> [Char] -> IO ()
+      writeBinary :: a -> [Char] -> IO ()
+      readMessageBegin :: a -> IO ([Char],Message_type,Int)
+      readMessageEnd :: a -> IO ()
+      readStructBegin :: a -> IO [Char]
+      readStructEnd :: a -> IO ()
+      readFieldBegin :: a -> IO ([Char],T_type,Int)
+      readFieldEnd :: a -> IO ()
+      readMapBegin :: a -> IO (T_type,T_type,Int)
+      readMapEnd :: a -> IO ()
+      readListBegin :: a -> IO (T_type,Int)
+      readListEnd :: a -> IO ()
+      readSetBegin :: a -> IO (T_type,Int)
+      readSetEnd :: a -> IO ()
+      readBool :: a -> IO Bool
+      readByte :: a -> IO Int
+      readI16 :: a -> IO Int
+      readI32 :: a -> IO Int
+      readI64 :: a -> IO Int
+      readDouble :: a -> IO Double
+      readString :: a -> IO [Char]
+      readBinary :: a -> IO [Char]
+      skipFields :: a -> IO ()
+      skipMapEntries :: a -> Int -> T_type -> T_type -> IO ()
+      skipSetEntries :: a -> Int -> T_type -> IO ()
+      skip :: a -> T_type -> IO ()
+      skipFields a = do (_,ty,_) <- readFieldBegin a
+                        if ty == T_STOP then
+                              return ()
+                              else do skip a ty
+                                      readFieldEnd a
+                                      skipFields a
+      skipMapEntries a n k v= if n == 0 then
+                                    return ()
+                                    else do skip a k
+                                            skip a v
+                                            skipMapEntries a (n-1) k v
+      skipSetEntries a n k = if n == 0 then
+                                   return ()
+                                   else do skip a k
+                                           skipSetEntries a (n-1) k
+      skip a typ = case typ of
+                       T_STOP -> return ()
+                       T_VOID -> return ()
+                       T_BOOL -> do readBool a
+                                    return ()
+                       T_BYTE -> do readByte a
+                                    return ()
+                       T_I08 -> do readByte a
+                                   return ()
+                       T_I16 -> do readI16 a
+                                   return ()
+                       T_I32 -> do readI32 a
+                                   return ()
+                       T_U64 -> do readI64 a
+                                   return ()
+                       T_I64 -> do readI64 a
+                                   return ()
+                       T_DOUBLE -> do readDouble a
+                                      return ()
+                       T_STRING -> do readString a
+                                      return ()
+                       T_UTF7 -> return ()
+                       T_STRUCT -> do readStructBegin a
+                                      skipFields a
+                                      readStructEnd a
+                                      return ()
+                       T_MAP -> do (k,v,s) <- readMapBegin a
+                                   skipMapEntries a s k v
+                                   readMapEnd a
+                                   return ()
+                       T_SET -> do (ty,s) <- readSetBegin a
+                                   skipSetEntries a s ty
+                                   readSetEnd a
+                                   return ()
+                       T_LIST -> do (ty,s) <- readListBegin a
+                                    skipSetEntries a s ty
+                                    readListEnd a
+                                    return ()
+                       T_UTF8       -> return ()
+                       T_UTF16      -> return ()
+                       T_UNKNOWN    -> return ()
+  
+  
+  data PE_type = PE_UNKNOWN      
+               | PE_INVALID_DATA
+               | PE_NEGATIVE_SIZE
+               | PE_SIZE_LIMIT
+               | PE_BAD_VERSION
+                 deriving (Eq, Data, Typeable)
+
+  data ProtocolExn = ProtocolExn PE_type [Char] deriving (Typeable, Data)
+  
+  data AE_type = AE_UNKNOWN
+               | AE_UNKNOWN_METHOD
+               | AE_INVALID_MESSAGE_TYPE
+               | AE_WRONG_METHOD_NAME
+               | AE_BAD_SEQUENCE_ID
+               | AE_MISSING_RESULT
+                 deriving (Eq, Data, Typeable)
+  
+  instance Enum AE_type where
+      toEnum i = case i of
+                   0 -> AE_UNKNOWN
+                   1 -> AE_UNKNOWN_METHOD
+                   2 -> AE_INVALID_MESSAGE_TYPE
+                   3 -> AE_WRONG_METHOD_NAME
+                   4 -> AE_BAD_SEQUENCE_ID
+                   5 -> AE_MISSING_RESULT
+                   _ -> AE_UNKNOWN
+      fromEnum t = case t of
+                     AE_UNKNOWN -> 0
+                     AE_UNKNOWN_METHOD -> 1
+                     AE_INVALID_MESSAGE_TYPE -> 2
+                     AE_WRONG_METHOD_NAME -> 3
+                     AE_BAD_SEQUENCE_ID -> 4
+                     AE_MISSING_RESULT -> 5
+  
+  data AppExn = AppExn {ae_type :: AE_type, ae_message :: [Char]} deriving (Typeable, Data)
+  
+  readAppExnFields pt rec = do (n,ft,id) <- readFieldBegin pt
+                               if ft == T_STOP then return rec
+                                      else
+                                        case id of
+                                          1 -> if ft == T_STRING then
+                                                   do s <- readString pt
+                                                      readAppExnFields pt rec{ae_message = s}
+                                                   else do skip pt ft
+                                                           readAppExnFields pt rec
+                                          2 -> if ft == T_I32 then
+                                                   do i <- readI32 pt
+                                                      readAppExnFields pt rec{ae_type = (toEnum  i)}
+                                                   else do skip pt ft
+                                                           readAppExnFields pt rec
+                                          _ -> do skip pt ft
+                                                  readFieldEnd pt
+                                                  readAppExnFields pt rec
+  
+  readAppExn pt = do readStructBegin pt
+                     rec <- readAppExnFields pt (AppExn {ae_type = undefined, ae_message = undefined})
+                     readStructEnd pt
+                     return rec
+  
+  
+  writeAppExn pt ae = do writeStructBegin pt "TApplicationException"
+                         if ae_message ae /= "" then
+                             do writeFieldBegin pt ("message",T_STRING,1)
+                                writeString pt (ae_message ae)
+                                writeFieldEnd pt
+                             else return ()
+                         writeFieldBegin pt ("type",T_I32,2);
+                         writeI32 pt (fromEnum (ae_type ae))
+                         writeFieldEnd pt
+                         writeFieldStop pt
+                         writeStructEnd pt
+
+                                          
diff --git a/test/hs/Client.hs b/test/hs/Client.hs
new file mode 100644
index 0000000..10e5929
--- /dev/null
+++ b/test/hs/Client.hs
@@ -0,0 +1,29 @@
+module Client where
+import Thrift
+import ThriftTest_Client
+import ThriftTest_Types
+import TSocket
+import TBinaryProtocol
+import qualified Data.Map as Map
+import qualified Data.Set as Set
+import Control.Monad
+t = TSocket "127.0.0.1" 9090 Nothing
+
+main = do to <- topen t
+          let p =  TBinaryProtocol to
+          let ps = (p,p)
+          print =<< testString ps "bya"
+          print =<< testByte ps 8
+          print =<< testByte ps (-8)
+          print =<< testI32 ps 32
+          print =<< testI32 ps (-32)
+          print =<< testI64 ps 64
+          print =<< testI64 ps (-64)
+          print =<< testDouble ps 3.14
+          print =<< testDouble ps (-3.14)
+          print =<< testMap ps (Map.fromList [(1,1),(2,2),(3,3)])
+          print =<< testList ps [1,2,3,4,5]
+          print =<< testSet ps (Set.fromList [1,2,3,4,5])
+          print =<< testStruct ps (Xtruct (Just "hi") (Just 4) (Just 5) Nothing)
+          tclose to
+          
diff --git a/test/hs/Server.hs b/test/hs/Server.hs
new file mode 100644
index 0000000..ee4fd20
--- /dev/null
+++ b/test/hs/Server.hs
@@ -0,0 +1,33 @@
+module Server where
+import Thrift
+import ThriftTest
+import ThriftTest_Iface
+import Data.Map as Map
+import TServer
+import Control.Exception
+import ThriftTest_Types
+
+
+data TestHandler = TestHandler
+instance ThriftTest_Iface TestHandler where
+    testVoid a = return ()
+    testString a (Just s) = do print s; return s
+    testByte a (Just x) = do print x; return x
+    testI32 a (Just x) = do print x; return x
+    testI64 a (Just x) = do print x; return x
+    testDouble a (Just x) = do print x; return x
+    testStruct a (Just x) = do print x; return x
+    testNest a (Just x) = do print x; return x
+    testMap a (Just x) = do print x; return x
+    testSet a (Just x) = do print x; return x
+    testList a (Just x) = do print x; return x
+    testEnum a (Just x) = do print x; return x
+    testTypedef a (Just x) = do print x; return x
+    testMapMap a (Just x) = return (Map.fromList [(1,Map.fromList [(2,2)])])
+    testInsanity a (Just x) = return (Map.fromList [(1,Map.fromList [(ONE,x)])])
+    testMulti a a1 a2 a3 a4 a5 a6 = return (Xtruct Nothing Nothing Nothing Nothing)
+    testException a c = throwDyn (Xception (Just 1) (Just "bya"))
+    testMultiException a c1 c2 = return (Xtruct Nothing Nothing Nothing Nothing)
+
+
+main = do (run_basic_server TestHandler process 9090) `catchDyn` (\(TransportExn s t) -> print s)
diff --git a/test/hs/runclient.sh b/test/hs/runclient.sh
new file mode 100644
index 0000000..3dace0a
--- /dev/null
+++ b/test/hs/runclient.sh
@@ -0,0 +1,2 @@
+#!/bin/bash
+ghci -fglasgow-exts -i/home/iproctor/code/projects/thrift/trunk/lib/hs/src -igen-hs Client.hs
diff --git a/test/hs/runserver.sh b/test/hs/runserver.sh
new file mode 100644
index 0000000..c090155
--- /dev/null
+++ b/test/hs/runserver.sh
@@ -0,0 +1,2 @@
+#!/bin/bash
+ghci -fglasgow-exts -i/home/iproctor/code/projects/thrift/trunk/lib/hs/src -igen-hs Server.hs