package lambdapi

All checked builds pass
  1. Overview
  2. Docs
Proof assistant for the λΠ-calculus modulo rewriting

Install

dune-project
 Dependency

Authors

Maintainers

Sources

lambdapi-4.0.0.tbz
sha256=0e2e73e0ea0c674f1263bf85ed3453ed3d6084d38eea29974a03ba9e382d29d7
sha512=2831db57fcd09206687a92fc9ee7d363e87bcec5167949e9c8161d41c63d5887b9f3556bfd63a7a9a0c12e5a5dc61a397baf4f496ebdb2a2fce122142c05e7a1

doc/src/lambdapi.export/lean.ml.html

Source file lean.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
(** Translate parser-level Lambdapi commands to Lean.

There are two modes:

- raw_lean: translation of commands as they are

- stt_lean: translation of commands modulo an encoding of simple type theory
specified through a lambdapi file with the option --encoding. A map giving the
number of type variables of each function symbol can be given as a .tvs file
(as generated by hol2dk) with the option --arities.

In both modes, a renaming map can be provided with the option --renaming to
rename some identifiers. *)

open Lplib open Extra
open Common open Pos open Error
open Parsing open Syntax
open Stt

let typ_arity h =
  match h.elt with
  | P_Iden({elt=(_,sym_name);_},true) when !stt ->
      begin
        match StrMap.find_opt sym_name !tvs_map with
        | None -> 0
        | Some n -> n
      end
  | _ -> 0

let is_Set id =
  match QidMap.find_opt id.elt !encoding with
  | Some Set -> true
  | _ -> false

let is_typ_Set = function
  | Some{elt=P_Iden(id,_);_} -> is_Set id
  | _ -> false

let nonempty oc =
  List.iter (fun id -> string oc " [Nonempty "; param_id oc id; char oc ']')

(** Translation of terms. *)

let rec term oc t =
  (*if Logger.log_enabled() then
    log "pp %a" (*Pos.short t.pos*) Pretty.term t;*)
  match t.elt with
  | P_Meta _ -> wrn t.pos "TODO"; assert false
  | P_Patt _ -> wrn t.pos "TODO"; assert false
  | P_Expl _ -> wrn t.pos "TODO"; assert false
  | P_Type -> string oc "Type"
  | P_Wild -> char oc '_'
  | P_NLit _ -> wrn t.pos "TODO"; assert false
  | P_SLit _ -> wrn t.pos "TODO"; assert false
  | P_Iden(qid,b) -> if b then char oc '@'; qident oc qid
  | P_Arro(u,v) -> arrow oc u v
  | P_Abst(xs,u) -> abst oc xs u
  | P_Prod(xs,u) -> prod oc xs u
  | P_LLet(x,xs,a,u,v) ->
    string oc "let "; ident oc x; params_list oc xs; typopt oc a;
    string oc " := "; term oc u; string oc " ; "; term oc v
  | P_Wrap u -> term oc u
  | P_Appl _ ->
      let default h ts =
        paren oc h; char oc ' ';
        let ts =
          let n = typ_arity h in
          if n <= 0 then ts else
            let l1,l2 = List.cut ts n in
            l1 @ List.init n (fun _ -> P.wild) @ l2
        in
        list paren " " oc ts
      in
      app t default
        (fun h ts expl builtin ->
          match !use_notations, !use_implicits && not expl, builtin, ts with
          | _, _, (El|Prf), [u] -> term oc u
          | _, _, (Arr|Imp), [u;v] -> arrow oc u v
          | _, _, All, [_;{elt=P_Wrap({elt=P_Abst([_] as xs,u);_});_}]
          | _, true, All, [{elt=P_Wrap({elt=P_Abst([_] as xs,u);_});_}]
            -> prod oc xs u
          | _, _, Ex, [_;{elt=P_Wrap({elt=P_Abst([x],u);_});_}]
          | _, true, Ex, [{elt=P_Wrap({elt=P_Abst([x],u);_});_}] ->
              string oc "∃ "; raw_params oc x; string oc ", "; term oc u
          | true, _, Eq, [_;u;v]
          | true, true, Eq, [u;v] -> paren oc u; string oc " = "; paren oc v
          | true, _, Or, [u;v] -> paren oc u; string oc " ∨ "; paren oc v
          | true, _, And, [u;v] ->  paren oc u; string oc " ∧ "; paren oc v
          | true, _, Not, [u] -> string oc "¬ "; paren oc u
          | _ -> default h ts)

and arrow oc u v = paren oc u; string oc " -> "; term oc v
and abst oc xs u =
  string oc "fun"; params_list_in_abs oc xs; string oc " => "; term oc u
and prod oc xs u =
  string oc "∀"; params_list_in_prod oc xs; string oc ", "; term oc u

and paren oc t =
  let default() = char oc '('; term oc t; char oc ')' in
  match t.elt with
  | P_Arro _ | P_Abst _ | P_Prod _ | P_LLet _ | P_Wrap _ -> default()
  | P_Appl _ ->
      app t (fun _ _ -> default())
        (fun _ ts _ builtin ->
          match builtin, ts with
          | (El|Prf), [u] -> paren oc u
          | _ -> default())
  | _ -> term oc t

and raw_params oc (ids,t,_) = param_ids oc ids; typopt oc t

and params oc ((ids,t,b) as x) =
  match b, t with
  | true, _ -> char oc '{'; raw_params oc x; char oc '}'
  | false, Some _ -> char oc '('; raw_params oc x; char oc ')'
  | false, None -> param_ids oc ids

and top_params oc ((ids,a,_) as x) =
  params oc x; if is_typ_Set a then nonempty oc ids

and params_list oc = List.iter (prefix " " params oc)

and top_params_list oc = List.iter (prefix " " top_params oc)

(* same as top_params_list but avoid some parentheses *)
and params_list_in_prod oc l =
  match l with
  | [(ids,a,_) as x] ->
    if is_typ_Set a then (char oc ' '; params oc x; nonempty oc ids)
    else params_list_in_abs oc l
  | _ -> top_params_list oc l

(* same as params_list but avoid some parentheses *)
and params_list_in_abs oc l =
  match l with
  | [(_,_,false) as x] -> char oc ' '; raw_params oc x
  | _ -> params_list oc l

and typopt oc t = Option.iter (prefix " : " term oc) t

(** Translation of commands. *)

let req_mod oc p = string oc "import "; path oc p; newline oc
let open_mod oc p = string oc "open "; path oc p; newline oc

let openings = ref []

(* [is_typ_constr t = true] iff [t] is of the form [Set -> .. -> Set]. *)
let rec is_typ_constr {elt;_} =
  match elt with
  | P_Iden(id,_) -> is_Set id
  | P_Arro({elt=P_Iden(id,_);_},t) -> is_Set id && is_typ_constr t
  | _ -> false

(* If [is_typ_constr t], then [replace_Set_by_Type t] returns [Some(u,n)] with
   [u] a copy of [t] where each occurrence of Set has been replaced by TYPE,
   and [n>=0] is the number of replacements - 1. Otherwise,
   [replace_Set_by_Type t = None]. *)
let replace_Set_by_Type =
  let n = ref 0 in
  let rec aux ({elt;_} as t) =
    match elt with
    | P_Iden(id,_) when is_Set id -> incr n; {t with elt=P_Type}
    | P_Arro({elt=P_Iden(id,_);_} as u, v) when is_Set id ->
      incr n; {t with elt=P_Arro({u with elt=P_Type}, aux v)}
    | _ -> raise Exit
  in
  fun t -> n := 0; try let u = aux t in Some(u, !n - 1) with Exit -> None

let typ_vars oc n =
  if n > 0 then
    (string oc "a0"; for i = 1 to n - 1 do string oc " a"; int oc i done)

let command oc {elt; pos} =
  begin match elt with
  | P_open(_,_,ps) -> List.iter (open_mod oc) ps
  | P_require(b,ps) ->
      List.iter (req_mod oc) ps;
      begin
        match b with
        | Some true -> openings := List.rev_append ps !openings
        | _ -> () (*FIXME?*)
      end
  | P_require_as(p,i) -> Stt.alias := StrMap.add i.elt p.elt !Stt.alias
  | P_symbol { p_sym_mod; p_sym_kw=_; p_sym_nam; p_sym_arg; p_sym_typ;
               p_sym_trm; p_sym_prf=_; p_sym_def } ->
    if not (is_mapped p_sym_nam.elt) then
        begin match p_sym_def, p_sym_trm, p_sym_arg, p_sym_typ with
          | true, Some t, _, Some a when List.exists is_lem p_sym_mod ->
            string oc "nonrec theorem "; ident oc p_sym_nam;
            top_params_list oc p_sym_arg; string oc " : "; term oc a;
            string oc " := by apply "; term oc t; newline oc
          | true, Some t, _, _ ->
            if List.exists is_opaq p_sym_mod then string oc "opaque "
            else string oc "@[reducible]\nnoncomputable def ";
            ident oc p_sym_nam;
            top_params_list oc p_sym_arg; typopt oc p_sym_typ;
            string oc " := "; term oc t; newline oc
          | false, _, [], Some t ->
            string oc "axiom "; ident oc p_sym_nam; string oc " : ";
            begin match replace_Set_by_Type t with
            | None -> term oc t
            | Some(t,n) -> term oc t;
              string oc "\n@[instance]\naxiom ne_"; ident oc p_sym_nam;
              if n > 0 then begin
                string oc " ("; typ_vars oc n; string oc " : Type)"
              end;
              string oc " : Nonempty";
              if n > 0 then begin
                string oc " ("; ident oc p_sym_nam; char oc ' ';
                typ_vars oc n; char oc ')'
              end
            end;
            newline oc
          | false, _, _, Some t ->
            string oc "axiom "; ident oc p_sym_nam;
            string oc " : ∀"; top_params_list oc p_sym_arg; string oc ", ";
            term oc t; newline oc
          | _ -> wrn pos "Command not translated."
        end
  | _ -> wrn pos "Command not translated."
  end

let commands oc = Stream.iter (command oc)

let handle_requires cmds =
  let rec handle_next_elt() =
    let cmd = Stream.next cmds in
    match cmd.elt with
    | P_require(b, ps) ->
        List.iter (req_mod stdout) ps;
        begin
          match b with
          | Some true -> openings := List.rev_append ps !openings;
          | _ -> ()
        end;
        handle_next_elt()
    | _ -> Some cmd
  in
  try handle_next_elt() with Stream.Failure -> None

let unset_option oc s =
  string oc "set_option "; string oc s; string oc " false\n"

let print : string -> p_commands -> unit =
  let space = Str.regexp "[ \t\n]+" in
  let oc = stdout in
  fun file cmds ->
  let requires =
    match !require with
    | None -> []
    | Some s -> Str.split space s
  in
  List.iter (fun s -> string oc "import "; string oc s; newline oc) requires;
  match handle_requires cmds with
  | None -> ()
  | Some cmd ->
    newline oc;
    List.iter (unset_option oc)
      ["linter.style.header"
      ;"linter.style.missingEnd"
      ;"linter.unusedVariables"
      ;"linter.style.longLine"];
    if !openings <> [] then newline oc;
    List.iter (open_mod oc) (List.rev !openings);
    string oc "\nnamespace ";
    string oc (String.concat "." !Common.Library.root_path);
    char oc '.';
    string oc (Filename.chop_extension file);
    string oc "\n\n";
    command oc cmd;
    commands oc cmds