Implement functions.
[goals.git] / src / ast.mli
index e2189cf..f8b88f0 100644 (file)
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  *)
 
-type file = stmt list
-and stmt =
-  (** let id = expr *)
-  | Let of id * expr
-  (** goal id (params) = patterns : exprs = code *)
-  | Goal of id * id list * pattern list * expr list * code option
+module Env : sig
+  type key = string
+  type 'a t
+  val empty: 'a t
+  val add: key -> 'a -> 'a t -> 'a t
+  val find: key -> 'a t -> 'a
+  val fold: (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
+  val filter: (key -> 'a -> bool) -> 'a t -> 'a t
+  val bindings: 'a t -> (key * 'a) list
+
+  (* This is not the normal Map.merge function. *)
+  val merge : 'a t -> 'a t -> 'a t
+end
+
+(** Location where we parsed from $loc = $startpos, $endpos *)
+type loc = Lexing.position * Lexing.position
+val noloc : loc
+val print_loc : out_channel -> loc -> unit
+val string_loc : unit -> loc -> string
+
+(** An environment is a set of variable and goal definitions, mapping
+    variable or goal name -> expression. *)
+type env = expr Env.t
 and pattern =
-  (** match tactic such as file ("filename") *)
-  | PTactic of id * substs list
-  (** match named variable, which must be a string or list *)
-  | PVarSubst of id
+  (** match tactic such as *file ("filename") *)
+  | PTactic of loc * id * substs list
 and expr =
-  (** goalname (params), tactic (params) etc. *)
-  | ECall of id * expr list
-  (** variable *)
-  | EVar of id
-  (** string with %-substitutions *)
-  | EString of substs
+  (** goal (params) = patterns : exprs code *)
+  | EGoalDefn of loc * goal
+  (** function (params) = code *)
+  | EFuncDefn of loc * func
+  (** tactic (params) = code *)
+  | ETacticDefn of loc * tactic
+  (** call goal (params) or function (params) *)
+  | ECall of loc * id * expr list
+  (** call *tactic (params) etc. *)
+  | ETacticCtor of loc * id * expr list
+  (** variable, or goal call with no parameters *)
+  | EVar of loc * id
   (** list *)
-  | EList of expr list
+  | EList of loc * expr list
+  (** string with %-substitutions *)
+  | ESubsts of loc * substs
+  (** constant expression, such as a plain string, int, boolean, etc. *)
+  | EConstant of loc * constant
+and constant =
+  | CString of string
+and goal = param_decl list * pattern list * expr list * code option
+and func = param_decl list * code
+and tactic = param_decl list * code
+  (** Goal/func/tactic parameter. *)
+and param_decl = id
 and id = string
 and code = substs
 and substs = subst list
@@ -46,6 +78,22 @@ 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
+
+(** Look up a function in the environment.  Raise [Failure _] if not
+    found or if the named variable is not a function. *)
+val getfunc : env -> loc -> id -> func
+
+(** Look up a tactic in the environment.  Raise [Failure _] if not
+    found or if the named variable is not a tactic. *)
+val gettactic : env -> loc -> id -> tactic
+
 (** This is used for incrementally building Ast.substs in the parser. *)
 module Substs : sig
   type t
@@ -56,4 +104,8 @@ module Substs : sig
   val add_var : t -> string -> unit
 end
 
-val print_file : out_channel -> file -> unit
+(** %a formatters. *)
+val print_env : out_channel -> env -> unit
+val string_pattern : unit -> pattern -> string
+val string_expr : unit -> expr -> string
+val print_expr : out_channel -> expr -> unit