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 |
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 ])) |