package lambdapi
All checked builds pass sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
Proof assistant for the λΠ-calculus modulo rewriting
Install
dune-project
Dependency
Authors
Maintainers
Sources
lambdapi-4.0.0.tbz
sha256=0e2e73e0ea0c674f1263bf85ed3453ed3d6084d38eea29974a03ba9e382d29d7
sha512=2831db57fcd09206687a92fc9ee7d363e87bcec5167949e9c8161d41c63d5887b9f3556bfd63a7a9a0c12e5a5dc61a397baf4f496ebdb2a2fce122142c05e7a1
doc/src/lambdapi.core/sign.ml.html
Source file sign.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 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449(** Signature for symbols. *) open Lplib open Extra open Common open Error open Pos open Timed open Term (** Data associated to inductive types. *) type ind_data = { ind_cons : sym list (** Constructors. *) ; ind_prop : sym (** Induction principle. *) ; ind_nb_params : int (** Number of parameters. *) ; ind_nb_types : int (** Number of mutually defined types. *) ; ind_nb_cons : int (** Number of constructors. *) } (** Rules and notation added to an external symbol. *) type sym_data = { rules : rule list ; nota : float notation option } (** Data associated to a dependency. *) type dep_data = { dep_symbols : sym_data StrMap.t ; dep_open : bool } (** Representation of a signature, that is, what is introduced by a module/file. Signatures must be created with the functions [create] or [read] below only.*) type t = { sign_symbols : sym StrMap.t ref ; sign_path : Path.t ; sign_deps : dep_data Path.Map.t ref ; sign_builtins : sym StrMap.t ref ; sign_ind : ind_data SymMap.t ref ; sign_cp_pos : cp_pos list SymMap.t ref } (** [mem sign name] checks whether a symbol named [name] exists in [sign]. *) let mem : t -> string -> bool = fun sign name -> StrMap.mem name !(sign.sign_symbols) (** [find sign name] finds the symbol named [name] in signature [sign] if it exists, and raises the [Not_found] exception otherwise. *) let find : t -> string -> sym = fun sign name -> StrMap.find name !(sign.sign_symbols) (** [loaded] stores the signatures of the known (already compiled or currently being compiled) modules. The current module is stored in [loaded] so that the symbols that it contains can be qualified with the name of the module. This behavior was inherited from previous versions of Dedukti. *) let loaded : t Path.Map.t ref = ref Path.Map.empty (** [find_qualified path name] returns the symbol identified by [path] and [name] in the known modules (already compiled or currently being compiled) *) let find_qualified : Path.t -> string -> sym = fun path name -> find (Path.Map.find path !loaded) name (** Signature for symbols introduced by Lambdapi and not the user (e.g. unification/coercion rules, strings) and always loaded. *) module Ghost = struct let path = Path.ghost "ghost" let sign = { sign_symbols = ref StrMap.empty ; sign_path = path ; sign_deps = ref Path.Map.empty ; sign_builtins = ref StrMap.empty ; sign_ind = ref SymMap.empty ; sign_cp_pos = ref SymMap.empty } let _ = loaded := Path.Map.add path sign !loaded (** [iter f] iters function [f] on ghost symbols. *) let iter : (sym -> unit) -> unit = fun f -> StrMap.iter (fun _ s -> f s) !(sign.sign_symbols) end (** [create path] creates a new signature with path [path] and the ghost module as dependency. *) let create : Path.t -> t = fun sign_path -> let dep = {dep_symbols = StrMap.empty; dep_open=true} in let sign_deps = ref (Path.Map.singleton Ghost.path dep) in { sign_symbols = ref StrMap.empty ; sign_path ; sign_deps ; sign_builtins = ref StrMap.empty ; sign_ind = ref SymMap.empty ; sign_cp_pos = ref SymMap.empty } (** [loading] contains the modules that are being processed. They are stored in a stack due to dependencies. Note that the topmost element corresponds to the current module. If a module appears twice in the stack, then there is a circular dependency. *) let loading : Path.t list ref = ref [] (** [current_path ()] returns the current signature path. *) let current_path : unit -> Path.t = fun () -> (Path.Map.find (List.hd !loading) !loaded).sign_path (** [link sign] establishes physical links to the external symbols. *) let link : t -> unit = fun sign -> let link_symb s = if s.sym_path = sign.sign_path then s else try find (Path.Map.find s.sym_path !loaded) s.sym_name with Not_found -> if String.is_string_literal s.sym_name then begin Ghost.sign.sign_symbols := StrMap.add s.sym_name s !(Ghost.sign.sign_symbols); s end else assert false in let link_term mk_Appl = let rec link_term t = match unfold t with | Type | Kind | Vari _ | Plac _ -> t | Symb s -> mk_Symb(link_symb s) | Prod(a,b) -> mk_Prod(link_term a, binder link_term b) | Abst(a,b) -> mk_Abst(link_term a, binder link_term b) | LLet(a,t,b) -> mk_LLet(link_term a, link_term t, binder link_term b) | Appl(a,b) -> mk_Appl(link_term a, link_term b) | Patt(i,n,ts)-> mk_Patt(i, n, Array.map link_term ts) | Bvar _ -> assert false | Meta _ -> assert false | Wild -> assert false | TRef _ -> assert false in link_term in let link_lhs = link_term mk_Appl_not_canonical and link_term = link_term mk_Appl in let link_rule r = let lhs = List.map link_lhs r.lhs in let rhs = link_term r.rhs in {r with lhs ; rhs} in let f _ s = s.sym_type := link_term !(s.sym_type); s.sym_def := Option.map link_term !(s.sym_def); s.sym_rules := List.map link_rule !(s.sym_rules); Tree.update s in StrMap.iter f !(sign.sign_symbols); let f mp {dep_symbols=sm; _} = if sm <> Extra.StrMap.empty then let sign = try Path.Map.find mp !loaded with Not_found -> assert false in let g n sd = let s = try find sign n with Not_found -> assert false in s.sym_rules := !(s.sym_rules) @ List.map link_rule sd.rules; Option.iter (fun n -> s.sym_nota := n) sd.nota; Tree.update s in StrMap.iter g sm in Path.Map.iter f !(sign.sign_deps); sign.sign_builtins := StrMap.map link_symb !(sign.sign_builtins); let link_ind_data i = { ind_cons = List.map link_symb i.ind_cons; ind_prop = link_symb i.ind_prop; ind_nb_params = i.ind_nb_params; ind_nb_types = i.ind_nb_types; ind_nb_cons = i.ind_nb_cons } in let f s i m = SymMap.add (link_symb s) (link_ind_data i) m in sign.sign_ind := SymMap.fold f !(sign.sign_ind) SymMap.empty; let link_cp_pos (pos,l,r,p,l_p) = pos, link_lhs l, link_term r, p, link_lhs l_p in let f s cps m = SymMap.add (link_symb s) (List.map link_cp_pos cps) m in sign.sign_cp_pos := SymMap.fold f !(sign.sign_cp_pos) SymMap.empty let link s = Debug.(record_time Sharing (fun () -> link s)) (** [unlink sign] removes references to external symbols (and thus signatures) in the signature [sign]. This function is used to minimize the size of object files, by preventing a recursive inclusion of all the dependencies. Note however that [unlink] processes [sign] in place, which means that the signature is invalidated in the process. *) let unlink : t -> unit = fun sign -> let unlink_sym s = s.sym_dtree := Tree_type.empty_dtree; if s.sym_path <> sign.sign_path then (s.sym_type := mk_Kind; s.sym_rules := []) in let rec unlink_term t = match unfold t with | Symb s -> unlink_sym s | Prod(a,b) | Abst(a,b) -> unlink_term a; unlink_term (snd(unbind b)) | LLet(a,t,b) -> unlink_term a; unlink_term t; unlink_term (snd(unbind b)) | Appl(a,b) -> unlink_term a; unlink_term b | Meta _ -> assert false | Wild -> assert false | TRef _ -> assert false | Bvar _ -> assert false | Plac _ | Vari _ | Patt _ | Type | Kind -> () in let unlink_rule r = List.iter unlink_term r.lhs; unlink_term r.rhs in let f _ s = unlink_term !(s.sym_type); Option.iter unlink_term !(s.sym_def); List.iter unlink_rule !(s.sym_rules) in StrMap.iter f !(sign.sign_symbols); let f _ {dep_symbols=sm; _} = StrMap.iter (fun _ sd -> List.iter unlink_rule sd.rules) sm in Path.Map.iter f !(sign.sign_deps); StrMap.iter (fun _ s -> unlink_sym s) !(sign.sign_builtins); let unlink_ind_data i = List.iter unlink_sym i.ind_cons; unlink_sym i.ind_prop in let f s i = unlink_sym s; unlink_ind_data i in SymMap.iter f !(sign.sign_ind); let unlink_cp_pos (_,l,r,_,l_p) = unlink_term l; unlink_term r; unlink_term l_p in let f s cps = unlink_sym s; List.iter unlink_cp_pos cps in SymMap.iter f !(sign.sign_cp_pos) (** [add_symbol_callback] is used to index symbols in the document currently edited in Spl mode*) let add_symbol_callback = Stdlib.ref (fun ~path:_ _ -> ()) (** [add_rules_callback] is used to index rules in the document currently edited in Spl mode*) let add_rules_callback = Stdlib.ref (fun ~path:_ _ _ -> ()) (** [add_symbol sign expo prop mstrat opaq name pos typ impl notation] adds in the signature [sign] a symbol with name [name], exposition [expo], property [prop], matching strategy [strat], opacity [opaq], type [typ], implicit arguments [impl], no notation, no definition and no rules. [name] should not already be used in [sign]. [pos] is the position of the declaration (without its definition). The created symbol is returned. *) let add_symbol : t -> expo -> prop -> match_strat -> bool -> strloc -> popt -> term -> bool list -> sym = fun sign sym_expo sym_prop sym_mstrat sym_opaq name pos typ impl -> let sym = create_sym sign.sign_path sym_expo sym_prop sym_mstrat sym_opaq name pos (cleanup typ) (minimize_impl impl) in let f = function | None -> Some sym | Some _ -> fatal_no_pos "Bug: symbol %a already declared." Term.qsym sym in sign.sign_symbols := StrMap.update name.elt f !(sign.sign_symbols); if Stdlib.(!Common.Console.lsp_mod) then Stdlib.(!add_symbol_callback ~path:sign.sign_path sym) ; sym (** [strip_private sign] removes private symbols from signature [sign]. *) let strip_private : t -> unit = fun sign -> let not_prv sym = not (Term.is_private sym) in sign.sign_symbols := StrMap.filter (fun _ s -> not_prv s) !(sign.sign_symbols) (** [write sign file] writes the signature [sign] to the file [fname]. *) let write : t -> string -> unit = fun sign fname -> (* [Unix.fork] is used to safely [unlink] and write an object file, while preserving a valid copy of the written signature in the parent process. *) match Unix.fork () with | 0 -> begin let oc = open_out fname in try unlink sign; Marshal.to_channel oc sign [Marshal.Closures]; close_out oc; Stdlib.(Debug.do_print_time := false); exit 0 with e -> close_out oc; Sys.remove fname; raise e end | i -> ignore (Unix.waitpid [] i); Stdlib.(Debug.do_print_time := true) let write s n = Debug.(record_time Writing (fun () -> write s n)) (** [read fname] reads a signature from the object file [fname]. Note that the file can only be read properly if it was build with the same binary as the one being evaluated. If this is not the case, the program gracefully fails with an error message. *) (* NOTE here, we rely on the fact that a marshaled closure can only be read by processes running the same binary as the one that produced it. *) let read : string -> t = fun fname -> let ic = open_in fname in let sign = try let sign = Marshal.from_channel ic in close_in ic; sign with Failure _ -> close_in ic; fatal_no_pos "File \"%s\" is incompatible with current binary." fname in (* Timed references need reset after unmarshaling (see [Timed] doc). *) unsafe_reset sign.sign_symbols; unsafe_reset sign.sign_deps; unsafe_reset sign.sign_builtins; unsafe_reset sign.sign_ind; unsafe_reset sign.sign_cp_pos; let shallow_reset_sym s = unsafe_reset s.sym_type; unsafe_reset s.sym_def; unsafe_reset s.sym_rules; (* s.sym_dtree is not reset since it is recomputed. *) in let rec reset_term t = match unfold t with | Type | Kind | Vari _ | Plac _ -> () | Symb s -> shallow_reset_sym s | Prod(a,b) | Abst(a,b) -> reset_term a; reset_term (snd (unbind b)) | LLet(a,t,b) -> reset_term a; reset_term t; reset_term (snd(unbind b)) | Appl(a,b) -> reset_term a; reset_term b | Patt(_,_,ts) -> Array.iter reset_term ts | Bvar _ -> assert false | TRef _ -> assert false | Wild -> assert false | Meta _ -> assert false in let reset_rule r = List.iter reset_term r.lhs; reset_term r.rhs in let reset_sym s = shallow_reset_sym s; reset_term !(s.sym_type); Option.iter reset_term !(s.sym_def); List.iter reset_rule !(s.sym_rules) in StrMap.iter (fun _ s -> reset_sym s) !(sign.sign_symbols); StrMap.iter (fun _ s -> shallow_reset_sym s) !(sign.sign_builtins); let f _ {dep_symbols=sm; _} = StrMap.iter (fun _ sd -> List.iter reset_rule sd.rules) sm in Path.Map.iter f !(sign.sign_deps); let reset_ind i = shallow_reset_sym i.ind_prop; List.iter shallow_reset_sym i.ind_cons in let f s i = shallow_reset_sym s; reset_ind i in SymMap.iter f !(sign.sign_ind); let reset_cp_pos (_,l,r,_,l_p) = reset_term l; reset_term r; reset_term l_p in let f s cps = shallow_reset_sym s; List.iter reset_cp_pos cps in SymMap.iter f !(sign.sign_cp_pos); sign let read = let open Stdlib in let r = ref Ghost.sign in fun n -> Debug.(record_time Reading (fun () -> r := read n)); !r (** [add_rules sign s rs] adds the new rules [rs] to the symbol [s]. When the rules do not correspond to a symbol of signature [sign], they are stored in its dependencies. /!\ does not update the decision tree or the critical pairs. *) let add_rules : t -> sym -> rule list -> unit = fun sign s rs -> s.sym_rules := !(s.sym_rules) @ rs; if s.sym_path <> sign.sign_path then begin let d = try Path.Map.find s.sym_path !(sign.sign_deps) with Not_found -> assert false in let f = function | None -> Some{rules=rs; nota=None} | Some sd -> Some{sd with rules=sd.rules@rs} in let sm = StrMap.update s.sym_name f d.dep_symbols in let d = {d with dep_symbols=sm} in sign.sign_deps := Path.Map.add s.sym_path d !(sign.sign_deps) end ; if Stdlib.(!Common.Console.lsp_mod) then Stdlib.(!add_rules_callback ~path:sign.sign_path s rs) (** [add_rule sign s r] adds the new rule [r] to the symbol [s]. When the rule does not correspond to a symbol of signature [sign], it is stored in its dependencies. /!\ does not update the decision tree or the critical pairs. *) let add_rule : t -> sym_rule -> unit = fun sign (s,r) -> add_rules sign s [r] (** [add_notation sign sym nota] changes the notation of [s] to [n] in the signature [sign]. *) let add_notation : t -> sym -> float notation -> unit = fun sign s n -> s.sym_nota := n; if s.sym_path <> sign.sign_path then let d = try Path.Map.find s.sym_path !(sign.sign_deps) with Not_found -> assert false in let f = function | None -> Some{rules=[]; nota=Some n} | Some sd -> Some{sd with nota=Some n} in let sm = StrMap.update s.sym_name f d.dep_symbols in let d = {d with dep_symbols=sm} in sign.sign_deps := Path.Map.add s.sym_path d !(sign.sign_deps) (** [add_builtin sign name sym] binds the builtin [name] to [sym] in the signature [sign]. The previous binding, if any, is discarded. *) let add_builtin : t -> string -> sym -> unit = fun sign name sym -> sign.sign_builtins := StrMap.add name sym !(sign.sign_builtins) (** [add_inductive sign ind_sym ind_cons ind_prop ind_prop_args] add to [sign] the inductive type [ind_sym] with constructors [ind_cons], induction principle [ind_prop] with [ind_prop_args] arguments. *) let add_inductive : t -> sym -> sym list -> sym -> int -> int -> unit = fun sign ind_sym ind_cons ind_prop ind_nb_params ind_nb_types -> let ind_nb_cons = List.length ind_cons in let ind = {ind_cons; ind_prop; ind_nb_params; ind_nb_types; ind_nb_cons} in sign.sign_ind := SymMap.add ind_sym ind !(sign.sign_ind) (** [iterate f sign] applies [f] on [sign] and its dependencies recursively. *) let iterate : (t -> unit) -> t -> unit = fun f sign -> let visited = Stdlib.ref Path.Set.empty in let rec handle sign = Stdlib.(visited := Path.Set.add sign.sign_path !visited); f sign; let dep path _ = if not (Path.Set.mem path Stdlib.(!visited)) then handle (Path.Map.find path !loaded) in Path.Map.iter dep !(sign.sign_deps) in handle sign (** [dependencies sign] returns an association list containing (the transitive closure of) the dependencies of the signature [sign]. Note that the order of the list gives one possible loading order for the signatures. Note also that [sign] itself appears at the end of the list. *) let rec dependencies : t -> (Path.t * t) list = fun sign -> (* Recursively compute dependencies for the immediate dependencies. *) let f p _ l = dependencies (Path.Map.find p !loaded) :: l in let deps = Path.Map.fold f !(sign.sign_deps) [[(sign.sign_path, sign)]] in (* Minimize and put everything together. *) let rec minimize acc deps = let not_here (p,_) = let has_p = List.exists (fun (q,_) -> p = q) in not (List.exists has_p acc) in match deps with | [] -> List.rev acc | d::deps -> minimize ((List.filter not_here d) :: acc) deps in List.concat (minimize [] deps)
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>