package forester

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file Sem.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
open Base
open Forester_prelude

module MethodTable = Map.Make (String)

module Text_modifier =
struct
  type t = Sentence_case | Identity
  [@@deriving show]
end

type modifier = Text_modifier.t
let pp_modifier = Text_modifier.pp

type node =
  | Text of string
  | Verbatim of string
  | Transclude of transclusion_opts * addr
  | Subtree of transclusion_opts * tree
  | Query_tree of transclusion_opts * query
  | Link of addr * t option * modifier
  | Xml_tag of xml_resolved_qname * (xml_resolved_qname * t) list * t
  | TeX_cs of TeX_cs.t
  | Math of math_mode * t
  | Embed_tex of embedded_tex
  | Img of string
  | Prim of Prim.t * t
  | Ref of addr
[@@deriving show]

and embedded_tex = {preamble : t; source : t}
[@@deriving show]

and transclusion_opts =
  {toc : bool;
   show_heading : bool;
   show_metadata : bool;
   title_override : t option;
   taxon_override : string option;
   expanded : bool;
   numbered : bool}
[@@deriving show]

and t = node Range.located list

and tree =
  {fm : frontmatter;
   bm : backmatter_section list;
   body : t}
[@@deriving show]

and frontmatter =
  {title : t option;
   taxon : string option;
   authors : addr list;
   contributors : addr list;
   dates : Date.t list;
   addr : addr;
   metas : (string * t) list;
   tags: string list;
   physical_parent : addr option;
   designated_parent : addr option;
   source_path : string option;
   number : string option}
[@@deriving show]

and backmatter_section =
  | Backmatter_section of {title: t; query : query}

and value =
  | VContent of t
  | VClo of value Env.t * Symbol.t binding list * Syn.t
  | VQuery_polarity of Query.polarity
  | VQuery_mode of Query.mode
  | VQuery of query
  | VSym of Symbol.t
  | VObject of Symbol.t
  | VAddr of addr
[@@deriving show]

and query =
  | Rel of Query.rel_query * addr
  | Isect of query list
  | Union of query list
  | Complement of query
  | Isect_fam of query * query_clo
  | Union_fam of query * query_clo
[@@deriving show]

and query_clo =
  | QClo of value Env.t * Symbol.t * Syn.t
  | QClo_rel of Query.rel_query

type obj_method =
  {body : Syn.t;
   self : Symbol.t;
   super : Symbol.t;
   env : value Env.t}

type obj =
  {prototype : Symbol.t option;
   methods : obj_method MethodTable.t}

let is_whitespace node =
  match Range.(node.value) with
  | Text txt -> String.trim txt = ""
  | _ -> false

let strip_whitespace =
  List.filter @@ fun x -> not @@ is_whitespace x

let trim_whitespace xs =
  let rec trim_front xs =
    match xs with
    | x :: xs when is_whitespace x ->
      trim_front xs
    | xs -> xs
  and trim_back xs =
    List.rev @@ trim_front @@ List.rev xs
  in
  trim_back @@ trim_front xs


let sentence_case nodes =
  let map_head f =
    function
    | [] -> []
    | x :: xs -> f x :: xs
  in
  let map_located f node =
    Range.{node with value = f node.value}
  in
  nodes |> map_head @@ map_located @@ function
  | Text str -> Text (String_util.sentence_case str)
  | Link (addr, title, _) -> Link (addr, title, Sentence_case)
  | node -> node


let apply_modifier =
  function
  | Text_modifier.Sentence_case -> sentence_case
  | Text_modifier.Identity -> Fun.id

(** Best-effort rendering of a nodes as a string, to use in text-only contexts.*)
let string_of_nodes =
  let rec render nodes =
    String.concat "" @@
    List.filter_map render_node nodes
  and render_node located =
    match Range.(located.value) with
    | Text s -> Some s
    | Verbatim s -> Some s
    | Link (_, Some title, _) -> Some (render title)
    | Xml_tag (_, _, bdy) | Math (_, bdy) -> Some (render bdy)
    | Embed_tex {source; _} -> Some (render source)
    | Prim (_, x) -> Some (render x)
    | Transclude _ | Subtree _ | Query_tree _ | TeX_cs _ | Img _ | Link _ | Ref _ -> None
  in
  render

module Util =
struct
  let peek_title (tree : tree) =
    match tree.fm.title with
    | Some ({value = Text txt; _} :: _) -> Some txt
    | _ -> None

  let peek_addr (tree : tree) =
    tree.fm.addr

  let tags (tree : tree) =
    tree.fm.tags

  let taxon (tree : tree) =
    tree.fm.taxon

  let authors (tree : tree) =
    tree.fm.authors

  let basic_comparator =
    let latest_date tree =
      let sorted_dates = tree.fm.dates |> List.sort (Compare.invert Date.compare) in
      List.nth_opt sorted_dates 0
    in
    let by_date =
      Fun.flip @@
      Compare.under latest_date @@
      Compare.option Date.compare
    in
    let by_title = Compare.under peek_title @@ Compare.option String.compare in
    Compare.cascade by_date by_title

  let sort =
    List.sort basic_comparator

  let sort_for_index =
    let by_has_parent = Compare.under (fun x -> Option.is_some x.fm.designated_parent) @@ Bool.compare in
    List.sort @@ Compare.cascade by_has_parent basic_comparator

end

module Query =
struct

  (** A heuristic for computing an intersection of queries. *)
  let rec query_cost q =
    match q with
    | Rel _ -> 200
    | Isect qs ->
      List.fold_left (fun i q -> min (query_cost q) i) 1000 qs
    | Union qs ->
      List.fold_left (fun i q -> max (query_cost q) i) 0 qs
    | Isect_fam (q, _) -> query_cost q
    | Union_fam (q, _) -> query_cost q
    | Complement _ -> 900

  let sort_by_ascending_cost qs =
    qs |> List.sort @@ fun q0 q1 ->
    compare (query_cost q0) (query_cost q1)

  let sort_by_descending_cost qs =
    qs |> List.sort @@ fun q0 q1 ->
    compare (query_cost q1) (query_cost q0)


  let rec isect qs =
    match sort_by_ascending_cost qs with
    | Isect qs :: qs' -> isect @@ qs @ qs'
    | qs -> Isect qs

  let rec union qs =
    match sort_by_descending_cost qs with
    | Union qs :: qs' -> union @@ qs @ qs'
    | qs -> Union qs

  let rec complement =
    function
    | Union qs -> isect @@ List.map complement qs
    | Complement q -> q
    | q -> Complement q

  let rel mode pol rel addr =
    Rel ((mode, pol, rel), addr)

  let tree_under x =
    rel Paths Outgoing Query.Rel.transclusion x

  let isect_fam_rel q mode pol rel =
    Isect_fam (q, QClo_rel (mode, pol, rel))

  let union_fam_rel q mode pol rel : query =
    Union_fam (q, QClo_rel (mode, pol, rel))

  let has_taxon taxon =
    rel Edges Incoming Query.Rel.taxa @@ User_addr taxon

  let hereditary_contributors addr =
    let q_non_ref_under =
      isect [
        tree_under addr;
        complement @@ has_taxon "reference"
      ]
    in
    let q_all_contributors =
      union_fam_rel
        q_non_ref_under
        Query.Edges
        Query.Outgoing
        Query.Rel.contributors
    in
    let q_authors = rel Edges Outgoing Query.Rel.authors addr in
    isect [q_all_contributors; complement q_authors]


  let references addr =
    isect [
      union_fam_rel (tree_under addr) Query.Edges Query.Outgoing Query.Rel.links;
      has_taxon "reference"
    ]

  let context addr =
    rel Edges Incoming Query.Rel.transclusion addr

  let backlinks addr =
    rel Edges Incoming Query.Rel.links addr

  let related addr =
    isect [
      rel Edges Outgoing Query.Rel.links addr;
      complement @@ has_taxon "reference"
    ]

  let contributions addr =
    union [
      rel Edges Incoming Query.Rel.authors addr;
      rel Edges Incoming Query.Rel.contributors addr
    ]

end

let empty_frontmatter ~addr =
  {addr;
   title = None;
   taxon = None;
   dates = [];
   authors = [];
   contributors = [];
   tags = [];
   metas = [];
   physical_parent = None;
   designated_parent = None;
   source_path = None;
   number = None}

let default_backmatter ~addr =
  let make_section title query =
    let title = [Range.locate_opt None @@ Text title] in
    Backmatter_section {title; query}
  in
  [make_section "references" @@ Query.references addr;
   make_section "context" @@ Query.context addr;
   make_section "backlinks" @@ Query.backlinks addr;
   make_section "related" @@ Query.related addr;
   make_section "contributions" @@ Query.contributions addr]

let empty_tree ~addr =
  {fm = empty_frontmatter ~addr;
   body = [];
   bm = default_backmatter ~addr}

let default_transclusion_opts : transclusion_opts =
  {title_override = None;
   taxon_override = None;
   show_heading = true;
   show_metadata = true;
   toc = true;
   expanded = true;
   numbered = true}

let extract_content (x : value Range.located) =
  match x.value with
  | VContent content -> content
  | _ -> Reporter.fatalf ?loc:x.loc Type_error "Expected content"

let extract_query_clo (x : value Range.located) =
  match x.value with
  | VClo (env, [Strict, x], body) -> QClo (env, x, body)
  | _ -> Reporter.fatalf ?loc:x.loc Type_error "Expected query closure"

let extract_string (x : value Range.located) =
  x |> extract_content |> string_of_nodes

let extract_obj_ptr (x : value Range.located) =
  match x.value with
  | VObject sym -> sym
  | _ -> Reporter.fatalf ?loc:x.loc Type_error "Expected object"

let extract_sym (x : value Range.located) =
  match x.value with
  | VSym x -> x
  | _ -> Reporter.fatalf ?loc:x.loc Type_error "Expected symbol"

let extract_query_node (x : value Range.located) =
  match x.value with
  | VQuery q -> q
  | _ -> Reporter.fatalf ?loc:x.loc Type_error "Expected query node"

let extract_query_polarity (x : value Range.located) =
  match x.value with
  | VQuery_polarity pol -> pol
  | _ -> Reporter.fatalf ?loc:x.loc Type_error "Expected query polarity"

let extract_query_mode (x : value Range.located) =
  match x.value with
  | VQuery_mode mode -> mode
  | _ -> Reporter.fatalf ?loc:x.loc Type_error "Expected query mode"

let extract_addr (x : value Range.located) =
  match x.value with
  | VContent content -> User_addr (string_of_nodes content)
  | VAddr addr -> addr
  | _ -> Reporter.fatalf ?loc:x.loc Type_error "Expected tree address"

OCaml

Innovation. Community. Security.