-(* Find the goal which matches the given tactic and run it.
- * cargs is a list of parameters (all constants).
- *)
-and run_tactic env loc tactic cargs =
- Cmdline.debug "%a: running tactic %s" Ast.string_loc loc tactic;
-
- (* Find all goals in the environment. Returns a list of (name, goal). *)
- let goals =
- let env = Ast.Env.bindings env in
- filter_map
- (function
- | name, Ast.EGoal (loc, goal) -> Some (name, goal)
- | _ -> None) env in
-
- (* Find all patterns. Returns a list of (pattern, name, goal). *)
- let patterns : (Ast.pattern * Ast.id * Ast.goal) list =
- List.flatten
- (List.map (fun (name, ((_, patterns, _, _) as goal)) ->
- List.map (fun pattern -> (pattern, name, goal)) patterns) goals) in
-
- (* Find any patterns (ie. tactics) which match the one we
- * are searching for. This returns a binding for the goal args,
- * so we end up with a list of (pattern, name, goal, args).
- *)
- let patterns : (Ast.pattern * Ast.id * Ast.goal * Ast.expr list) list =
- filter_map (
- fun (pattern, name, ((params, _, _, _) as goal)) ->
- match matching_pattern env loc tactic cargs pattern params with
- | None -> None
- | Some args -> Some (pattern, name, goal, args)
- ) patterns in
-
- match patterns with
- | [_, name, goal, args] ->
- run_goal env loc name args goal
-
- | [] ->
- (* There's no matching goal, but we don't need one if
- * the tactic doesn't need to be rebuilt.
- *)
- let targs = List.map (function Ast.CString s -> [Ast.SString s]) cargs in
- let p = Ast.PTactic (loc, tactic, targs) in
- if needs_rebuild env loc [] p then (
- let t = Ast.ECallTactic (loc, tactic,
- List.map (fun c -> Ast.EConstant (loc, c))
- cargs) in
- failwithf "%a: don't know how to build %a"
- Ast.string_loc loc Ast.string_expr t
+ (* Tactics expand to the first parameter. *)
+ | ETacticCtor (loc, _, []) -> Filename.quote ""
+ | ETacticCtor (loc, _, (arg :: _)) -> expr_to_shell_string env arg
+
+ | EGoalDefn (loc, _) ->
+ failwithf "%a: cannot use goal in shell expansion"
+ Ast.string_loc loc
+
+ | EFuncDefn (loc, _) ->
+ failwithf "%a: cannot use function in shell expansion"
+ Ast.string_loc loc
+
+ | ETacticDefn (loc, _) ->
+ failwithf "%a: cannot use tactic in shell expansion"
+ Ast.string_loc loc
+
+and run_code ?(quiet = false) env loc code =
+ let code = to_shell_script env loc code in
+ let code =
+ "source " ^ Filename.quote Cmdline.prelude_sh_file ^ "\n" ^
+ "set -e\n" ^
+ (if not quiet then "set -x\n" else "") ^
+ "\n" ^
+ code in
+
+ let chan = Unix.open_process_in code in
+ let b = Buffer.create 1024 in
+ (try
+ while true do
+ Buffer.add_string b (input_line chan);
+ Buffer.add_char b '\n'
+ done
+ with End_of_file -> ());
+ let st = Unix.close_process_in chan in
+ let i =
+ match st with
+ | Unix.WEXITED i -> i
+ | Unix.WSIGNALED i ->
+ failwithf "%a: killed by signal %d" Ast.string_loc loc i
+ | Unix.WSTOPPED i ->
+ failwithf "%a: stopped by signal %d" Ast.string_loc loc i in
+ i, Buffer.contents b
+
+and evaluate_goal_arg env = function
+ | Ast.EVar (loc, name) ->
+ let expr = Ast.getvar env loc name in
+ evaluate_goal_arg env expr
+
+ | ESubsts (loc, str) ->
+ let str = substitute env loc str in
+ Ast.EConstant (loc, Ast.CString str)
+
+ | EList (loc, exprs) ->
+ Ast.EList (loc, List.map (evaluate_goal_arg env) exprs)
+
+ | ETacticCtor (loc, name, exprs) ->
+ Ast.ETacticCtor (loc, name, List.map (evaluate_goal_arg env) exprs)
+
+ | ECall (loc, name, args) ->
+ let expr = Ast.getvar env loc name in
+ (match expr with
+ | EGoalDefn _ ->
+ (* Goals don't return anything so they cannot be used in
+ * goal args. Use a function instead.
+ *)
+ failwithf "%a: cannot use goal call ‘%s’ in goal argument"
+ Ast.string_loc loc name
+
+ | EFuncDefn (loc, func) ->
+ call_function env loc name args func
+
+ | _ ->
+ failwithf "%a: cannot call ‘%s’ which is not a function"
+ Ast.string_loc loc name