1 (* Author: Florian Haftmann, TU Muenchen
3 Code generation for list literals.
8 val implode_list: string -> string -> Code_Thingol.iterm -> Code_Thingol.iterm list option
9 val default_list: int * string
10 -> (Code_Printer.fixity -> Code_Thingol.iterm -> Pretty.T)
11 -> Code_Printer.fixity -> Code_Thingol.iterm -> Code_Thingol.iterm -> Pretty.T
12 val add_literal_list: string -> theory -> theory
15 structure List_Code : LIST_CODE =
18 open Basic_Code_Thingol;
20 fun implode_list nil' cons' t =
22 fun dest_cons (IConst (c, _) `$ t1 `$ t2) =
27 val (ts, t') = Code_Thingol.unfoldr dest_cons t;
29 of IConst (c, _) => if c = nil' then SOME ts else NONE
33 fun default_list (target_fxy, target_cons) pr fxy t1 t2 =
34 Code_Printer.brackify_infix (target_fxy, Code_Printer.R) fxy (
35 pr (Code_Printer.INFX (target_fxy, Code_Printer.X)) t1,
36 Code_Printer.str target_cons,
37 pr (Code_Printer.INFX (target_fxy, Code_Printer.R)) t2
40 fun add_literal_list target =
42 fun pretty literals [nil', cons'] pr thm vars fxy [(t1, _), (t2, _)] =
43 case Option.map (cons t1) (implode_list nil' cons' t2)
45 Code_Printer.literal_list literals (map (pr vars Code_Printer.NOBR) ts)
47 default_list (Code_Printer.infix_cons literals) (pr vars) fxy t1 t2;
48 in Code_Target.add_syntax_const target
49 @{const_name Cons} (SOME (2, ([@{const_name Nil}, @{const_name Cons}], pretty)))