package dedukti
An implementation of The Lambda-Pi Modulo Theory
Install
Dune Dependency
Authors
Maintainers
Sources
v2.7.tar.gz
sha512=97171b48dd96043d84587581d72edb442f63e7b5ac1695771aa1c3c9074739e15bc7d17678fedb7062acbf403a0bf323d97485c31b92376b80c63b5c2300ee3c
sha256=5e1b6a859dfa1eb2098947a99c7d11ee450f750d96da1720f4834e1505d1096c
doc/src/dedukti.kernel/rule.ml.html
Source file rule.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
open Basic open Format open Term type pattern = | Var of loc * ident * int * pattern list (* Y x1 ... xn *) | Pattern of loc * name * pattern list | Lambda of loc * ident * pattern | Brackets of term type wf_pattern = | LJoker | LVar of ident * int * int list | LLambda of ident * wf_pattern | LPattern of name * wf_pattern array | LBoundVar of ident * int * wf_pattern array | LACSet of name * wf_pattern list type rule_name = Beta | Delta of name | Gamma of bool * name let rule_name_eq : rule_name -> rule_name -> bool = fun n1 n2 -> match (n1, n2) with | Delta x, Delta y -> name_eq x y | Gamma (b1, x), Gamma (b2, y) -> b1 = b2 && name_eq x y | _, _ -> false type 'a rule = {name : rule_name; ctx : 'a context; pat : pattern; rhs : term} type partially_typed_rule = term option rule type typed_rule = term rule type arity_rule = int rule type constr = int * term let pp_constr fmt (i, t) = fprintf fmt "%i =b %a" i pp_term t type rule_infos = { l : loc; name : rule_name; nonlinear : int list; cst : name; args : pattern list; rhs : term; ctx_size : int; esize : int; pats : wf_pattern array; arity : int array; constraints : constr list; } let infer_rule_context ri = let res = Array.make ri.ctx_size (dloc, mk_ident "_", -1) in let rec aux k = function | LJoker -> () | LVar (name, n, _) -> if n >= k then res.(n - k) <- (dloc, name, ri.arity.(n - k)) | LLambda (_, body) -> aux (k + 1) body | LPattern (_, args) -> Array.iter (aux k) args | LBoundVar (_, _, args) -> Array.iter (aux k) args | LACSet (_, args) -> List.iter (aux k) args in Array.iter (aux 0) ri.pats; Array.to_list res let infer_rule_context_without_arity ri = ri |> infer_rule_context |> List.map (fun (loc, id, _) -> (loc, id, None)) let pattern_of_rule_infos r = Pattern (r.l, r.cst, r.args) type rule_error = | BoundVariableExpected of loc * pattern | DistinctBoundVariablesExpected of loc * ident | VariableBoundOutsideTheGuard of loc * term | UnboundVariable of loc * ident * pattern (* FIXME : this exception seems never to be raised *) | AVariableIsNotAPattern of loc * ident | NonLinearNonEqArguments of loc * ident (* FIXME: the reason for this exception should be formalized on paper ! *) | NotEnoughArguments of loc * ident * int * int * int | NonLinearRule of loc * rule_name exception Rule_error of rule_error let rec pp_pattern out pattern = match pattern with | Var (_, x, n, []) -> fprintf out "%a[%i]" pp_ident x n | Var (_, x, n, lst) -> fprintf out "%a[%i] %a" pp_ident x n (pp_list " " pp_pattern_wp) lst | Pattern (_, n, []) -> fprintf out "%a" pp_name n | Pattern (_, n, pats) -> fprintf out "%a %a" pp_name n (pp_list " " pp_pattern_wp) pats | Lambda (_, x, p) -> fprintf out "%a => %a" pp_ident x pp_pattern p | Brackets t -> fprintf out "{ %a }" pp_term t and pp_pattern_wp out pattern = match pattern with | (Var (_, _, _, _ :: _) | Pattern _ | Lambda _) as p -> fprintf out "(%a)" pp_pattern p | p -> pp_pattern out p let rec pp_wf_pattern fmt wf_pattern = match wf_pattern with | LJoker -> fprintf fmt "_" | LVar (x, n, []) -> fprintf fmt "%a[%i]" pp_ident x n | LVar (x, n, lst) -> fprintf fmt "%a[%i] %a" pp_ident x n (pp_list " " pp_print_int) lst | LPattern (n, pats) when Array.length pats = 0 -> fprintf fmt "%a" pp_name n | LPattern (n, pats) -> fprintf fmt "%a %a" pp_name n (pp_list " " pp_wf_pattern_wp) (Array.to_list pats) | LLambda (x, p) -> fprintf fmt "%a => %a" pp_ident x pp_wf_pattern p | LBoundVar (x, n, pats) when Array.length pats = 0 -> fprintf fmt "%a[%i]" pp_ident x n | LBoundVar (x, n, pats) -> fprintf fmt "%a[%i] %a" pp_ident x n (pp_list " " pp_wf_pattern_wp) (Array.to_list pats) | LACSet (cst, l) -> fprintf fmt "%a{%a}" pp_name cst (pp_list "; " pp_wf_pattern_wp) l and pp_wf_pattern_wp fmt wf_pattern = match wf_pattern with | (LVar (_, _, _ :: _) | LPattern _ | LLambda _) as p -> fprintf fmt "(%a)" pp_wf_pattern p | _ -> pp_wf_pattern fmt wf_pattern let get_loc_pat = function | Var (l, _, _, _) | Pattern (l, _, _) | Lambda (l, _, _) -> l | Brackets t -> get_loc t let get_loc_rule r = get_loc_pat r.pat let pp_rule_name fmt = function | Beta -> fprintf fmt "Beta" | Delta n -> fprintf fmt "Delta: %a" pp_name n | Gamma (true, n) -> fprintf fmt "Gamma: %a" pp_name n | Gamma (false, n) -> fprintf fmt "Gamma (default): %a" pp_name n let pp_rule pp_ctxt fmt (rule : 'a rule) = fprintf fmt " {%a} [%a] %a --> %a" pp_rule_name rule.name pp_ctxt rule.ctx pp_pattern rule.pat pp_term rule.rhs let pp_untyped_rule fmt = pp_rule pp_untyped_context fmt let pp_typed_rule = pp_rule pp_typed_context let pp_part_typed_rule = pp_rule pp_part_typed_context (* FIXME: do not print all the informations because it is used in utils/errors *) let pp_rule_infos out r = pp_untyped_rule out { name = r.name; ctx = infer_rule_context r; pat = pattern_of_rule_infos r; rhs = r.rhs; } let pattern_to_term p = let rec aux k = function | Brackets t -> t | Pattern (l, n, args) -> mk_App2 (mk_Const l n) (List.map (aux k) args) | Var (l, x, n, args) -> mk_App2 (mk_DB l x n) (List.map (aux k) args) | Lambda (l, x, pat) -> mk_Lam l x None (aux (k + 1) pat) in aux 0 p type pattern_info = { constraints : constr list; context_size : int; arity : int array; nonlinear : int list; } (* ************************************************************************** *) (* ************************************************************************** *) let bracket_ident = mk_ident "{_}" (* FIXME: can this be replaced by dmark? *) let rec all_distinct = function | [] -> true | hd :: tl -> if List.mem hd tl then false else all_distinct tl module IntHashtbl = Hashtbl.Make (struct type t = int let equal i j = i = j let hash i = i land max_int end) (* TODO : cut this function in smaller ones *) (** [check_patterns size pats] checks that the given pattern is a well formed Miller pattern in a context of size [size] and linearizes it. More precisely: - Context variables are exclusively applied to distinct locally bound variables - Occurences of each context variable are all applied to the same number of arguments Returns the representation of the corresponding linear well formed pattern together with extracted pattern information: - Convertibility constraints from non-linearity and brackets - Size of generated context - Arity infered for all context variables *) let check_patterns (esize : int) (pats : pattern list) : wf_pattern list * pattern_info = let nonlinear = ref [] in let constraints = ref [] in let context_size = ref esize in let arity = IntHashtbl.create 10 in let extract_db k = function | Var (_, _, n, []) when n < k -> n | p -> raise (Rule_error (BoundVariableExpected (get_loc_pat p, p))) in let rec aux (k : int) (pat : pattern) : wf_pattern = match pat with | Lambda (_, x, p) -> LLambda (x, aux (k + 1) p) | Var (_, x, n, args) when n < k -> LBoundVar (x, n, Array.of_list (List.map (aux k) args)) | Var (l, x, n, args) (* Context variable (n>=k) *) -> (* Miller variables should only be applied to locally bound variables *) let args' = List.map (extract_db k) args in (* Miller variables should be applied to distinct variables *) if not (all_distinct args') then raise (Rule_error (DistinctBoundVariablesExpected (l, x))); let nb_args' = List.length args' in if IntHashtbl.mem arity (n - k) then if nb_args' <> IntHashtbl.find arity (n - k) then raise (Rule_error (NonLinearNonEqArguments (l, x))) else nonlinear := (n - k) :: !nonlinear else IntHashtbl.add arity (n - k) nb_args'; LVar (x, n, args') | Brackets t -> let unshifted = try Subst.unshift k t with Subst.UnshiftExn -> raise (Rule_error (VariableBoundOutsideTheGuard (get_loc t, t))) (* Note: A different exception is previously raised at rule type-checking for this. *) in IntHashtbl.add arity !context_size 0; (* Brackets are variable with arity 0 *) incr context_size; let nvar = !context_size - 1 in (* DB indice for a fresh context variable *) constraints := (nvar, unshifted) :: !constraints; LVar (bracket_ident, nvar + k, []) | Pattern (_, n, args) -> LPattern (n, Array.of_list (List.map (aux k) args)) in let wf_pats = List.map (aux 0) pats in ( wf_pats, { context_size = !context_size; constraints = !constraints; arity = Array.init !context_size (fun i -> IntHashtbl.find arity i); nonlinear = !nonlinear; } ) let to_rule_infos (r : 'a rule) : rule_infos = let ctx_size = List.length r.ctx in let l, cst, args = match r.pat with | Pattern (l, cst, args) -> (l, cst, args) | Var (l, x, _, _) -> raise (Rule_error (AVariableIsNotAPattern (l, x))) | Lambda _ | Brackets _ -> assert false (* already raised at the parsing level *) in let pats2, infos = check_patterns ctx_size args in { l; name = r.name; nonlinear = infos.nonlinear; cst; args; rhs = r.rhs; ctx_size; esize = infos.context_size; pats = Array.of_list pats2; arity = infos.arity; constraints = infos.constraints; } let untyped_rule_of_rule_infos ri : partially_typed_rule = { name = ri.name; ctx = infer_rule_context_without_arity ri; pat = pattern_of_rule_infos ri; rhs = ri.rhs; } (* Rule checking *) (* Checks that all Miller variables are applied to at least as many arguments on the rhs as they are on the lhs (their arity). *) let check_arity (r : rule_infos) : unit = let check _ id n k nargs = let expected_args = r.arity.(n - k) in if nargs < expected_args then raise @@ Rule_error (NotEnoughArguments (r.l, id, n, nargs, expected_args)) in let rec aux k = function | Kind | Type _ | Const _ -> () | DB (l, id, n) -> if n >= k then check l id n k 0 | App (DB (l, id, n), a1, args) when n >= k -> check l id n k (List.length args + 1); List.iter (aux k) (a1 :: args) | App (f, a1, args) -> List.iter (aux k) (f :: a1 :: args) | Lam (_, _, None, b) -> aux (k + 1) b | Lam (_, _, Some a, b) | Pi (_, _, a, b) -> aux k a; aux (k + 1) b in aux 0 r.rhs (** Checks that all rule are left-linear. *) let check_linearity (r : rule_infos) : unit = if r.nonlinear <> [] then raise (Rule_error (NonLinearRule (r.l, r.name)))
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>