From 28a34f592996bec9dc524f1741764878c9c85e1c Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Tue, 24 Dec 2019 18:10:57 +0000 Subject: [PATCH] Refactor evaluation. Also more work on pattern matching and when to rebuild. --- src/ast.ml | 97 +++++++++++++++++++++++++------ src/ast.mli | 24 +++++++- src/eval.ml | 187 ++++++++++++++++++++++-------------------------------------- 3 files changed, 170 insertions(+), 138 deletions(-) diff --git a/src/ast.ml b/src/ast.ml index 48ff6aa..f1a4137 100644 --- a/src/ast.ml +++ b/src/ast.ml @@ -20,6 +20,8 @@ open Lexing open Printf +open Utils + module Env = Map.Make (String) type loc = position * position @@ -53,6 +55,62 @@ and subst = | SString of string | SVar of id +let getvar env loc name = + try Env.find name env + with Not_found -> + failwithf "%a: variable ‘%s’ not found" string_loc loc name + +let getgoal env loc name = + let expr = + try Env.find name env + with Not_found -> + failwithf "%a: goal ‘%s’ not found" string_loc loc name in + let goal = + match expr with + | EGoal (loc, goal) -> goal + | _ -> + failwithf "%a: tried to call ‘%s’ which is not a goal" + string_loc loc name in + goal + +let rec to_constant env = function + | EConstant (loc, c) -> c + + | EVar (loc, name) -> + let expr = getvar env loc name in + to_constant env expr + + | ESubsts (loc, str) -> + CString (substitute env loc str) + + | EList (loc, _) -> + failwithf "%a: list found where constant expression expected" + string_loc loc + + | ECall (loc, name, _) -> + failwithf "%a: cannot use goal ‘%s’ in constant expression" + string_loc loc name + + | ETactic (loc, name, _) -> + failwithf "%a: cannot use tactic ‘*%s’ in constant expression" + string_loc loc name + + | EGoal (loc, _) -> + failwithf "%a: cannot use goal in constant expression" + string_loc loc + +and substitute env loc substs = + let b = Buffer.create 13 in + List.iter ( + function + | SString s -> Buffer.add_string b s + | SVar name -> + let expr = getvar env loc name in + match to_constant env expr with + | CString s -> Buffer.add_string b s + ) substs; + Buffer.contents b + module Substs = struct type t = { mutable elems : subst list; (* built in reverse order *) @@ -116,26 +174,31 @@ and string_pattern () = function and print_pattern fp p = output_string fp (string_pattern () p) -and print_expr fp = function - | EGoal _ -> assert false (* printed above *) +and string_expr () = function + | EGoal (loc, (params, patterns, exprs, code)) -> + sprintf "goal (%s) = %s : %s%s" + (String.concat ", " params) + (String.concat ", " (List.map (string_pattern ()) patterns)) + (String.concat ", " (List.map (string_expr ()) exprs)) + (match code with None -> "" | Some code -> " = { ... }") | ECall (loc, name, params) -> - fprintf fp "%s (" name; - iter_with_commas fp print_expr params; - fprintf fp ")" + sprintf "%s (%s)" + name (String.concat ", " (List.map (string_expr ()) params)) | ETactic (loc, name, params) -> - fprintf fp "*%s (" name; - iter_with_commas fp print_expr params; - fprintf fp ")" - | EVar (loc, var) -> print_id fp var + sprintf "*%s (%s)" + name (String.concat ", " (List.map (string_expr ()) params)) + | EVar (loc, var) -> var | EList (loc, xs) -> - fprintf fp "["; - iter_with_commas fp print_expr xs; - fprintf fp "]" - | ESubsts (loc, s) -> print_substs fp s - | EConstant (loc, c) -> print_constant fp c - -and print_constant fp = function - | CString s -> fprintf fp "%S" s + sprintf "[%s]" (String.concat ", " (List.map (string_expr ()) xs)) + | ESubsts (loc, s) -> string_substs () s + | EConstant (loc, c) -> string_constant () c + +and print_expr fp expr = output_string fp (string_expr () expr) + +and string_constant () = function + | CString s -> sprintf "%S" s + +and print_constant fp c = output_string fp (string_constant () c) and print_id = output_string diff --git a/src/ast.mli b/src/ast.mli index 1470309..c59de00 100644 --- a/src/ast.mli +++ b/src/ast.mli @@ -40,7 +40,8 @@ type env = expr Env.t and pattern = (** match tactic such as *file ("filename") *) | PTactic of loc * id * substs list - (** match named variable, which must be a string or list *) + (** match named variable, which must be a string, ETactic or + a list of those *) | PVar of loc * id and expr = (** goal (params) = patterns : exprs = code *) @@ -69,6 +70,24 @@ and subst = (** %-substitution. *) | SVar of id +(** Look up a variable in the environment. Raise [Failure _] + if not found. *) +val getvar : env -> loc -> id -> expr + +(** Look up a goal in the environment. Raise [Failure _] if not + found or if the named variable is not a goal. *) +val getgoal : env -> loc -> id -> goal + +(** Take any expression and simplify it down to a constant. + If the expression cannot be simplified then this raises + [Failure _]. *) +val to_constant : env -> expr -> constant + +(** Take a substitution list and try to turn it into a simple + string by evaluating every variable. If not possible this + raises [Failure _]. *) +val substitute : env -> loc -> substs -> string + (** This is used for incrementally building Ast.substs in the parser. *) module Substs : sig type t @@ -79,6 +98,9 @@ module Substs : sig val add_var : t -> string -> unit end +(** Print all definitions in an environment. *) val print_env : out_channel -> env -> unit +(** %a formatters. *) val string_pattern : unit -> pattern -> string +val string_expr : unit -> expr -> string diff --git a/src/eval.ml b/src/eval.ml index b8dbe54..9e3bf3d 100644 --- a/src/eval.ml +++ b/src/eval.ml @@ -27,31 +27,19 @@ and evaluate_target env = function (* Call a goal. *) | Ast.ECall (loc, name, args) -> - let expr = - try Ast.Env.find name env - with Not_found -> - failwithf "%a: goal ‘%s’ not found" Ast.string_loc loc name in - let goal = - match expr with - | Ast.EGoal (loc, goal) -> goal - | _ -> - failwithf "%a: tried to call ‘%s’ which is not a goal" - Ast.string_loc loc name in - run_goal loc env name args goal + let goal = Ast.getgoal env loc name in + run_goal env loc name args goal | Ast.ETactic (loc, name, args) -> (* All parameters of tactics must be simple expressions (strings, * in future booleans, numbers, etc). *) - let args = List.map (simplify env) args in - run_goal_for_tactic loc env name args + let args = List.map (Ast.to_constant env) args in + run_tactic env loc name args (* Look up the variable and substitute it. *) | Ast.EVar (loc, name) -> - let expr = - try Ast.Env.find name env - with Not_found -> - failwithf "%a: variable ‘%s’ not found" Ast.string_loc loc name in + let expr = Ast.getvar env loc name in evaluate_target env expr (* Lists are inlined when found as a target. *) @@ -60,16 +48,62 @@ and evaluate_target env = function (* A string (with or without substitutions) implies *file(filename). *) | Ast.ESubsts (loc, str) -> - let str = substitute loc env str in - run_goal_for_tactic loc env "file" [Ast.CString str] + let str = Ast.substitute env loc str in + run_tactic env loc "file" [Ast.CString str] | Ast.EConstant (loc, c) -> - run_goal_for_tactic loc env "file" [c] + run_tactic env loc "file" [c] + +(* Run a goal by name. *) +and run_goal env loc name args (params, patterns, deps, code) = + (* Create a new environment which maps the parameter names to + * the args. + *) + let env = + let params = + try List.combine params args + with Invalid_argument _ -> + failwithf "%a: calling goal ‘%s’ with wrong number of arguments" + Ast.string_loc loc name in + List.fold_left (fun env (k, v) -> Ast.Env.add k v env) env params in + + (* Check all dependencies have been updated. *) + evaluate_targets env deps; + + (* Check if any target (ie. pattern) needs to be rebuilt. *) + let rebuild = + List.exists (needs_rebuild env loc name deps) patterns in + + if rebuild then ( + (* Run the code (if any). *) + (match code with + | None -> () + | Some code -> + let code = Ast.substitute env loc code in + Printf.printf "running : %s\n" code + ); + + (* Check all targets were updated (else it's an error). *) + let pattern_still_needs_rebuild = + try Some (List.find (needs_rebuild env loc name deps) patterns) + with Not_found -> None in + match pattern_still_needs_rebuild with + | None -> () + | Some pattern -> + failwithf "%a: goal ‘%s’ ran successfully but it did not rebuild %a" + Ast.string_loc loc + name + Ast.string_pattern pattern + ) + +(* Return whether the target (pattern) needs to be rebuilt. *) +and needs_rebuild env loc name deps pattern = + false (* XXX *) (* Find the goal which matches the given tactic and run it. - * Params is a list of constants. + * const_args is a list of parameters (all constants). *) -and run_goal_for_tactic loc env tactic const_args = +and run_tactic env loc tactic const_args = (* Search across all goals for a matching tactic. *) let goals = let env = Ast.Env.bindings env in @@ -83,21 +117,22 @@ and run_goal_for_tactic loc env tactic const_args = try List.find (fun (_, (_, patterns, _, _)) -> - List.exists (matching_pattern loc env tactic const_args) patterns) + List.exists (matching_pattern env loc tactic const_args) patterns) goals with Not_found -> - failwithf "%a: don't know how to build %s %s" - Ast.string_loc loc - tactic - (String.concat ", " - (List.map (function Ast.CString s -> s) const_args)) in + let tactic = + Ast.ETactic (loc, tactic, + List.map (fun c -> Ast.EConstant (loc, c)) + const_args) in + failwithf "%a: don't know how to build %a" + Ast.string_loc loc Ast.string_expr tactic in let args = [] (* XXX calculate free variables *) in - run_goal loc env name args goal + run_goal env loc name args goal (* XXX This only does exact matches at the moment. *) -and matching_pattern loc env tactic const_args = function +and matching_pattern env loc tactic const_args = function | Ast.PTactic (loc, constructor, params) when tactic = constructor && List.length const_args = List.length params -> @@ -105,7 +140,7 @@ and matching_pattern loc env tactic const_args = function * to constants, but don't fail here if we can't do this. *) (try - let params = List.map (substitute loc env) params in + let params = List.map (Ast.substitute env loc) params in let params = List.map (fun s -> Ast.CString s) params in const_args = params with Failure _ -> false @@ -113,93 +148,5 @@ and matching_pattern loc env tactic const_args = function | Ast.PTactic _ -> false - | Ast.PVar (loc, name) -> assert false -(* - NOT IMPLEMENTED - we need variables to contain constructors! - (try - let expr = Ast.StringMap.find name env in - let expr = simplify env expr in - with Not_found -> false - ) -*) - -(* Run a named goal. *) -and run_goal loc env name args (params, patterns, deps, code) = - (* Substitute the args for the parameters in the environment. *) - let params = - try List.combine params args - with Invalid_argument _ -> - failwithf "%a: calling goal ‘%s’ with wrong number of arguments" - Ast.string_loc loc name in - let env = - List.fold_left (fun env (k, v) -> Ast.Env.add k v env) - env params in - - (* Evaluate the dependencies first. *) - evaluate_targets env deps; - - (* Check if any target needs to be updated. *) - (* XXX *) - - (* Run the code (if any). *) - (match code with - | None -> () - | Some code -> - let code = substitute loc env code in - Printf.printf "running : %s\n" code - ); - - (* Check all targets were updated (else it's an error). *) - (* XXX *) + | Ast.PVar (loc, name) -> assert false (* not implemented *) -(* Take any expression and simplify it down to a constant. - * If the expression cannot be simplified then this throws - * an error. - *) -and simplify env = function - | Ast.EConstant (loc, c) -> c - - | Ast.EVar (loc, name) -> - let expr = - try Ast.Env.find name env - with Not_found -> - failwithf "%a: variable ‘%s’ not found" Ast.string_loc loc name in - simplify env expr - - | Ast.ESubsts (loc, str) -> - Ast.CString (substitute loc env str) - - | Ast.EList (loc, _) -> - failwithf "%a: list found where constant expression expected" - Ast.string_loc loc - - | Ast.ECall (loc, name, _) -> - failwithf "%a: cannot use goal ‘%s’ in constant expression" - Ast.string_loc loc name - - | Ast.ETactic (loc, name, _) -> - failwithf "%a: cannot use tactic ‘*%s’ in constant expression" - Ast.string_loc loc name - - | Ast.EGoal (loc, _) -> - failwithf "%a: cannot use goal in constant expression" - Ast.string_loc loc - -(* Take a substitution list and try to turn it into a simple - * string by evaluating every variable. If not possible this - * throws an error. Returns a string. - *) -and substitute loc env substs = - let b = Buffer.create 13 in - List.iter ( - function - | Ast.SString s -> Buffer.add_string b s - | Ast.SVar name -> - let expr = - try Ast.Env.find name env - with Not_found -> - failwithf "%a: variable ‘%s’ not found" Ast.string_loc loc name in - match simplify env expr with - | Ast.CString s -> Buffer.add_string b s - ) substs; - Buffer.contents b -- 1.8.3.1