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

Source file parser.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
(** Parsing functions for Lambdapi.

    This module includes two parsers: a parser for the Lambdapi syntax whose
    functions are available either through the submodule {!module:Parser.Lp}
    or directly in {!module:Parser}, and a parser for the Dedukti syntax
    available through {!module:Parser.Dk}. *)

open Lplib open Base
open Common
open Syntax
open Lexing

(** [parser_fatal pos fmt] is a wrapper for [Error.fatal] that enforces
    that the error has an attached source code position. *)
let parser_fatal : Pos.pos -> ('a,'b) koutfmt -> 'a = fun pos fmt ->
  Error.fatal (Some(pos)) fmt

(** Module type of a parser. *)
module type PARSER = sig

  type lexbuf

  val parse_lexbuf : lexbuf -> p_commands
  (** [parse_lexbuf lb] is the same as [parse_string] but with an already
      created lexbuf. *)

  val parse_in_channel : string -> in_channel -> p_commands
  (** [parse f ic] returns a stream of commands parsed from channel [ic]
      created from file [f]. Commands are parsed lazily and the channel is
      closed once all entries are parsed. *)

  val parse_file : string -> p_commands
  (** [parse_file f] returns a stream of parsed commands of file [f]. Commands
      are parsed lazily. *)

  val parse_string : string -> string -> p_commands
  (** [parse_string f s] returns a stream of parsed commands from string [s]
      which comes from file [f] ([f] can be anything). *)

end

(* defined in OCaml >= 4.11 only *)
let set_filename (lb:Lexing.lexbuf) (fname:string): unit =
  lb.lex_curr_p <- {lb.lex_curr_p with pos_fname = fname}

(** Parsing dk syntax. *)
module Dk : PARSER with type lexbuf := Lexing.lexbuf = struct

  open Lexing

  let parse_lexbuf (icopt:in_channel option)
        (entry:lexbuf -> 'a) (lb:lexbuf) : 'a Stream.t =
    let generator _ =
      try Some(entry lb)
      with
      | End_of_file -> Option.iter close_in icopt; None
      | DkParser.Error ->
          let pos = Pos.locate (lb.lex_start_p, lb.lex_curr_p) in
          parser_fatal pos "Unexpected token \"%s\"." (lexeme lb)
    in
    Stream.from generator

  let parse_in_channel (entry:lexbuf -> 'a) (fname:string) (ic:in_channel)
      : 'a Stream.t =
    let lb = from_channel ic in
    set_filename lb fname;
    parse_lexbuf (Some ic) entry lb

  let parse_file entry fname = parse_in_channel entry fname (open_in fname)

  let parse_string (entry: lexbuf -> 'a) (fname:string) (s:string)
      : 'a Stream.t =
    let lb = from_string s in
    set_filename lb fname;
    parse_lexbuf None entry lb

  let command =
    let r = ref (Pos.none (P_open(None,false,[]))) in
    fun (lb:lexbuf): p_command ->
    Debug.(record_time Parsing
             (fun () -> r := DkParser.line DkLexer.token lb)); !r

  (* exported functions *)
  let parse_string = parse_string command
  let parse_in_channel = parse_in_channel command
  let parse_file = parse_file command
  let parse_lexbuf = parse_lexbuf None command
end

open Sedlexing

(** Parsing lp syntax. *)
module Lp :
sig
  include PARSER with type lexbuf := Sedlexing.lexbuf

  val parse_term_string: Lexing.position -> string -> Syntax.p_term
  (** [parse_rwpatt_string p s] parses a term from string [s] assuming that
      [s] starts at position [p]. *)

  val parse_rwpatt_string: Lexing.position -> string -> Syntax.p_rwpatt
  (** [parse_rwpatt_string f s] parses a rewrite pattern specification from
      string [s] assuming that [s] starts at position [p]. *)

  val parse_search_string:
    allow_rocq_syntax:bool -> alone:bool -> Lexing.position -> string ->
     Syntax.search
  (** [parse_search_string ~allow_rocq_syntax ~alone p s] parses a query
      from string [s] assuming that [s] starts at position [p].
      [~allow_rocq_syntax] to allow a subset of Rocq syntax
       (fun/forall/exists)
      [~alone] to parse the query only if followed by EOF. *)

  end
= struct

  type zlexbuf = LpLexer.token OneLookaheadCellLexbuf.lexbuf

  let handle_error (icopt: in_channel option)
        (entry: zlexbuf -> 'a) (lb: zlexbuf): 'a option =
    try Some(entry lb)
    with
    | End_of_file -> Option.iter close_in icopt; None
    | LpLexer.SyntaxError(_,{pos=None; _}) -> assert false
    | LpLexer.SyntaxError(_,{pos=Some pos; elt}) ->
        parser_fatal pos "Syntax error. %s" elt

  let parse_lexbuf' ~allow_rocq_syntax (icopt: in_channel option)
        (entry: zlexbuf -> 'a) (lb: lexbuf): 'a Stream.t =
    Stream.from
     (fun _ ->
       handle_error icopt entry
        (OneLookaheadCellLexbuf.new_parser
          ~pp:LpParser.pp_token
           ~lexer_:(fun () -> LpLexer.token ~allow_rocq_syntax lb)))

  let parse_lexbuf (icopt: in_channel option)
       (entry: zlexbuf -> 'a) (lb: lexbuf): 'a Stream.t =
   parse_lexbuf' ~allow_rocq_syntax:false icopt entry lb

  let parse_string (entry: zlexbuf -> 'a) (fname: string) (s: string)
      : 'a Stream.t =
    let lb = Utf8.from_string s in
    set_filename lb fname;
    parse_lexbuf None entry lb

  let parse_in_channel (entry: zlexbuf -> 'a) (fname:string) (ic: in_channel)
      : 'a Stream.t =
    let lb = Utf8.from_channel ic in
    set_filename lb fname;
    parse_lexbuf (Some ic) entry lb

  let parse_file (entry: zlexbuf -> 'a) (fname: string): 'a Stream.t =
    parse_in_channel entry fname (open_in fname)

  let parse_entry_string ~allow_rocq_syntax (entry: zlexbuf -> 'a)
   (lexpos:Lexing.position) (s:string): 'a =
    let lb = Utf8.from_string s in
    set_position lb lexpos;
    set_filename lb lexpos.pos_fname;
    Stream.next (parse_lexbuf' ~allow_rocq_syntax None entry lb)

  (* exported functions *)
  let parse_term_string =
    parse_entry_string ~allow_rocq_syntax:false LpParser.term
  let parse_rwpatt_string =
    parse_entry_string ~allow_rocq_syntax:false LpParser.rwpatt
  let parse_search_string ~allow_rocq_syntax ~alone =
   parse_entry_string ~allow_rocq_syntax
    (if alone then LpParser.alone_search else LpParser.search)

  let parse_in_channel = parse_in_channel LpParser.command
  let parse_file = parse_file LpParser.command
  let parse_string = parse_string LpParser.command
  let parse_lexbuf = parse_lexbuf None LpParser.command

end

include Lp
open Error

(** [path_of_string s] converts the string [s] into a path. *)
let path_of_string : string -> Path.t = fun s ->
  let lb = Utf8.from_string s in
  try
    begin match LpLexer.token ~allow_rocq_syntax:false lb with
      | UID s, _, _ -> [s]
      | QID p, _, _ -> List.rev p
      | _ -> fatal_no_pos "Syntax error: \"%s\" is not a path." s
    end
  with
      LpLexer.SyntaxError _ ->
      fatal_no_pos "Syntax error: \"%s\" is not a path." s

(** [qident_of_string s] converts the string [s] into a qident. *)
let qident_of_string : string -> Core.Term.qident = fun s ->
  let lb = Utf8.from_string s in
  try
    begin match LpLexer.token ~allow_rocq_syntax:false lb with
      | QID [], _, _ -> assert false
      | QID (s::p), _, _ -> (List.rev p, s)
      | _ ->
          fatal_no_pos "Syntax error: \"%s\" is not a qualified identifier." s
    end
  with
    | LpLexer.SyntaxError _ ->
      fatal_no_pos "Syntax error: \"%s\" is not a qualified identifier." s

(** [parse_file fname] selects and runs the correct parser on file [fname], by
    looking at its extension. *)
let parse_file : string -> p_commands = fun fname ->
  match Filename.check_suffix fname Library.lp_src_extension with
  | true  -> Lp.parse_file fname
  | false -> Dk.parse_file fname