Debian Codegraph @

apps/json/src/rocq_elpi_json_plugin.ml

raw symbols SCIP
1 open Elpi.API
2
3 (* todo, move this in Elpi, there are already two copies of this code
4 in rocq-elpi *)
5 let listC (d : ('a,'h,'cst) ContextualConversion.t) =
6 let embed ~depth h cst s l =
7 let s, l, eg = Utils.map_acc (d.ContextualConversion.embed ~depth h cst ) s l in
8 s, Utils.list_to_lp_list l, eg in
9 let readback ~depth h cst s t =
10 Utils.map_acc (d.ContextualConversion.readback ~depth h cst) s
11 (Utils.lp_list_to_list ~depth t)
12 in
13 let pp fmt l =
14 Format.fprintf fmt "[%a]" (RawPp.list d.ContextualConversion.pp ~boxed:true "; ") l in
15 { ContextualConversion.embed; readback;
16 ty = TyApp ("list",d.ContextualConversion.ty,[]);
17 pp;
18 pp_doc = (fun fmt () -> ()) }
19
20 let pairC = ContextualConversion.(!>>> ) Elpi.Builtin.pair
21 let stringC = ContextualConversion.(!> ) BuiltInData.string
22
23 let json : Yojson.Basic.t Conversion.t = AlgebraicData.(declare {
24 ty = Conversion.TyName "json";
25 doc = "Yojson.Basic.t";
26 pp = Yojson.Basic.pp;
27 constructors = [
28 K("json.null","",N,B `Null,M (fun ~ok ~ko -> function `Null -> ok | _ -> ko ()));
29 K("json.bool","",A(Elpi.Builtin.bool,N),B (function x -> `Bool x),M (fun ~ok ~ko -> function `Bool x -> ok x | _ -> ko ()));
30 K("json.int","",A(BuiltInData.int,N),B (function x -> `Int x),M (fun ~ok ~ko -> function `Int x -> ok x | _ -> ko ()));
31 K("json.float","",A(BuiltInData.float,N),B (function x -> `Float x),M (fun ~ok ~ko -> function `Float x -> ok x | _ -> ko ()));
32 K("json.string","",A(BuiltInData.string,N),B (function x -> `String x),M (fun ~ok ~ko -> function `String x -> ok x | _ -> ko ()));
33 K("json.assoc","key value pairs. When generated from built-in predicates, keys are sorted",C((fun x -> listC (pairC stringC x)),N),
34 B (function x -> `Assoc x),
35 M (fun ~ok ~ko -> function `Assoc x ->
36 let sx = List.stable_sort (fun (x,_) (y,_) -> String.compare x y) x in
37 ok sx | _ -> ko ()));
38 K("json.list","",C(listC,N),B (function x -> `List x),M (fun ~ok ~ko -> function `List x -> ok x | _ -> ko ()));
39 ];
40 }
41 ) |> ContextualConversion.(!<)
42
43 open BuiltIn
44 open BuiltInPredicate
45 open BuiltInPredicate.Notation
46 open BuiltInData
47 open Elpi.Builtin
48 let json_from_file = MLCode(Pred("json.from_file",
49 In(string,"File",
50 Out(json,"J",
51 Out(diagnostic,"D",
52 Easy {|like Yojson.Basic.from_file reads J from File.
53 If D is error then J is not assigned.|}))),
54 (fun f _ _ ~depth ->
55 try !: (Yojson.Basic.from_file ~fname:f f) +! mkOK
56 with Yojson.Json_error s | Sys_error s -> ?: None +! (mkERROR s)
57 )),DocAbove)
58
59 let json_from_string = MLCode(Pred("json.from_string",
60 In(string,"String",
61 Out(json,"J",
62 Out(diagnostic,"D",
63 Easy {|like Yojson.Basic.from_string reads J from String.
64 If D is error then J is not assigned.|}))),
65 (fun f _ _ ~depth ->
66 try !: (Yojson.Basic.from_string f) +! mkOK
67 with Yojson.Json_error s -> ?: None +! (mkERROR s)
68 )),DocAbove)
69
70 let json_to_file = MLCode(Pred("json.to_file",
71 In(string,"File",
72 In(json,"J",
73 Out(diagnostic,"D",
74 Easy {|like Yojson.Basic.to_file write J to File.|}))),
75 (fun f j _ ~depth ->
76 try Yojson.Basic.to_file f j; !: mkOK
77 with Yojson.Json_error s | Sys_error s -> !: (mkERROR s)
78 )),DocAbove)
79
80 let json_to_string = MLCode(Pred("json.to_string",
81 In(json,"J",
82 Out(string,"S",
83 Out(diagnostic,"D",
84 Easy {|like Yojson.Basic.to_string write J to a compact string S.|}))),
85 (fun j _ _ ~depth ->
86 try !: (Yojson.Basic.to_string ~std:true j) +! mkOK
87 with Yojson.Json_error s -> ?: None +! (mkERROR s)
88 )),DocAbove)
89
90 let json_to_string_pretty = MLCode(Pred("json.pretty_to_string",
91 In(json,"J",
92 Out(string,"S",
93 Out(diagnostic,"D",
94 Easy {|like Yojson.Basic.pretty_to_string write J to a nice string S.|}))),
95 (fun j _ _ ~depth ->
96 try !: (Yojson.Basic.pretty_to_string ~std:true j) +! mkOK
97 with Yojson.Json_error s -> ?: None +! (mkERROR s)
98 )),DocAbove)
99
100 let builtins =
101 declare ~file_name:"json.elpi" [
102 LPDoc {|Yojson bindings for Elpi|};
103 LPDoc {|license: GNU Lesser General Public License Version 2.1 or later|};
104 LPDoc {|------------------------------------------------------------------------|};
105 LPDoc {|This code is not Rocq specific, i.e. could go to an Elpi specific package.|};
106 MLData json;
107 json_from_file;
108 json_from_string;
109 json_to_file;
110 json_to_string;
111 json_to_string_pretty;
112 LPDoc {|------------------------------------------------------------------------|};
113 LPDoc {|Rocq-Elpi APIs|};
114 LPDoc {|------------------------------------------------------------------------|};
115 LPCode {|
116 func coq.json->string json -> string.
117 coq.json->string J S :- json.pretty_to_string J S ok.
118
119 func coq.string->json string -> json.
120 coq.string->json S J :- json.from_string S J ok.
121
122 % [coq.extra-dep->json Dep J D] reads a json file declared as an extra
123 % dependency, see the coq.extra-dep API.
124 func coq.extra-dep->json string -> json, diagnostic.
125 coq.extra-dep->json S J D :- coq.extra-dep S (some P), !, json.from_file P J D.
126 coq.extra-dep->json S _ (error Msg) :- Msg is "unknown Extra Dependency " ^ S.
127
128 |}
129 ]