src/Tools/Code/code_haskell.ML
changeset 69623 ef02c5e051e5
parent 69593 3dda49e08b9d
child 69690 1fb204399d8d
equal deleted inserted replaced
69622:003475955593 69623:ef02c5e051e5
   329   ("Bool", ["True", "False"]),
   329   ("Bool", ["True", "False"]),
   330   ("Maybe", ["Nothing", "Just"])
   330   ("Maybe", ["Nothing", "Just"])
   331 ];
   331 ];
   332 
   332 
   333 fun serialize_haskell module_prefix string_classes ctxt { module_name,
   333 fun serialize_haskell module_prefix string_classes ctxt { module_name,
   334     reserved_syms, identifiers, includes, class_syntax, tyco_syntax, const_syntax } exports program =
   334     reserved_syms, identifiers, includes, class_syntax, tyco_syntax, const_syntax } program exports =
   335   let
   335   let
   336 
   336 
   337     (* build program *)
   337     (* build program *)
   338     val reserved = fold (insert (op =) o fst) includes reserved_syms;
   338     val reserved = fold (insert (op =) o fst) includes reserved_syms;
   339     val { deresolver, flat_program = haskell_program } = haskell_program_of_program
   339     val { deresolver, flat_program = haskell_program } = haskell_program_of_program
   355     fun print_stmt deresolve = print_haskell_stmt
   355     fun print_stmt deresolve = print_haskell_stmt
   356       class_syntax tyco_syntax const_syntax (make_vars reserved)
   356       class_syntax tyco_syntax const_syntax (make_vars reserved)
   357       deresolve (if string_classes then deriving_show else K false);
   357       deresolve (if string_classes then deriving_show else K false);
   358 
   358 
   359     (* print modules *)
   359     (* print modules *)
   360     fun print_module_frame module_name exports ps =
   360     fun module_names module_name =
   361       (module_name, Pretty.chunks2 (
   361       let
   362         concat [str "module",
   362         val (xs, x) = split_last (Long_Name.explode module_name)
       
   363       in xs @ [x ^ ".hs"] end
       
   364     fun print_module_frame module_name header exports ps =
       
   365       (module_names module_name, Pretty.chunks2 (
       
   366         header
       
   367         @ concat [str "module",
   363           case exports of
   368           case exports of
   364             SOME ps => Pretty.block [str module_name, enclose "(" ")" (commas ps)]
   369             SOME ps => Pretty.block [str module_name, enclose "(" ")" (commas ps)]
   365           | NONE => str module_name,
   370           | NONE => str module_name,
   366           str "where {"
   371           str "where {"
   367         ]
   372         ]
   395         val (export_ps, body_ps) = (flat o rev o Code_Symbol.Graph.strong_conn) gr
   400         val (export_ps, body_ps) = (flat o rev o Code_Symbol.Graph.strong_conn) gr
   396           |> map_filter print_stmt'
   401           |> map_filter print_stmt'
   397           |> split_list
   402           |> split_list
   398           |> apfst (map_filter I);
   403           |> apfst (map_filter I);
   399       in
   404       in
   400         print_module_frame module_name (SOME export_ps)
   405         print_module_frame module_name [str language_pragma] (SOME export_ps)
   401           ((if null import_ps then [] else [Pretty.chunks import_ps]) @ body_ps)
   406           ((if null import_ps then [] else [Pretty.chunks import_ps]) @ body_ps)
   402       end;
   407       end;
   403 
   408 
   404     (*serialization*)
       
   405     fun write_module width (SOME destination) (module_name, content) =
       
   406           let
       
   407             val _ = File.check_dir destination;
       
   408             val filepath = (Path.append destination o Path.ext "hs" o Path.explode o implode
       
   409               o separate "/" o Long_Name.explode) module_name;
       
   410             val _ = Isabelle_System.mkdirs (Path.dir filepath);
       
   411           in
       
   412             (File.write filepath o format [] width o Pretty.chunks2)
       
   413               [str language_pragma, content]
       
   414           end
       
   415       | write_module width NONE (_, content) = writeln (format [] width content);
       
   416   in
   409   in
   417     Code_Target.serialization
   410     (Code_Target.Hierarchy (map (fn (module_name, content) => print_module_frame module_name [] NONE [content]) includes
   418       (fn width => fn destination => K () o map (write_module width destination))
   411       @ map (fn module_name => print_module module_name (Graph.get_node haskell_program module_name))
   419       (fn present => fn width => rpair (try (deresolver ""))
   412         ((flat o rev o Graph.strong_conn) haskell_program)), try (deresolver ""))
   420         o (map o apsnd) (format present width))
       
   421       (map (fn (module_name, content) => print_module_frame module_name NONE [content]) includes
       
   422         @ map (fn module_name => print_module module_name (Graph.get_node haskell_program module_name))
       
   423           ((flat o rev o Graph.strong_conn) haskell_program))
       
   424   end;
   413   end;
   425 
   414 
   426 val serializer : Code_Target.serializer =
   415 val serializer : Code_Target.serializer =
   427   Code_Target.parse_args (Scan.optional (Args.$$$ "root" -- Args.colon |-- Args.name) ""
   416   Code_Target.parse_args (Scan.optional (Args.$$$ "root" -- Args.colon |-- Args.name) ""
   428     -- Scan.optional (Args.$$$ "string_classes" >> K true) false
   417     -- Scan.optional (Args.$$$ "string_classes" >> K true) false