Skip to content

Commit 4870ecf

Browse files
authored
Merge pull request #287 from Beluga-lang/ext/prop-eq
Merge the prop equality extension
2 parents 10b554d + 4b79c50 commit 4870ecf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+7017
-1233
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.vscode
2+
13
# Dune outputs
24
_build
35
*.opam

driver/Lexer.mll

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@
4343
| DOT _ -> "."
4444
| LET _ -> "let"
4545
| IN _ -> "in"
46-
| EQ _ -> ":="
46+
| DEF _ -> ":="
47+
| EQ _ -> "="
48+
| LANGLE _ -> "<"
49+
| RANGLE _ -> ">"
50+
| REFL _ -> "refl"
51+
| AS _ -> "as"
4752

4853
let get_range_of_token : token -> (position * position) =
4954
function
@@ -69,7 +74,12 @@
6974
| DOT r
7075
| LET r
7176
| IN r
77+
| DEF r
7278
| EQ r
79+
| LANGLE r
80+
| RANGLE r
81+
| REFL r
82+
| AS r
7383
| VAR (r, _) -> r
7484

7585
let format_token (f: Format.formatter) (t: token): unit =
@@ -109,7 +119,12 @@ rule read =
109119
| "." { DOT (get_range lexbuf) }
110120
| "let" {LET (get_range lexbuf) }
111121
| "in" {IN (get_range lexbuf) }
112-
| ":=" {EQ (get_range lexbuf) }
122+
| ":=" {DEF (get_range lexbuf) }
123+
| "=" {EQ (get_range lexbuf) }
124+
| "<" {LANGLE (get_range lexbuf) }
125+
| ">" {RANGLE (get_range lexbuf) }
126+
| "refl" {REFL (get_range lexbuf) }
127+
| "as" {AS (get_range lexbuf) }
113128
| string { VAR (get_range lexbuf, Lexing.lexeme lexbuf) }
114129
| _ as c { failwith (Format.asprintf "@[<v 2>Lexer error:@ @[<v 2>Unexpected character %C@ at %a@]@]@." c format_position lexbuf.lex_start_p) }
115130
and comment =

driver/PrettyPrinter.ml

Lines changed: 76 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ let rec get_pi_params_of_obj : Cst.obj -> (string * Cst.obj) list * Cst.obj =
4141
let rec format_obj_prec (p : int) (f : Format.formatter) : Cst.obj -> unit =
4242
let open Format in
4343
function
44+
| Cst.Coq_var x -> pp_print_string f x
4445
| Cst.Coq_typ i -> fprintf f "Type@%d" i
4546
| Cst.Coq_nat -> fprintf f "Nat"
4647
| Cst.Coq_zero -> fprintf f "0"
@@ -61,17 +62,10 @@ let rec format_obj_prec (p : int) (f : Format.formatter) : Cst.obj -> unit =
6162
format_obj escr mx format_obj em format_obj ez sx sr format_obj es
6263
in
6364
pp_print_paren_if (p >= 1) impl f ()
64-
| Cst.Coq_app (ef, ea) ->
65-
let impl f () =
66-
fprintf f "%a@ %a" (format_obj_prec 1) ef (format_obj_prec 2) ea
67-
in
68-
pp_open_hvbox f 2;
69-
pp_print_paren_if (p >= 2) impl f ();
70-
pp_close_box f ()
71-
| Cst.Coq_fn (px, ep, ebody) ->
72-
let params, ebody' = get_fn_params_of_obj ebody in
65+
| Cst.Coq_pi (px, ep, eret) ->
66+
let params, eret' = get_pi_params_of_obj eret in
7367
let impl f () =
74-
pp_print_string f "fun ";
68+
pp_print_string f "forall ";
7569
pp_open_tbox f ();
7670
pp_set_tab f ();
7771
pp_print_list ~pp_sep:pp_print_tab format_obj_param f ((px, ep) :: params);
@@ -81,15 +75,15 @@ let rec format_obj_prec (p : int) (f : Format.formatter) : Cst.obj -> unit =
8175
then pp_print_space f ()
8276
else pp_force_newline f ()
8377
end;
84-
fprintf f "-> @[<hov 2>%a@]" format_obj ebody'
78+
fprintf f "-> @[<hov 2>%a@]" format_obj eret'
8579
in
8680
pp_open_hvbox f 2;
8781
pp_print_paren_if (p >= 1) impl f ();
8882
pp_close_box f ()
89-
| Cst.Coq_pi (px, ep, eret) ->
90-
let params, eret' = get_pi_params_of_obj eret in
83+
| Cst.Coq_fn (px, ep, ebody) ->
84+
let params, ebody' = get_fn_params_of_obj ebody in
9185
let impl f () =
92-
pp_print_string f "forall ";
86+
pp_print_string f "fun ";
9387
pp_open_tbox f ();
9488
pp_set_tab f ();
9589
pp_print_list ~pp_sep:pp_print_tab format_obj_param f ((px, ep) :: params);
@@ -99,12 +93,49 @@ let rec format_obj_prec (p : int) (f : Format.formatter) : Cst.obj -> unit =
9993
then pp_print_space f ()
10094
else pp_force_newline f ()
10195
end;
102-
fprintf f "-> @[<hov 2>%a@]" format_obj eret'
96+
fprintf f "-> @[<hov 2>%a@]" format_obj ebody'
10397
in
10498
pp_open_hvbox f 2;
10599
pp_print_paren_if (p >= 1) impl f ();
106100
pp_close_box f ()
107-
| Cst.Coq_var x -> pp_print_string f x
101+
| Cst.Coq_app (ef, ea) ->
102+
let impl f () =
103+
fprintf f "%a@ %a" (format_obj_prec 1) ef (format_obj_prec 2) ea
104+
in
105+
pp_open_hvbox f 2;
106+
pp_print_paren_if (p >= 2) impl f ();
107+
pp_close_box f ()
108+
| Cst.Coq_prop_eq (e1, et, e2) ->
109+
let impl f () =
110+
fprintf f "%a@ =<%a>@ %a" (format_obj_prec 1) e1 (format_obj_prec 0) et (format_obj_prec 1) e2
111+
in
112+
pp_open_hvbox f 2;
113+
pp_print_paren_if (p >= 1) impl f ();
114+
pp_close_box f ()
115+
| Cst.Coq_refl (et, ex) ->
116+
let impl f () =
117+
fprintf f "refl@ %a@ %a" (format_obj_prec 2) et (format_obj_prec 2) ex
118+
in
119+
pp_open_hvbox f 2;
120+
pp_print_paren_if (p >= 1) impl f ();
121+
pp_close_box f ()
122+
| Cst.Coq_eqrec (escr, mx, my, mz, em, rx, er, e1, ea, e2) ->
123+
let impl f () =
124+
fprintf f
125+
"@[<hv 0>@[<hov 2>rec %a@ as %a =<%a> %a@ return %s %s %s . %a@]@ @[<hov 2>| refl %s =>@ \
126+
%a@]@ end@]"
127+
format_obj escr
128+
format_obj e1
129+
format_obj ea
130+
format_obj e2
131+
mx
132+
my
133+
mz
134+
format_obj em
135+
rx
136+
format_obj er
137+
in
138+
pp_print_paren_if (p >= 1) impl f ()
108139

109140
and format_obj_param f (px, ep) = Format.fprintf f "(%s : %a)" px format_obj ep
110141
and format_obj f = format_obj_prec 0 f
@@ -128,6 +159,9 @@ let exp_to_obj =
128159
fun () -> suffix := 0 )
129160
in
130161
let rec impl (ctx : string list) : exp -> Cst.obj = function
162+
| Coq_a_var x -> Cst.Coq_var (List.nth ctx x)
163+
| Coq_a_typ i -> Cst.Coq_typ i
164+
| Coq_a_nat -> Cst.Coq_nat
131165
| Coq_a_zero -> Cst.Coq_zero
132166
| Coq_a_succ e -> Cst.Coq_succ (impl ctx e)
133167
| Coq_a_natrec (em, ez, es, escr) ->
@@ -139,9 +173,11 @@ let exp_to_obj =
139173
let ez' = impl ctx ez in
140174
let es' = impl (sr :: sx :: ctx) es in
141175
Cst.Coq_natrec (escr', mx, em', ez', sx, sr, es')
142-
| Coq_a_nat -> Cst.Coq_nat
143-
| Coq_a_typ i -> Cst.Coq_typ i
144-
| Coq_a_var x -> Cst.Coq_var (List.nth ctx x)
176+
| Coq_a_pi (ep, eret) ->
177+
let px = match ep with Coq_a_typ _ -> new_tyvar () | _ -> new_var () in
178+
let ep' = impl ctx ep in
179+
let eret' = impl (px :: ctx) eret in
180+
Cst.Coq_pi (px, ep', eret')
145181
| Coq_a_fn (ep, ebody) ->
146182
let px = match ep with Coq_a_typ _ -> new_tyvar () | _ -> new_var () in
147183
let ep' = impl ctx ep in
@@ -151,11 +187,27 @@ let exp_to_obj =
151187
let ef' = impl ctx ef in
152188
let ea' = impl ctx ea in
153189
Cst.Coq_app (ef', ea')
154-
| Coq_a_pi (ep, eret) ->
155-
let px = match ep with Coq_a_typ _ -> new_tyvar () | _ -> new_var () in
156-
let ep' = impl ctx ep in
157-
let eret' = impl (px :: ctx) eret in
158-
Cst.Coq_pi (px, ep', eret')
190+
| Coq_a_eq (et, e1, e2) ->
191+
let et' = impl ctx et in
192+
let e1' = impl ctx e1 in
193+
let e2' = impl ctx e2 in
194+
Cst.Coq_prop_eq (e1', et', e2')
195+
| Coq_a_refl (et, ex) ->
196+
let et' = impl ctx et in
197+
let ex' = impl ctx ex in
198+
Cst.Coq_refl (et', ex')
199+
| Coq_a_eqrec (ea, em, er, e1, e2, escr) ->
200+
let ea' = impl ctx ea in
201+
let mx = match ea with Coq_a_typ _ -> new_tyvar () | _ -> new_var () in
202+
let my = match ea with Coq_a_typ _ -> new_tyvar () | _ -> new_var () in
203+
let mz = new_tyvar () in
204+
let em' = impl (mz :: my :: mx :: ctx) em in
205+
let rx = match em with Coq_a_typ _ -> new_tyvar () | _ -> new_var () in
206+
let er' = impl (rx :: ctx) er in
207+
let e1' = impl ctx e1 in
208+
let e2' = impl ctx e2 in
209+
let escr' = impl ctx escr in
210+
Cst.Coq_eqrec (escr', mx, my, mz, em', rx, er', e1', ea', e2')
159211
| Coq_a_sub _ -> failwith "Invalid internal language construct"
160212
in
161213
fun exp ->

0 commit comments

Comments
 (0)