package forester
A tool for tending mathematical forests
Install
Dune Dependency
Authors
Maintainers
Sources
4.0.1.tar.gz
md5=1781b3d67f7ff4edaeb5cfdedba46b9e
sha512=879a583fd3ddf1cf44039fbe9a1d7280e9cc9cc0340506561dc8c6bb6b792db7c512a682d4d09d6c6b0b3b14940665573c9d94580c9ce0a87ee05822fbe4c229
doc/src/forester.core/Expand.ml.html
Source file Expand.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
open Prelude open Resolver open Bwd module Set = Set.Make (String) module UnitMap = Map.Make (String) type exports = P.data Trie.Untagged.t module U = Algaeff.State.Make (struct type t = exports UnitMap.t end) module Builtins = struct let create path = let sym = Symbol.fresh path in sym, fun () -> Resolver.Scope.include_singleton (path, (Sym sym, ())) module Transclude = struct let title_sym, alloc_title = create ["transclude"; "title"] let taxon_sym, alloc_taxon = create ["transclude"; "taxon"] let expanded_sym, alloc_expanded = create ["transclude"; "expanded"] let show_heading_sym, alloc_show_heading = create ["transclude"; "heading"] let toc_sym, alloc_toc = create ["transclude"; "toc"] let numbered_sym, alloc_numbered = create ["transclude"; "numbered"] let show_metadata_sym, alloc_show_metadata = create ["transclude"; "metadata"] end end let rec expand : Code.t -> Syn.t = function | [] -> [] | {value = Prim (p, x); loc} :: rest -> {value = Syn.Prim (p, expand x); loc} :: expand rest | {value = Text x; loc} :: rest -> {value = Syn.Text x; loc} :: expand rest | {value = Namespace (path, body); loc} :: rest -> let result = Scope.section path @@ fun () -> expand body in result @ expand rest | {value = Open path; loc} :: rest -> Scope.section [] @@ fun () -> begin Scope.modify_visible @@ Resolver.Lang.union [ Resolver.Lang.all; Resolver.Lang.renaming path [] ] end; expand rest | {value = Group (Squares, title); loc = loc1} :: {value = Group (Parens, dest); loc = loc2} :: rest -> let dest = expand dest in let title = Option.some @@ expand title in let link = Syn.Link {dest; title} in {value = link; loc = loc1} :: expand rest | {value = Group (Squares, [{value = Group (Squares, dest); _}]); loc} :: rest -> let dest = expand dest in {value = Syn.Link {dest; title = None}; loc} :: expand rest | {value = Group (d, xs); loc} :: rest -> {value = Syn.Group (d, expand xs); loc} :: expand rest | {value = Transclude addr; loc} :: rest -> {value = Syn.Transclude addr; loc} :: expand rest | {value = Subtree (addr, nodes); loc} :: rest -> let subtree = expand_tree_inner @@ Code.{source_path = None; addr = addr; code = nodes} in {value = Syn.Subtree (addr, subtree); loc} :: expand rest | {value = Query query; loc} :: rest -> {value = Syn.Query (Query.map expand query); loc} :: expand rest | {value = Embed_tex (preamble, source); loc} :: rest -> {value = Syn.Embed_tex {preamble = expand preamble; source = expand source}; loc} :: expand rest | {value = Math (m, xs); loc} :: rest -> {value = Syn.Math (m, expand xs); loc} :: expand rest | {value = Ident (path, methods); loc} :: rest -> let rec loop acc = function | [] -> acc | m :: ms -> loop [Range.{value = Syn.Call (acc, m); loc}] ms in loop (expand_ident loc path) methods @ expand rest | {value = Ref x; loc} :: rest -> {value = Syn.Ref (expand x); loc} :: expand rest | {value = Scope body; _} :: rest -> let body = Scope.section [] @@ fun () -> expand body in body @ expand rest | {value = Put (k, v); loc} :: rest -> let k = expand_sym loc k in let v = expand v in [{value = Syn.Put (k, v, expand rest); loc}] | {value = Default (k, v); loc} :: rest -> let k = expand_sym loc k in let v = expand v in [{value = Syn.Default (k, v, expand rest); loc}] | {value = Get k; loc} :: rest -> let k = expand_sym loc k in {value = Syn.Get k; loc} :: expand rest | {value = Object {self; methods}; loc} :: rest -> let self, methods = Scope.section [] @@ fun () -> let sym = Symbol.fresh [] in let var = Range.{value = Syn.Var sym; loc} in self |> Option.iter (fun self -> Scope.import_subtree ([], Trie.Untagged.singleton (self, Resolver.P.Term [var]))); sym, List.map expand_method methods in {value = Syn.Object {self; methods}; loc} :: expand rest | {value = Patch {obj; self; methods}; loc} :: rest -> let self, super, methods = Scope.section [] @@ fun () -> let self_sym = Symbol.fresh [] in let super_sym = Symbol.fresh [] in let self_var = Range.locate_opt None @@ Syn.Var self_sym in let super_var = Range.locate_opt None @@ Syn.Var super_sym in self |> Option.iter begin fun self -> Scope.import_subtree ([], Trie.Untagged.singleton (self, Resolver.P.Term [self_var])); Scope.import_subtree ([], Trie.Untagged.singleton (self @ ["super"], Resolver.P.Term [super_var])); end; self_sym, super_sym, List.map expand_method methods in let patched = Syn.Patch {obj = expand obj; self; super; methods} in {value = patched; loc} :: expand rest | {value = Call (obj, method_name); loc} :: rest -> {value = Syn.Call (expand obj, method_name); loc} :: expand rest | {value = If_tex (x, y); loc} :: rest -> let x = expand x in let y = expand y in {value = Syn.If_tex (x, y); loc} :: expand rest | {value = Xml_tag (title, attrs, body); loc} :: rest -> let title = expand_xml_ident loc title in let attrs = attrs |> List.map @@ fun (k, v) -> expand_xml_ident loc k, expand v in let body = expand body in {value = Syn.Xml_tag (title, attrs, body); loc} :: expand rest | {value = Import (vis, dep); loc} :: rest -> let import = UnitMap.find_opt dep @@ U.get () in begin match import with | None -> Reporter.emitf ?loc:loc Tree_not_found "Could not find tree %s" dep; expand rest | Some tree -> begin match vis with | Public -> Resolver.Scope.include_subtree ([], tree) | Private -> Resolver.Scope.import_subtree ([], tree) end; expand rest end; | {value = Let (a, bs, def); loc} :: rest -> let singl = Trie.Untagged.singleton (a, Resolver.P.Term (expand_lambda loc (bs, def))) in Resolver.Scope.section [] @@ fun _ -> Resolver.Scope.import_subtree ([], singl); expand rest | {value = Def (path, xs, body); loc} :: rest -> let lam = expand_lambda loc (xs, body) in Resolver.Scope.include_singleton (path, (Term lam, ())); expand rest | {value = Decl_xmlns (prefix, xmlns); loc} :: rest -> let path = ["xmlns"; prefix] in let singl = Trie.Untagged.singleton (path, Resolver.P.Xmlns {prefix; xmlns}) in Resolver.Scope.include_singleton (path, (Xmlns {prefix; xmlns}, ())); expand rest | {value = Alloc path; loc} :: rest -> let symbol = Symbol.fresh path in Resolver.Scope.include_singleton (path, (Sym symbol, ())); expand rest | {value = Title xs; loc} :: rest -> {value = Syn.Title (expand xs); loc} :: expand rest | {value = Parent addr; loc} :: rest -> {value = Syn.Parent addr; loc} :: expand rest | {value = Author ; loc} :: rest -> {value = Syn.Author author; loc} :: expand rest | {value = Contributor ; loc} :: rest -> {value = Syn.Contributor author; loc} :: expand rest | {value = Tag tag; loc} :: rest -> {value = Syn.Tag tag; loc} :: expand rest | {value = Taxon taxon; loc} :: rest -> {value = Syn.Taxon taxon; loc} :: expand rest | {value = Date str; loc} :: rest -> {value = Syn.Date str; loc} :: expand rest | {value = Number str; loc} :: rest -> {value = Syn.Number str; loc} :: expand rest | {value = Meta (k, v); loc} :: rest -> {value = Syn.Meta (k, expand v); loc} :: expand rest and expand_method (key, body) = key, expand body and expand_lambda loc : Trie.path list * Code.t -> Syn.t = fun (xs, body) -> Scope.section [] @@ fun () -> let syms = xs |> List.map @@ fun x -> let sym = Symbol.fresh x in let var = Range.locate_opt None @@ Syn.Var sym in let singlx = Trie.Untagged.singleton (x, Resolver.P.Term [var]) in Scope.import_subtree ([], singlx); sym in [Range.{value = Syn.Lam (syms, expand body); loc}] and expand_ident loc path = match Scope.resolve path, path with | None, [name] -> [Range.{value = Syn.Unresolved name; loc}] | None, _ -> Reporter.fatalf ?loc Resolution_error "path %a could not be resolved" Trie.pp_path path | Some (Term x, ()), _ -> let relocate Range.{value; _} = Range.{value; loc} in List.map relocate x | Some (Sym x, ()), _ -> Reporter.fatalf ?loc Resolution_error "path %a resolved to symbol %a instead of term" Trie.pp_path path Symbol.pp x | Some (Xmlns {xmlns; prefix}, ()), _ -> Reporter.fatalf ?loc Resolution_error "path %a resolved to xmlns:%s=\"%s\" instead of term" Trie.pp_path path xmlns prefix and expand_sym loc path = match Scope.resolve path, path with | Some (Sym x, ()), _ -> x | _ -> Reporter.fatalf ?loc Resolution_error "expected path `%a` to resolve to symbol" Trie.pp_path path and expand_xml_ident loc (prefix, uname) = match prefix with | None -> Base.Xml_resolved_qname {xmlns = None; prefix = None; uname} | Some prefix -> match Scope.resolve ["xmlns"; prefix] with | Some (Xmlns {xmlns; prefix}, ()) -> Base.Xml_resolved_qname {xmlns = Some xmlns; prefix = Some prefix; uname} | _ -> Reporter.fatalf ?loc Resolution_error "expected path `%s` to resolve to xmlns" prefix and expand_tree_inner (tree : Code.tree) : Syn.tree = let trace f = match tree.addr with | Some addr -> Reporter.tracef "when expanding tree at address `%s`" addr f | None -> f () in trace @@ fun () -> Scope.section [] @@ fun () -> let units = U.get () in let syn = expand tree.code in let exports = Resolver.Scope.get_export () in let units = match tree.addr with | None -> units | Some addr -> UnitMap.add addr exports units in U.set units; syn let expand_tree (units : exports UnitMap.t) (tree : Code.tree) = let trace f = match tree.addr with | Some addr -> Reporter.tracef "when expanding tree at address `%s`" addr f | None -> f () in U.run ~init:units @@ fun () -> Resolver.Scope.run @@ fun () -> Builtins.Transclude.alloc_title (); Builtins.Transclude.alloc_taxon (); Builtins.Transclude.alloc_expanded (); Builtins.Transclude.alloc_show_heading (); Builtins.Transclude.alloc_toc (); Builtins.Transclude.alloc_numbered (); Builtins.Transclude.alloc_show_metadata (); let tree = expand_tree_inner tree in let units = U.get () in units, tree
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>