src/Pure/Tools/codegen_serializer.ML
changeset 22355 f9d35783d28d
parent 22306 a532c39c8917
child 22396 6c7f9207fa9e
equal deleted inserted replaced
22354:ec6a033b27be 22355:f9d35783d28d
     1     (*  Title:      Pure/Tools/codegen_serializer.ML
     1 (*  Title:      Pure/Tools/codegen_serializer.ML
     2     ID:         $Id$
     2     ID:         $Id$
     3     Author:     Florian Haftmann, TU Muenchen
     3     Author:     Florian Haftmann, TU Muenchen
     4 
     4 
     5 Serializer from intermediate language ("Thin-gol") to
     5 Serializer from intermediate language ("Thin-gol") to
     6 target languages (like SML or Haskell).
     6 target languages (like SML or Haskell).
     7 *)
     7 *)
     8 
     8 
     9 signature CODEGEN_SERIALIZER =
     9 signature CODEGEN_SERIALIZER =
    10 sig
    10 sig
    11   include BASIC_CODEGEN_THINGOL;
    11   include BASIC_CODEGEN_THINGOL;
       
    12 
       
    13   val add_syntax_class: string -> class
       
    14     -> (string * ((string * typ list) * string) list) option -> theory -> theory;
       
    15   val add_syntax_inst: string -> string * class -> bool -> theory -> theory;
       
    16   val add_syntax_tycoP: string -> string -> OuterParse.token list
       
    17     -> (theory -> theory) * OuterParse.token list;
       
    18   val add_syntax_constP: string -> string -> OuterParse.token list
       
    19     -> (theory -> theory) * OuterParse.token list;
    12 
    20 
    13   val add_pretty_list: string -> string -> string -> (Pretty.T list -> Pretty.T)
    21   val add_pretty_list: string -> string -> string -> (Pretty.T list -> Pretty.T)
    14    -> ((string -> string) * (string -> string)) option -> int * string
    22    -> ((string -> string) * (string -> string)) option -> int * string
    15    -> theory -> theory;
    23    -> theory -> theory;
    16   val add_pretty_ml_string: string -> string -> string -> string
    24   val add_pretty_ml_string: string -> string -> string -> string
    31   val eval_term: theory -> (theory -> string -> string) -> CodegenThingol.code
    39   val eval_term: theory -> (theory -> string -> string) -> CodegenThingol.code
    32     -> (string * 'a option ref) * CodegenThingol.iterm -> 'a;
    40     -> (string * 'a option ref) * CodegenThingol.iterm -> 'a;
    33   val code_width: int ref;
    41   val code_width: int ref;
    34 end;
    42 end;
    35 
    43 
    36 structure CodegenSerializer: CODEGEN_SERIALIZER =
    44 structure CodegenSerializer : CODEGEN_SERIALIZER =
    37 struct
    45 struct
    38 
    46 
    39 open BasicCodegenThingol;
    47 open BasicCodegenThingol;
    40 val tracing = CodegenThingol.tracing;
    48 val tracing = CodegenThingol.tracing;
    41 
    49 
  1725       thy
  1733       thy
  1726       |> (map_syntax_exprs target o apsnd o apsnd)
  1734       |> (map_syntax_exprs target o apsnd o apsnd)
  1727            (Symtab.delete_safe c')
  1735            (Symtab.delete_safe c')
  1728   end;
  1736   end;
  1729 
  1737 
       
  1738 fun cert_class thy class =
       
  1739   let
       
  1740     val _ = AxClass.get_definition thy class;
       
  1741   in class end;
       
  1742 
  1730 fun read_class thy raw_class =
  1743 fun read_class thy raw_class =
  1731   let
  1744   let
  1732     val class = Sign.intern_class thy raw_class;
  1745     val class = Sign.intern_class thy raw_class;
  1733     val _ = AxClass.get_definition thy class;
  1746     val _ = AxClass.get_definition thy class;
  1734   in class end;
  1747   in class end;
  1735 
  1748 
  1736 fun read_type thy raw_tyco =
  1749 fun cert_tyco thy tyco =
       
  1750   let
       
  1751     val _ = if Sign.declared_tyname thy tyco then ()
       
  1752       else error ("No such type constructor: " ^ quote tyco);
       
  1753   in tyco end;
       
  1754 
       
  1755 fun read_tyco thy raw_tyco =
  1737   let
  1756   let
  1738     val tyco = Sign.intern_type thy raw_tyco;
  1757     val tyco = Sign.intern_type thy raw_tyco;
  1739     val _ = if Sign.declared_tyname thy tyco then ()
  1758     val _ = if Sign.declared_tyname thy tyco then ()
  1740       else error ("No such type constructor: " ^ quote raw_tyco);
  1759       else error ("No such type constructor: " ^ quote raw_tyco);
  1741   in tyco end;
  1760   in tyco end;
  1769           (no_bindings (SOME (parse_infix fst (L, 1) ">>=")))
  1788           (no_bindings (SOME (parse_infix fst (L, 1) ">>=")))
  1770     |> gen_add_syntax_const (K I) target_Haskell c_kbind'
  1789     |> gen_add_syntax_const (K I) target_Haskell c_kbind'
  1771           (no_bindings (SOME (parse_infix fst (L, 1) ">>")))
  1790           (no_bindings (SOME (parse_infix fst (L, 1) ">>")))
  1772   end;
  1791   end;
  1773 
  1792 
  1774 val add_syntax_class = gen_add_syntax_class read_class CodegenConsts.read_const;
       
  1775 val add_syntax_inst = gen_add_syntax_inst read_class read_type;
       
  1776 val add_syntax_tyco = gen_add_syntax_tyco read_type;
       
  1777 val add_syntax_const = gen_add_syntax_const CodegenConsts.read_const;
       
  1778 
       
  1779 fun add_reserved target =
  1793 fun add_reserved target =
  1780   map_reserveds target o insert (op =);
  1794   map_reserveds target o insert (op =);
  1781 
  1795 
  1782 fun add_modl_alias target =
  1796 fun add_modl_alias target =
  1783   map_syntax_modls target o apfst o Symtab.update o apsnd CodegenNames.check_modulename;
  1797   map_syntax_modls target o apfst o Symtab.update o apsnd CodegenNames.check_modulename;
  1818   ("code_class", "code_instance", "code_type", "code_const", "code_monad",
  1832   ("code_class", "code_instance", "code_type", "code_const", "code_monad",
  1819     "code_reserved", "code_modulename", "code_moduleprolog");
  1833     "code_reserved", "code_modulename", "code_moduleprolog");
  1820 
  1834 
  1821 in
  1835 in
  1822 
  1836 
       
  1837 val parse_syntax = parse_syntax;
       
  1838 
       
  1839 val add_syntax_class = gen_add_syntax_class cert_class (K I);
       
  1840 val add_syntax_inst = gen_add_syntax_inst cert_class cert_tyco;
       
  1841 val add_syntax_tyco = gen_add_syntax_tyco cert_tyco;
       
  1842 val add_syntax_const = gen_add_syntax_const (K I);
       
  1843 
       
  1844 val add_syntax_class_cmd = gen_add_syntax_class read_class CodegenConsts.read_const;
       
  1845 val add_syntax_inst_cmd = gen_add_syntax_inst read_class read_tyco;
       
  1846 val add_syntax_tyco_cmd = gen_add_syntax_tyco read_tyco;
       
  1847 val add_syntax_const_cmd = gen_add_syntax_const CodegenConsts.read_const;
       
  1848 
       
  1849 fun add_syntax_tycoP target tyco = parse_syntax I >> add_syntax_tyco_cmd target tyco;
       
  1850 fun add_syntax_constP target c = parse_syntax fst >> (add_syntax_const_cmd target c o no_bindings);
       
  1851 
  1823 fun add_pretty_list target nill cons mk_list mk_char_string target_cons thy =
  1852 fun add_pretty_list target nill cons mk_list mk_char_string target_cons thy =
  1824   let
  1853   let
  1825     val (_, nil'') = idfs_of_const thy nill;
  1854     val (_, nil'') = idfs_of_const thy nill;
  1826     val (cons', cons'') = idfs_of_const thy cons;
  1855     val (cons', cons'') = idfs_of_const thy cons;
  1827     val pr = pretty_list nil'' cons'' mk_list mk_char_string target_cons;
  1856     val pr = pretty_list nil'' cons'' mk_list mk_char_string target_cons;
  1828   in
  1857   in
  1829     thy
  1858     thy
  1830     |> gen_add_syntax_const (K I) target cons' (SOME pr)
  1859     |> add_syntax_const target cons' (SOME pr)
  1831   end;
  1860   end;
  1832 
  1861 
  1833 fun add_pretty_ml_string target nill cons str mk_char mk_string target_implode thy =
  1862 fun add_pretty_ml_string target nill cons str mk_char mk_string target_implode thy =
  1834   let
  1863   let
  1835     val (_, nil'') = idfs_of_const thy nill;
  1864     val (_, nil'') = idfs_of_const thy nill;
  1836     val (_, cons'') = idfs_of_const thy cons;
  1865     val (_, cons'') = idfs_of_const thy cons;
  1837     val (str', _) = idfs_of_const thy str;
  1866     val (str', _) = idfs_of_const thy str;
  1838     val pr = pretty_ml_string nil'' cons'' mk_char mk_string target_implode;
  1867     val pr = pretty_ml_string nil'' cons'' mk_char mk_string target_implode;
  1839   in
  1868   in
  1840     thy
  1869     thy
  1841     |> gen_add_syntax_const (K I) target str' (SOME pr)
  1870     |> add_syntax_const target str' (SOME pr)
  1842   end;
  1871   end;
  1843 
  1872 
  1844 fun add_undefined target undef target_undefined thy =
  1873 fun add_undefined target undef target_undefined thy =
  1845   let
  1874   let
  1846     val (undef', _) = idfs_of_const thy undef;
  1875     val (undef', _) = idfs_of_const thy undef;
  1847     fun pr _ _ _ _ = str target_undefined;
  1876     fun pr _ _ _ _ = str target_undefined;
  1848   in
  1877   in
  1849     thy
  1878     thy
  1850     |> gen_add_syntax_const (K I) target undef' (SOME (~1, pr))
  1879     |> add_syntax_const target undef' (SOME (~1, pr))
  1851   end;
  1880   end;
  1852 
  1881 
  1853 fun add_pretty_imperative_monad_bind target bind thy =
  1882 fun add_pretty_imperative_monad_bind target bind thy =
  1854   let
  1883   let
  1855     val (bind', _) = idfs_of_const thy bind;
  1884     val (bind', _) = idfs_of_const thy bind;
  1856     val pr = pretty_imperative_monad_bind
  1885     val pr = pretty_imperative_monad_bind
  1857   in
  1886   in
  1858     thy
  1887     thy
  1859     |> gen_add_syntax_const (K I) target bind' (SOME pr)
  1888     |> add_syntax_const target bind' (SOME pr)
  1860   end;
  1889   end;
  1861 
  1890 
  1862 val add_haskell_monad = gen_add_haskell_monad CodegenConsts.read_const;
  1891 val add_haskell_monad = gen_add_haskell_monad CodegenConsts.read_const;
  1863 
  1892 
  1864 val code_classP =
  1893 val code_classP =
  1865   OuterSyntax.command code_classK "define code syntax for class" K.thy_decl (
  1894   OuterSyntax.command code_classK "define code syntax for class" K.thy_decl (
  1866     parse_multi_syntax P.xname
  1895     parse_multi_syntax P.xname
  1867       (Scan.option (P.string -- Scan.optional (P.$$$ "where" |-- Scan.repeat1
  1896       (Scan.option (P.string -- Scan.optional (P.$$$ "where" |-- Scan.repeat1
  1868         (P.term --| (P.$$$ "\\<equiv>" || P.$$$ "==") -- P.string)) []))
  1897         (P.term --| (P.$$$ "\\<equiv>" || P.$$$ "==") -- P.string)) []))
  1869     >> (Toplevel.theory oo fold) (fn (target, syns) =>
  1898     >> (Toplevel.theory oo fold) (fn (target, syns) =>
  1870           fold (fn (raw_class, syn) => add_syntax_class target raw_class syn) syns)
  1899           fold (fn (raw_class, syn) => add_syntax_class_cmd target raw_class syn) syns)
  1871   );
  1900   );
  1872 
  1901 
  1873 val code_instanceP =
  1902 val code_instanceP =
  1874   OuterSyntax.command code_instanceK "define code syntax for instance" K.thy_decl (
  1903   OuterSyntax.command code_instanceK "define code syntax for instance" K.thy_decl (
  1875     parse_multi_syntax (P.xname --| P.$$$ "::" -- P.xname)
  1904     parse_multi_syntax (P.xname --| P.$$$ "::" -- P.xname)
  1876       ((P.minus >> K true) || Scan.succeed false)
  1905       ((P.minus >> K true) || Scan.succeed false)
  1877     >> (Toplevel.theory oo fold) (fn (target, syns) =>
  1906     >> (Toplevel.theory oo fold) (fn (target, syns) =>
  1878           fold (fn (raw_inst, add_del) => add_syntax_inst target raw_inst add_del) syns)
  1907           fold (fn (raw_inst, add_del) => add_syntax_inst_cmd target raw_inst add_del) syns)
  1879   );
  1908   );
  1880 
  1909 
  1881 val code_typeP =
  1910 val code_typeP =
  1882   OuterSyntax.command code_typeK "define code syntax for type constructor" K.thy_decl (
  1911   OuterSyntax.command code_typeK "define code syntax for type constructor" K.thy_decl (
  1883     parse_multi_syntax P.xname (parse_syntax I)
  1912     parse_multi_syntax P.xname (parse_syntax I)
  1884     >> (Toplevel.theory oo fold) (fn (target, syns) =>
  1913     >> (Toplevel.theory oo fold) (fn (target, syns) =>
  1885           fold (fn (raw_tyco, syn) => add_syntax_tyco target raw_tyco syn) syns)
  1914           fold (fn (raw_tyco, syn) => add_syntax_tyco_cmd target raw_tyco syn) syns)
  1886   );
  1915   );
  1887 
  1916 
  1888 val code_constP =
  1917 val code_constP =
  1889   OuterSyntax.command code_constK "define code syntax for constant" K.thy_decl (
  1918   OuterSyntax.command code_constK "define code syntax for constant" K.thy_decl (
  1890     parse_multi_syntax P.term (parse_syntax fst)
  1919     parse_multi_syntax P.term (parse_syntax fst)
  1891     >> (Toplevel.theory oo fold) (fn (target, syns) =>
  1920     >> (Toplevel.theory oo fold) (fn (target, syns) =>
  1892           fold (fn (raw_const, syn) => add_syntax_const target raw_const (no_bindings syn)) syns)
  1921           fold (fn (raw_const, syn) => add_syntax_const_cmd target raw_const (no_bindings syn)) syns)
  1893   );
  1922   );
  1894 
  1923 
  1895 val code_monadP =
  1924 val code_monadP =
  1896   OuterSyntax.command code_monadK "define code syntax for Haskell monads" K.thy_decl (
  1925   OuterSyntax.command code_monadK "define code syntax for Haskell monads" K.thy_decl (
  1897     P.term -- P.term -- P.term
  1926     P.term -- P.term -- P.term
  1922 val _ = OuterSyntax.add_parsers [code_classP, code_instanceP, code_typeP, code_constP,
  1951 val _ = OuterSyntax.add_parsers [code_classP, code_instanceP, code_typeP, code_constP,
  1923   code_reservedP, code_modulenameP, code_moduleprologP, code_monadP];
  1952   code_reservedP, code_modulenameP, code_moduleprologP, code_monadP];
  1924 
  1953 
  1925 (*including serializer defaults*)
  1954 (*including serializer defaults*)
  1926 val _ = Context.add_setup (
  1955 val _ = Context.add_setup (
  1927   gen_add_syntax_tyco (K I) "SML" "fun" (SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] =>
  1956   add_syntax_tyco "SML" "fun" (SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] =>
  1928       (gen_brackify (case fxy of NOBR => false | _ => eval_fxy (INFX (1, R)) fxy) o Pretty.breaks) [
  1957       (gen_brackify (case fxy of NOBR => false | _ => eval_fxy (INFX (1, R)) fxy) o Pretty.breaks) [
  1929         pr_typ (INFX (1, X)) ty1,
  1958         pr_typ (INFX (1, X)) ty1,
  1930         str "->",
  1959         str "->",
  1931         pr_typ (INFX (1, R)) ty2
  1960         pr_typ (INFX (1, R)) ty2
  1932       ]))
  1961       ]))
  1933   #> gen_add_syntax_tyco (K I) "OCaml" "fun" (SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] =>
  1962   #> add_syntax_tyco "OCaml" "fun" (SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] =>
  1934       (gen_brackify (case fxy of NOBR => false | _ => eval_fxy (INFX (1, R)) fxy) o Pretty.breaks) [
  1963       (gen_brackify (case fxy of NOBR => false | _ => eval_fxy (INFX (1, R)) fxy) o Pretty.breaks) [
  1935         pr_typ (INFX (1, X)) ty1,
  1964         pr_typ (INFX (1, X)) ty1,
  1936         str "->",
  1965         str "->",
  1937         pr_typ (INFX (1, R)) ty2
  1966         pr_typ (INFX (1, R)) ty2
  1938       ]))
  1967       ]))
  1939   #> gen_add_syntax_tyco (K I) "Haskell" "fun" (SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] =>
  1968   #> add_syntax_tyco "Haskell" "fun" (SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] =>
  1940       brackify_infix (1, R) fxy [
  1969       brackify_infix (1, R) fxy [
  1941         pr_typ (INFX (1, X)) ty1,
  1970         pr_typ (INFX (1, X)) ty1,
  1942         str "->",
  1971         str "->",
  1943         pr_typ (INFX (1, R)) ty2
  1972         pr_typ (INFX (1, R)) ty2
  1944       ]))
  1973       ]))