Implement functions.
[goals.git] / src / ast.mli
1 (* Goalfile Abstract Syntax Tree
2  * Copyright (C) 2019 Richard W.M. Jones
3  * Copyright (C) 2019 Red Hat Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *)
19
20 module Env : sig
21   type key = string
22   type 'a t
23   val empty: 'a t
24   val add: key -> 'a -> 'a t -> 'a t
25   val find: key -> 'a t -> 'a
26   val fold: (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
27   val filter: (key -> 'a -> bool) -> 'a t -> 'a t
28   val bindings: 'a t -> (key * 'a) list
29
30   (* This is not the normal Map.merge function. *)
31   val merge : 'a t -> 'a t -> 'a t
32 end
33
34 (** Location where we parsed from $loc = $startpos, $endpos *)
35 type loc = Lexing.position * Lexing.position
36 val noloc : loc
37 val print_loc : out_channel -> loc -> unit
38 val string_loc : unit -> loc -> string
39
40 (** An environment is a set of variable and goal definitions, mapping
41     variable or goal name -> expression. *)
42 type env = expr Env.t
43 and pattern =
44   (** match tactic such as *file ("filename") *)
45   | PTactic of loc * id * substs list
46 and expr =
47   (** goal (params) = patterns : exprs code *)
48   | EGoalDefn of loc * goal
49   (** function (params) = code *)
50   | EFuncDefn of loc * func
51   (** tactic (params) = code *)
52   | ETacticDefn of loc * tactic
53   (** call goal (params) or function (params) *)
54   | ECall of loc * id * expr list
55   (** call *tactic (params) etc. *)
56   | ETacticCtor of loc * id * expr list
57   (** variable, or goal call with no parameters *)
58   | EVar of loc * id
59   (** list *)
60   | EList of loc * expr list
61   (** string with %-substitutions *)
62   | ESubsts of loc * substs
63   (** constant expression, such as a plain string, int, boolean, etc. *)
64   | EConstant of loc * constant
65 and constant =
66   | CString of string
67 and goal = param_decl list * pattern list * expr list * code option
68 and func = param_decl list * code
69 and tactic = param_decl list * code
70   (** Goal/func/tactic parameter. *)
71 and param_decl = id
72 and id = string
73 and code = substs
74 and substs = subst list
75 and subst =
76   (** String literal part. *)
77   | SString of string
78   (** %-substitution. *)
79   | SVar of id
80
81 (** Look up a variable in the environment.  Raise [Failure _]
82     if not found. *)
83 val getvar : env -> loc -> id -> expr
84
85 (** Look up a goal in the environment.  Raise [Failure _] if not
86     found or if the named variable is not a goal. *)
87 val getgoal : env -> loc -> id -> goal
88
89 (** Look up a function in the environment.  Raise [Failure _] if not
90     found or if the named variable is not a function. *)
91 val getfunc : env -> loc -> id -> func
92
93 (** Look up a tactic in the environment.  Raise [Failure _] if not
94     found or if the named variable is not a tactic. *)
95 val gettactic : env -> loc -> id -> tactic
96
97 (** This is used for incrementally building Ast.substs in the parser. *)
98 module Substs : sig
99   type t
100   val create : unit -> t
101   val get : t -> substs
102   val add_char : t -> char -> unit
103   val add_string : t -> string -> unit
104   val add_var : t -> string -> unit
105 end
106
107 (** %a formatters. *)
108 val print_env : out_channel -> env -> unit
109 val string_pattern : unit -> pattern -> string
110 val string_expr : unit -> expr -> string
111 val print_expr : out_channel -> expr -> unit