src/Tools/Code/code_haskell.ML
changeset 48431 6efff142bb54
parent 48072 ace701efe203
child 48568 084cd758a8ab
equal deleted inserted replaced
48430:6cbfe187a0f9 48431:6efff142bb54
   301       { module_alias = module_alias, module_prefix = module_prefix,
   301       { module_alias = module_alias, module_prefix = module_prefix,
   302         reserved = reserved, empty_nsp = (reserved, reserved), namify_stmt = namify_stmt,
   302         reserved = reserved, empty_nsp = (reserved, reserved), namify_stmt = namify_stmt,
   303         modify_stmt = fn stmt => if select_stmt stmt then SOME stmt else NONE }
   303         modify_stmt = fn stmt => if select_stmt stmt then SOME stmt else NONE }
   304   end;
   304   end;
   305 
   305 
       
   306 val prelude_import_operators = [
       
   307   "==", "/=", "<", "<=", ">=", ">", "+", "-", "*", "/", "**", ">>=", ">>", "=<<", "&&", "||", "^", "^^", ".", "$", "$!", "++", "!!"
       
   308 ];
       
   309 
       
   310 val prelude_import_unqualified = [
       
   311   "Eq",
       
   312   "error",
       
   313   "id",
       
   314   "return",
       
   315   "not",
       
   316   "fst", "snd",
       
   317   "map", "filter", "concat", "concatMap", "reverse", "zip", "null", "takeWhile", "dropWhile", "all", "any",
       
   318   "Integer", "negate", "abs", "divMod",
       
   319   "String"
       
   320 ];
       
   321 
       
   322 val prelude_import_unqualified_constr = [
       
   323   ("Bool", ["True", "False"]),
       
   324   ("Maybe", ["Nothing", "Just"])
       
   325 ];
       
   326 
   306 fun serialize_haskell module_prefix string_classes { labelled_name, reserved_syms,
   327 fun serialize_haskell module_prefix string_classes { labelled_name, reserved_syms,
   307     includes, module_alias, class_syntax, tyco_syntax, const_syntax } program =
   328     includes, module_alias, class_syntax, tyco_syntax, const_syntax } program =
   308   let
   329   let
   309 
   330 
   310     (* build program *)
   331     (* build program *)
   329     fun print_stmt deresolve = print_haskell_stmt
   350     fun print_stmt deresolve = print_haskell_stmt
   330       class_syntax tyco_syntax const_syntax (make_vars reserved)
   351       class_syntax tyco_syntax const_syntax (make_vars reserved)
   331       deresolve (if string_classes then deriving_show else K false);
   352       deresolve (if string_classes then deriving_show else K false);
   332 
   353 
   333     (* print modules *)
   354     (* print modules *)
   334     val import_includes_ps =
       
   335       map (fn (name, _) => str ("import qualified " ^ name ^ ";")) includes;
       
   336     fun print_module_frame module_name ps =
   355     fun print_module_frame module_name ps =
   337       (module_name, Pretty.chunks2 (
   356       (module_name, Pretty.chunks2 (
   338         str ("module " ^ module_name ^ " where {")
   357         str ("module " ^ module_name ^ " where {")
   339         :: ps
   358         :: ps
   340         @| str "}"
   359         @| str "}"
   341       ));
   360       ));
       
   361     fun print_qualified_import module_name = semicolon [str "import qualified", str module_name];
       
   362     val import_common_ps =
       
   363       enclose "import Prelude (" ");" (commas (map str
       
   364         (map (Library.enclose "(" ")") prelude_import_operators @ prelude_import_unqualified)
       
   365           @ map (fn (tyco, constrs) => (enclose (tyco ^ "(") ")" o commas o map str) constrs) prelude_import_unqualified_constr))
       
   366       :: print_qualified_import "Prelude"
       
   367       :: map (print_qualified_import o fst) includes;
   342     fun print_module module_name (gr, imports) =
   368     fun print_module module_name (gr, imports) =
   343       let
   369       let
   344         val deresolve = deresolver module_name
   370         val deresolve = deresolver module_name;
   345         fun print_import module_name = (semicolon o map str) ["import qualified", module_name];
   371         fun print_import module_name = (semicolon o map str) ["import qualified", module_name];
   346         val import_ps = import_includes_ps @ map (print_import o fst) imports;
   372         val import_ps = import_common_ps @ map (print_qualified_import o fst) imports;
   347         fun print_stmt' gr name = case Graph.get_node gr name
   373         fun print_stmt' name = case Graph.get_node gr name
   348          of (_, NONE) => NONE
   374          of (_, NONE) => NONE
   349           | (_, SOME stmt) => SOME (markup_stmt name (print_stmt deresolve (name, stmt)));
   375           | (_, SOME stmt) => SOME (markup_stmt name (print_stmt deresolve (name, stmt)));
   350         val body_ps = map_filter (print_stmt' gr) ((flat o rev o Graph.strong_conn) gr);
   376         val body_ps = map_filter print_stmt' ((flat o rev o Graph.strong_conn) gr);
   351       in
   377       in
   352         print_module_frame module_name
   378         print_module_frame module_name
   353           ((if null import_ps then [] else [Pretty.chunks import_ps]) @ body_ps)
   379           ((if null import_ps then [] else [Pretty.chunks import_ps]) @ body_ps)
   354       end;
   380       end;
   355 
   381 
   470   #> fold (Code_Target.add_reserved target) [
   496   #> fold (Code_Target.add_reserved target) [
   471       "hiding", "deriving", "where", "case", "of", "infix", "infixl", "infixr",
   497       "hiding", "deriving", "where", "case", "of", "infix", "infixl", "infixr",
   472       "import", "default", "forall", "let", "in", "class", "qualified", "data",
   498       "import", "default", "forall", "let", "in", "class", "qualified", "data",
   473       "newtype", "instance", "if", "then", "else", "type", "as", "do", "module"
   499       "newtype", "instance", "if", "then", "else", "type", "as", "do", "module"
   474     ]
   500     ]
   475   #> fold (Code_Target.add_reserved target) [
   501   #> fold (Code_Target.add_reserved target) prelude_import_unqualified
   476       "Prelude", "Main", "Bool", "Maybe", "Either", "Ordering", "Char", "String", "Int",
   502   #> fold (Code_Target.add_reserved target o fst) prelude_import_unqualified_constr
   477       "Integer", "Float", "Double", "Rational", "IO", "Eq", "Ord", "Enum", "Bounded",
   503   #> fold (fold (Code_Target.add_reserved target) o snd) prelude_import_unqualified_constr;
   478       "Num", "Real", "Integral", "Fractional", "Floating", "RealFloat", "Monad", "Functor",
       
   479       "AlreadyExists", "ArithException", "ArrayException", "AssertionFailed", "AsyncException",
       
   480       "BlockedOnDeadMVar", "Deadlock", "Denormal", "DivideByZero", "DotNetException", "DynException",
       
   481       "Dynamic", "EOF", "EQ", "EmptyRec", "ErrorCall", "ExitException", "ExitFailure",
       
   482       "ExitSuccess", "False", "GT", "HeapOverflow",
       
   483       "IOError", "IOException", "IllegalOperation",
       
   484       "IndexOutOfBounds", "Just", "Key", "LT", "Left", "LossOfPrecision", "NoMethodError",
       
   485       "NoSuchThing", "NonTermination", "Nothing", "Obj", "OtherError", "Overflow",
       
   486       "PatternMatchFail", "PermissionDenied", "ProtocolError", "RecConError", "RecSelError",
       
   487       "RecUpdError", "ResourceBusy", "ResourceExhausted", "Right", "StackOverflow",
       
   488       "ThreadKilled", "True", "TyCon", "TypeRep", "UndefinedElement", "Underflow",
       
   489       "UnsupportedOperation", "UserError", "abs", "absReal", "acos", "acosh", "all",
       
   490       "and", "any", "appendFile", "asTypeOf", "asciiTab", "asin", "asinh", "atan",
       
   491       "atan2", "atanh", "basicIORun", "blockIO", "boundedEnumFrom", "boundedEnumFromThen",
       
   492       "boundedEnumFromThenTo", "boundedEnumFromTo", "boundedPred", "boundedSucc", "break",
       
   493       "catch", "catchException", "ceiling", "compare", "concat", "concatMap", "const",
       
   494       "cos", "cosh", "curry", "cycle", "decodeFloat", "denominator", "div", "divMod",
       
   495       "doubleToRatio", "doubleToRational", "drop", "dropWhile", "either", "elem",
       
   496       "emptyRec", "encodeFloat", "enumFrom", "enumFromThen", "enumFromThenTo",
       
   497       "enumFromTo", "error", "even", "exp", "exponent", "fail", "filter", "flip",
       
   498       "floatDigits", "floatProperFraction", "floatRadix", "floatRange", "floatToRational",
       
   499       "floor", "fmap", "foldl", "foldl'", "foldl1", "foldr", "foldr1", "fromDouble",
       
   500       "fromEnum", "fromEnum_0", "fromInt", "fromInteger", "fromIntegral", "fromObj",
       
   501       "fromRational", "fst", "gcd", "getChar", "getContents", "getLine", "head",
       
   502       "id", "inRange", "index", "init", "intToRatio", "interact", "ioError", "isAlpha",
       
   503       "isAlphaNum", "isDenormalized", "isDigit", "isHexDigit", "isIEEE", "isInfinite",
       
   504       "isLower", "isNaN", "isNegativeZero", "isOctDigit", "isSpace", "isUpper", "iterate", "iterate'",
       
   505       "last", "lcm", "length", "lex", "lexDigits", "lexLitChar", "lexmatch", "lines", "log",
       
   506       "logBase", "lookup", "loop", "map", "mapM", "mapM_", "max", "maxBound", "maximum",
       
   507       "maybe", "min", "minBound", "minimum", "mod", "negate", "nonnull", "not", "notElem",
       
   508       "null", "numerator", "numericEnumFrom", "numericEnumFromThen", "numericEnumFromThenTo",
       
   509       "numericEnumFromTo", "odd", "or", "otherwise", "pi", "pred", 
       
   510       "print", "product", "properFraction", "protectEsc", "putChar", "putStr", "putStrLn",
       
   511       "quot", "quotRem", "range", "rangeSize", "rationalToDouble", "rationalToFloat",
       
   512       "rationalToRealFloat", "read", "readDec", "readField", "readFieldName", "readFile",
       
   513       "readFloat", "readHex", "readIO", "readInt", "readList", "readLitChar", "readLn",
       
   514       "readOct", "readParen", "readSigned", "reads", "readsPrec", "realFloatToRational",
       
   515       "realToFrac", "recip", "reduce", "rem", "repeat", "replicate", "return", "reverse",
       
   516       "round", "scaleFloat", "scanl", "scanl1", "scanr", "scanr1", "seq", "sequence",
       
   517       "sequence_", "show", "showChar", "showException", "showField", "showList",
       
   518       "showLitChar", "showParen", "showString", "shows", "showsPrec", "significand",
       
   519       "signum", "signumReal", "sin", "sinh", "snd", "span", "splitAt", "sqrt", "subtract",
       
   520       "succ", "sum", "tail", "take", "takeWhile", "takeWhile1", "tan", "tanh", "threadToIOResult",
       
   521       "throw", "toEnum", "toInt", "toInteger", "toObj", "toRational", "truncate", "uncurry",
       
   522       "undefined", "unlines", "unsafeCoerce", "unsafeIndex", "unsafeRangeSize", "until", "unwords",
       
   523       "unzip", "unzip3", "userError", "words", "writeFile", "zip", "zip3", "zipWith", "zipWith3"
       
   524     ] (*due to weird handling of ':', we can't do anything else than to import *all* prelude symbols*);
       
   525 
   504 
   526 end; (*struct*)
   505 end; (*struct*)