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/dk.ml.html

Source file dk.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
(** Export a Lambdapi signature to Dedukti. *)

open Lplib open Base open Extra
open Timed
open Common
open Core open Term

(** Translation of identifiers. Lambdapi identifiers that are Dedukti keywords
   or invalid Dedukti identifiers are escaped, a feature offered by
   Dedukti. *)

let keyword_table = Hashtbl.create 59

let is_keyword : string -> bool = Hashtbl.mem keyword_table

let _ = let open Parsing.DkTokens in
  let loc = Lexing.dummy_pos, Lexing.dummy_pos in
  List.iter (fun (s, t) -> Hashtbl.add keyword_table s t)
    [ ".", DOT
    ; ",", COMMA
    ; ":", COLON
    ; "==", EQUAL
    ; "[", LEFTSQU
    ; "]", RIGHTSQU
    ; "{", LEFTBRA
    ; "}", RIGHTBRA
    ; "(", LEFTPAR
    ; ")", RIGHTPAR
    ; "-->", LONGARROW
    ; "->", ARROW
    ; "=>", FATARROW
    ; ":=", DEF
    ; "Type", TYPE loc
    ; "def", KW_DEF loc
    ; "defac", KW_DEFAC loc
    ; "defacu", KW_DEFACU loc
    ; "injective", KW_INJ loc
    ; "thm", KW_THM loc
    ; "private", KW_PRV loc
    ; "#NAME", NAME (loc, "")
    ; "#REQUIRE", REQUIRE (loc, "")
    ; "#EVAL", EVAL loc
    ; "#INFER", INFER loc
    ; "#CHECK", CHECK loc
    ; "#CHECKNOT", CHECKNOT loc
    ; "#ASSERT", ASSERT loc
    ; "#ASSERTNOT", ASSERTNOT loc
    ; "#PRINT", PRINT loc
    ; "#GDT", GDT loc]

let is_ident : string -> bool = fun s ->
  Parsing.DkLexer.is_ident (Lexing.from_string s)

let is_mident : string -> bool = fun s ->
  Parsing.DkLexer.is_mident (Lexing.from_string s)

(*let replace_spaces : string -> string = fun s ->
  let open Bytes in
  let b = of_string s in
  for i=0 to length b - 1 do
    match get b i with
    | ' ' | '\n' -> set b i '_'
    | _ -> ()
  done;
  to_string b*)

let ident : string pp = fun ppf s ->
  string ppf
    (if s = "" then Escape.escape s
     else if s.[0] = '{' then s
     else if is_keyword s then Escape.escape s
     else if is_ident s then s
     else Escape.escape s)

let current_path = Stdlib.ref []

let qid : (Path.t * string) pp = fun ppf (p, i) ->
  let qualif ppf p =
    if p <> Stdlib.(!current_path) then
      match p with
      | [] -> ()
      | p -> out ppf "%s." (List.last p)
  in
  out ppf "%a%a" qualif p ident i

(** Type of Dedukti declarations. *)
type decl =
  | Sym of sym
  | Rule of (Path.t * string * rule)

(** Declarations are ordered wrt their declaration positions. *)

let pos_of_decl : decl -> Pos.popt = fun i ->
  match i with
  | Sym s -> s.sym_decl_pos
  | Rule (_,_,r) -> r.rule_pos

let cmp : decl cmp = cmp_map (Lplib.Option.cmp Pos.cmp) pos_of_decl

(** Translation of terms. *)

let var : var pp = fun ppf v -> string ppf (uniq_name v)

let patt : int pp = fun ppf i -> out ppf "x%d" i

(** [term b ppf t] prints term [t]. Print abstraction domains if [b]. *)
let rec term : bool -> term pp = fun b ppf t ->
  match unfold t with
  | Vari v -> var ppf v
  | Type -> out ppf "Type"
  | Kind -> assert false
  | Symb s -> qid ppf (s.sym_path, s.sym_name)
  | Prod(t,u) ->
    let x,u' = unbind u in
    if binder_occur u
    then out ppf "(%a : %a -> %a)" var x (term b) t (term b) u'
    else out ppf "(%a -> %a)" (term b) t (term b) u'
  | Abst(t,u) ->
    let x,u = unbind u in
    if b then out ppf "(%a : %a => %a)" var x (term b) t (term b) u
    else out ppf "(%a => %a)" var x (term b) u
  | Appl _ ->
    let h, ts = get_args t in
    out ppf "(%a%a)" (term b) h (List.pp (prefix " " (term b)) "") ts
  | LLet(a,t,u) ->
    let x,u = unbind u in
    out ppf "((%a : %a := %a) => %a)" var x (term b) a (term b) t (term b) u
  | Patt(None,_,_) -> assert false
  | Patt(Some i,_,[||]) -> patt ppf i
  | Patt(Some i,_,ts) ->
    out ppf "(%a%a)" patt i (Array.pp (prefix " " (term b)) "") ts
  | Bvar _ -> assert false
  | TRef _ -> assert false
  | Wild -> assert false
  | Meta _ -> assert false
  | Plac _ -> assert false

(** Translation of declarations. *)

(* Dedukti only accepts the following declarations:
def s : A := t
thm s : A := t
injective s : A
private s : A
private def s : A
private injective s : A
def s : A
defac s [A]
private defac [A]
*)
let sym_type : sym pp = fun ppf s ->
  match s.sym_prop with
  | AC _ ->
      out ppf "[%a]" (term true)
        (match unfold !(s.sym_type) with Prod(t,_) -> t | _ -> assert false)
  | _ -> out ppf ": %a" (term true) !(s.sym_type)

let sym_decl_only : sym pp = fun ppf s ->
  out ppf "%s%s%a %a.@."
    (if s.sym_expo = Protec then "private " else "")
    (match s.sym_prop with
     | AC _ -> "defac "
     | Injec -> "injective "
     | Defin -> "def "
     | Assoc _ | Commu -> assert false
     | Const -> "")
    ident s.sym_name sym_type s

let sym_decl : sym pp = fun ppf s ->
  match !(s.sym_def) with
  | None -> sym_decl_only ppf s
  | Some d ->
      (* in case of an AC, injective or protected symbol, we need to give the
         definition in a rule after the declaration *)
      if is_modulo s || s.sym_prop = Injec || s.sym_expo = Protec then
        out ppf "%a[] %s --> %a.@." sym_decl_only s s.sym_name (term true) d
      else
        out ppf "%s%a : %a := %a.@."
          (if !(s.sym_opaq) then "thm " else "def ")
          ident s.sym_name (term true) !(s.sym_type) (term true) d

let rule_decl : (Path.t * string * rule) pp = fun ppf (p, n, r) ->
  let rec var ppf i =
    if i < 0 then ()
    else if i = 0 then patt ppf 0
    else out ppf "%a,%a" var (i-1) patt i
  in
  out ppf "[%a] %a%a --> %a.@." var (r.vars_nb - 1) qid (p, n)
    (List.pp (prefix " " (term false)) "") r.lhs (term true) r.rhs

let decl : decl pp = fun ppf decl ->
  match decl with
  | Sym s -> sym_decl ppf s
  | Rule r -> rule_decl ppf r

(** [decls_of_sign sign] computes a list of declarations for the
   signature [sign], in order of appearance in the source. *)
let decls_of_sign : Sign.t -> decl list = fun sign ->
  let add_sym l s = List.insert cmp (Sym s) l
  and add_rule p n l r =
    if p = Sign.Ghost.path then l
    else List.insert cmp (Rule (p, n, r)) l
  in
  let add_sign_symbol n s l =
    List.fold_left (add_rule [] n) (add_sym l s) !(s.sym_rules) in
  let add_rules p n sd l = List.fold_left (add_rule p n) l sd.Sign.rules in
  let add_sign_dep p d l = StrMap.fold (add_rules p) d.Sign.dep_symbols l in
  StrMap.fold add_sign_symbol !(sign.sign_symbols)
    (Path.Map.fold add_sign_dep !(sign.sign_deps) [])

(** Translation of a signature. *)

let mident : Path.t pp = fun ppf p ->
  let n = List.last p in
  if is_mident n then string ppf n
  else Error.fatal_no_pos "\"%s\" is not a valid module name in Dedukti" n

let require : Path.t -> _ -> unit = fun p _ ->
  if p <> Sign.Ghost.path then
    out Format.std_formatter "#REQUIRE %a.\n" mident p

let sign : Sign.t -> unit = fun sign ->
  Path.Map.iter require !(sign.sign_deps);
  Stdlib.(current_path := sign.sign_path);
  List.iter (decl Format.std_formatter) (decls_of_sign sign)