src/Tools/Code/code_haskell.ML
changeset 49446 6efff142bb54
parent 49087 ace701efe203
child 49583 084cd758a8ab
     1.1 --- a/src/Tools/Code/code_haskell.ML	Mon Jul 23 09:26:55 2012 +0200
     1.2 +++ b/src/Tools/Code/code_haskell.ML	Mon Jul 23 09:28:03 2012 +0200
     1.3 @@ -303,6 +303,27 @@
     1.4          modify_stmt = fn stmt => if select_stmt stmt then SOME stmt else NONE }
     1.5    end;
     1.6  
     1.7 +val prelude_import_operators = [
     1.8 +  "==", "/=", "<", "<=", ">=", ">", "+", "-", "*", "/", "**", ">>=", ">>", "=<<", "&&", "||", "^", "^^", ".", "$", "$!", "++", "!!"
     1.9 +];
    1.10 +
    1.11 +val prelude_import_unqualified = [
    1.12 +  "Eq",
    1.13 +  "error",
    1.14 +  "id",
    1.15 +  "return",
    1.16 +  "not",
    1.17 +  "fst", "snd",
    1.18 +  "map", "filter", "concat", "concatMap", "reverse", "zip", "null", "takeWhile", "dropWhile", "all", "any",
    1.19 +  "Integer", "negate", "abs", "divMod",
    1.20 +  "String"
    1.21 +];
    1.22 +
    1.23 +val prelude_import_unqualified_constr = [
    1.24 +  ("Bool", ["True", "False"]),
    1.25 +  ("Maybe", ["Nothing", "Just"])
    1.26 +];
    1.27 +
    1.28  fun serialize_haskell module_prefix string_classes { labelled_name, reserved_syms,
    1.29      includes, module_alias, class_syntax, tyco_syntax, const_syntax } program =
    1.30    let
    1.31 @@ -331,23 +352,28 @@
    1.32        deresolve (if string_classes then deriving_show else K false);
    1.33  
    1.34      (* print modules *)
    1.35 -    val import_includes_ps =
    1.36 -      map (fn (name, _) => str ("import qualified " ^ name ^ ";")) includes;
    1.37      fun print_module_frame module_name ps =
    1.38        (module_name, Pretty.chunks2 (
    1.39          str ("module " ^ module_name ^ " where {")
    1.40          :: ps
    1.41          @| str "}"
    1.42        ));
    1.43 +    fun print_qualified_import module_name = semicolon [str "import qualified", str module_name];
    1.44 +    val import_common_ps =
    1.45 +      enclose "import Prelude (" ");" (commas (map str
    1.46 +        (map (Library.enclose "(" ")") prelude_import_operators @ prelude_import_unqualified)
    1.47 +          @ map (fn (tyco, constrs) => (enclose (tyco ^ "(") ")" o commas o map str) constrs) prelude_import_unqualified_constr))
    1.48 +      :: print_qualified_import "Prelude"
    1.49 +      :: map (print_qualified_import o fst) includes;
    1.50      fun print_module module_name (gr, imports) =
    1.51        let
    1.52 -        val deresolve = deresolver module_name
    1.53 +        val deresolve = deresolver module_name;
    1.54          fun print_import module_name = (semicolon o map str) ["import qualified", module_name];
    1.55 -        val import_ps = import_includes_ps @ map (print_import o fst) imports;
    1.56 -        fun print_stmt' gr name = case Graph.get_node gr name
    1.57 +        val import_ps = import_common_ps @ map (print_qualified_import o fst) imports;
    1.58 +        fun print_stmt' name = case Graph.get_node gr name
    1.59           of (_, NONE) => NONE
    1.60            | (_, SOME stmt) => SOME (markup_stmt name (print_stmt deresolve (name, stmt)));
    1.61 -        val body_ps = map_filter (print_stmt' gr) ((flat o rev o Graph.strong_conn) gr);
    1.62 +        val body_ps = map_filter print_stmt' ((flat o rev o Graph.strong_conn) gr);
    1.63        in
    1.64          print_module_frame module_name
    1.65            ((if null import_ps then [] else [Pretty.chunks import_ps]) @ body_ps)
    1.66 @@ -472,55 +498,8 @@
    1.67        "import", "default", "forall", "let", "in", "class", "qualified", "data",
    1.68        "newtype", "instance", "if", "then", "else", "type", "as", "do", "module"
    1.69      ]
    1.70 -  #> fold (Code_Target.add_reserved target) [
    1.71 -      "Prelude", "Main", "Bool", "Maybe", "Either", "Ordering", "Char", "String", "Int",
    1.72 -      "Integer", "Float", "Double", "Rational", "IO", "Eq", "Ord", "Enum", "Bounded",
    1.73 -      "Num", "Real", "Integral", "Fractional", "Floating", "RealFloat", "Monad", "Functor",
    1.74 -      "AlreadyExists", "ArithException", "ArrayException", "AssertionFailed", "AsyncException",
    1.75 -      "BlockedOnDeadMVar", "Deadlock", "Denormal", "DivideByZero", "DotNetException", "DynException",
    1.76 -      "Dynamic", "EOF", "EQ", "EmptyRec", "ErrorCall", "ExitException", "ExitFailure",
    1.77 -      "ExitSuccess", "False", "GT", "HeapOverflow",
    1.78 -      "IOError", "IOException", "IllegalOperation",
    1.79 -      "IndexOutOfBounds", "Just", "Key", "LT", "Left", "LossOfPrecision", "NoMethodError",
    1.80 -      "NoSuchThing", "NonTermination", "Nothing", "Obj", "OtherError", "Overflow",
    1.81 -      "PatternMatchFail", "PermissionDenied", "ProtocolError", "RecConError", "RecSelError",
    1.82 -      "RecUpdError", "ResourceBusy", "ResourceExhausted", "Right", "StackOverflow",
    1.83 -      "ThreadKilled", "True", "TyCon", "TypeRep", "UndefinedElement", "Underflow",
    1.84 -      "UnsupportedOperation", "UserError", "abs", "absReal", "acos", "acosh", "all",
    1.85 -      "and", "any", "appendFile", "asTypeOf", "asciiTab", "asin", "asinh", "atan",
    1.86 -      "atan2", "atanh", "basicIORun", "blockIO", "boundedEnumFrom", "boundedEnumFromThen",
    1.87 -      "boundedEnumFromThenTo", "boundedEnumFromTo", "boundedPred", "boundedSucc", "break",
    1.88 -      "catch", "catchException", "ceiling", "compare", "concat", "concatMap", "const",
    1.89 -      "cos", "cosh", "curry", "cycle", "decodeFloat", "denominator", "div", "divMod",
    1.90 -      "doubleToRatio", "doubleToRational", "drop", "dropWhile", "either", "elem",
    1.91 -      "emptyRec", "encodeFloat", "enumFrom", "enumFromThen", "enumFromThenTo",
    1.92 -      "enumFromTo", "error", "even", "exp", "exponent", "fail", "filter", "flip",
    1.93 -      "floatDigits", "floatProperFraction", "floatRadix", "floatRange", "floatToRational",
    1.94 -      "floor", "fmap", "foldl", "foldl'", "foldl1", "foldr", "foldr1", "fromDouble",
    1.95 -      "fromEnum", "fromEnum_0", "fromInt", "fromInteger", "fromIntegral", "fromObj",
    1.96 -      "fromRational", "fst", "gcd", "getChar", "getContents", "getLine", "head",
    1.97 -      "id", "inRange", "index", "init", "intToRatio", "interact", "ioError", "isAlpha",
    1.98 -      "isAlphaNum", "isDenormalized", "isDigit", "isHexDigit", "isIEEE", "isInfinite",
    1.99 -      "isLower", "isNaN", "isNegativeZero", "isOctDigit", "isSpace", "isUpper", "iterate", "iterate'",
   1.100 -      "last", "lcm", "length", "lex", "lexDigits", "lexLitChar", "lexmatch", "lines", "log",
   1.101 -      "logBase", "lookup", "loop", "map", "mapM", "mapM_", "max", "maxBound", "maximum",
   1.102 -      "maybe", "min", "minBound", "minimum", "mod", "negate", "nonnull", "not", "notElem",
   1.103 -      "null", "numerator", "numericEnumFrom", "numericEnumFromThen", "numericEnumFromThenTo",
   1.104 -      "numericEnumFromTo", "odd", "or", "otherwise", "pi", "pred", 
   1.105 -      "print", "product", "properFraction", "protectEsc", "putChar", "putStr", "putStrLn",
   1.106 -      "quot", "quotRem", "range", "rangeSize", "rationalToDouble", "rationalToFloat",
   1.107 -      "rationalToRealFloat", "read", "readDec", "readField", "readFieldName", "readFile",
   1.108 -      "readFloat", "readHex", "readIO", "readInt", "readList", "readLitChar", "readLn",
   1.109 -      "readOct", "readParen", "readSigned", "reads", "readsPrec", "realFloatToRational",
   1.110 -      "realToFrac", "recip", "reduce", "rem", "repeat", "replicate", "return", "reverse",
   1.111 -      "round", "scaleFloat", "scanl", "scanl1", "scanr", "scanr1", "seq", "sequence",
   1.112 -      "sequence_", "show", "showChar", "showException", "showField", "showList",
   1.113 -      "showLitChar", "showParen", "showString", "shows", "showsPrec", "significand",
   1.114 -      "signum", "signumReal", "sin", "sinh", "snd", "span", "splitAt", "sqrt", "subtract",
   1.115 -      "succ", "sum", "tail", "take", "takeWhile", "takeWhile1", "tan", "tanh", "threadToIOResult",
   1.116 -      "throw", "toEnum", "toInt", "toInteger", "toObj", "toRational", "truncate", "uncurry",
   1.117 -      "undefined", "unlines", "unsafeCoerce", "unsafeIndex", "unsafeRangeSize", "until", "unwords",
   1.118 -      "unzip", "unzip3", "userError", "words", "writeFile", "zip", "zip3", "zipWith", "zipWith3"
   1.119 -    ] (*due to weird handling of ':', we can't do anything else than to import *all* prelude symbols*);
   1.120 +  #> fold (Code_Target.add_reserved target) prelude_import_unqualified
   1.121 +  #> fold (Code_Target.add_reserved target o fst) prelude_import_unqualified_constr
   1.122 +  #> fold (fold (Code_Target.add_reserved target) o snd) prelude_import_unqualified_constr;
   1.123  
   1.124  end; (*struct*)