forked from AU-COBRA/ConCert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCameLIGOPretty.v
1081 lines (968 loc) · 43.9 KB
/
CameLIGOPretty.v
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(** * Pretty-printing CameLIGO code *)
(** Adapted from [EPretty] of MetaCoq.Erasure. *)
(** ** Features/limitations *)
(** Printing covers most constructs of CIC_box (terms after erasure).
Usually we have to remove redundant boxes before printing.
There are some limitations on what can work after extraction,
due to the nature of CameLIGO, or sometimes, lack of proper support.
CameLIGO allows only tail-recursive calls and recursive functions must
have only one argument. So, we pack multiple arguments in a tuple.
In order for that to be correct, we assume that all fixpoints are
fully applied. *)
From MetaCoq.Erasure.Typed Require Import Utils.
From MetaCoq.Erasure.Typed Require Import ExAst.
From MetaCoq.Erasure.Typed Require Import Annotations.
From MetaCoq.Erasure.Typed Require Import Extraction.
From MetaCoq.Erasure.Typed Require Import ResultMonad.
From MetaCoq.Erasure Require Import EAst.
From MetaCoq.Erasure Require Import EAstUtils.
From MetaCoq.Utils Require Import MCList.
From MetaCoq.Utils Require Import MCPrelude.
From ConCert.Utils Require Import Env.
From ConCert.Utils Require Import Extras.
From ConCert.Utils Require Import StringExtra.
From ConCert.Execution Require ResultMonad.
From ConCert.Extraction Require Import Common.
From Coq Require Import String.
From Coq Require Import Nat.
From Coq Require Import ZArith.
From Coq Require Import List.
From Coq Require Import Basics.
Local Open Scope string_scope.
Import Kernames ListNotations.
(* F# style piping notation *)
Notation "f <| x" := (f x) (at level 32, left associativity, only parsing).
(* i.e. f <| x <| y = (f <| x) <| y, and means (f x) y *)
Notation "x |> f" := (f x) (at level 31, left associativity, only parsing).
(* i.e. x |> f |> g = (x |> f) |> g, and means g (f x) *)
Notation "s1 ^ s2" := (String.append s1 s2).
Class CameLIGOPrintConfig :=
{ (* cosnstructors start with an uppercase letter *)
print_ctor_name : kername -> string;
(* types start with a lowercase letter *)
print_type_name : kername -> string;
(* constants start with a lowercase letter *)
print_const_name : kername -> string }.
Notation "'bs_to_s' s" := (bytestring.String.to_string s) (at level 200).
Local Coercion bytestring.String.to_string : bytestring.String.t >-> string.
(** Prepend the last module name all global declaration names to avoid name clashes. Due
to limitations for constructors, use only part of the module name and cut the first
31 letters *)
Module PrintConfAddModuleNames.
Definition last_module_name (mp : modpath) : string :=
match mp with
| MPdot mp' nm => nm
| MPfile (nm :: _) => nm
| _ => ""
end.
Definition print_ctor_name_ (kn : kername) :=
let lmn := last_module_name (fst kn) in
let nm := (snd kn) in
(* NOTE: CameLIGO has a limitation for the constructor name length,
so we try our best to fit and don't get clashes *)
let ctor_name := if lmn =? "" then bs_to_s nm else substring 0 4 lmn ^ "_" ^ nm in
capitalize (substring 0 31 ctor_name).
Definition print_ind_type_name_ (ind_kn : kername) :=
let lmn := last_module_name (fst ind_kn) in
let nm := snd ind_kn in
let ty_name := if lmn =? "" then bs_to_s nm else lmn ^ "_" ^ nm in
uncapitalize ty_name.
Definition print_const_name_ (ind_kn : kername) :=
let lmn := last_module_name (fst ind_kn) in
let nm := (snd ind_kn) in
let ty_name := if lmn =? "" then bs_to_s nm else lmn ^ "_" ^ nm in
uncapitalize ty_name.
Local Instance PrintWithModuleNames : CameLIGOPrintConfig :=
{| print_ctor_name := print_ctor_name_;
print_type_name := print_ind_type_name_;
print_const_name := print_const_name_ |}.
End PrintConfAddModuleNames.
(** Print names only, don't prepend module names or any other parts of the
fully qualified path. Gives more readable code, but might lead to name clashes *)
Module PrintConfShortNames.
Local Instance PrintWithShortNames : CameLIGOPrintConfig :=
{| print_ctor_name := fun kn => capitalize (snd kn);
print_type_name := fun kn => uncapitalize (snd kn);
print_const_name := fun kn => uncapitalize (snd kn) |}.
End PrintConfShortNames.
Section PPTerm.
Context `{CameLIGOPrintConfig}.
Context (Σ : ExAst.global_env).
Definition look (e : env string) (s : string) : option string :=
lookup e s.
Definition is_rel (t : Ast.term) :=
match t with
| Ast.tRel _ => true
| _ => false
end.
(* Use string_of_nat from StringExtra instead of MetaCoq's string_of_nat *)
Definition string_of_nat := StringExtra.string_of_nat.
Definition tokenize := str_split.
(** Takes a fully qualified name and returns the last part, e.g.
for "Coq.ZArith.BinInt.Z.add" returns "add" *)
Definition unqual_name nm := last (tokenize "." nm) ("Error (Malformed_qualified_name)").
Definition print_uncurried (args : list string) :=
let arg_len := #|args| in
if (arg_len =? 0)%nat then
""
else
let no_parens := (#|args| =? 1)%nat in
Common.parens (no_parens) (String.concat ", " args).
Definition print_uncurried_app (s : string) (args : list string) :=
let print_parens := (Nat.ltb 0 (List.length args)) in
s ++ " " ++ print_uncurried args.
Definition is_arr (bt : box_type) : bool :=
match bt with
| TArr _ _ => true
| _ => false
end.
Definition map_targs (f : box_type -> string) : box_type -> list string :=
fix go bt := match bt with
| TApp t1 t2 => (go t1 ++ [f t2])%list
| _ => []
end.
Fixpoint get_tapp_hd (bt : box_type) : box_type :=
match bt with
| TApp t1 t2 => get_tapp_hd t1
| _ => bt
end.
(* Certain names in CameLIGO are reserved (like 'to' and others)
so we ensure no fresh names are reserved *)
(* Note: for reserved names from the syntax (like 'let', 'in', 'match', etc.)
we don't need to add them since they are also reserved names in Coq, hence
we can't write Coq programs with these names anyway. *)
Definition is_reserved_name (id : string) (reserved : list string) :=
List.existsb (String.eqb id) reserved.
Definition ligo_reserved_names :=
[
"to"
; "val"
; "amount"
; "balance"
; "continue"
; "hash"
; "sender"
].
(* NOTE: We should probably add more validation rules *)
Definition ligo_valid_type_var (tv : string) : bool :=
match String.index 0 "'" tv with
| Some _ => false
| None => true
end.
Open Scope bool.
Import BasicAst EAst.
Definition is_fresh (Γ : context) (id : string) :=
negb (is_reserved_name id ligo_reserved_names)
&& List.forallb
(fun decl =>
match decl.(decl_name) with
| nNamed id' =>
(* NOTE: we compare the identifiers up to
the capitalization of the first letters *)
negb (String.eqb (uncapitalize id) (uncapitalize id'))
| nAnon => true
end) Γ.
Fixpoint name_from_term (t : term) :=
match t with
| tRel _ | tVar _ | tEvar _ _ => "h"
| tLambda na t => "f"
| tLetIn na b t' => name_from_term t'
| tApp f _ => name_from_term f
| tConst c => "x"
| _ => "u"
end.
Definition fresh_id_from ctx n (id : string) :=
let fix aux i :=
match i with
| 0 => id
| S i' =>
let id' := id ^ string_of_nat (n - i) in
if is_fresh ctx id' then id'
else aux i'
end
in aux n.
(* NOTE: LIGO doesn't support wildcard *)
Definition fresh_string_name (ctx : context) (na : name) : string :=
let id := match na with
| nAnon => "a"
| nNamed nm => if nm =? "_" then "a" else nm
end
in
let id := uncapitalize id in
if ligo_valid_type_var id then
if is_fresh ctx id then id
else
fresh_id_from ctx (List.length ctx) id
else fresh_id_from ctx (List.length ctx) "a".
Definition fresh_string_names (Γ : context) (vs : list name) : context * list string :=
let fix go (Γ : context) (vs : list name) : context * list string :=
match vs with
| [] => (Γ, [])
| v :: vs0 =>
let nm := fresh_string_name Γ v in
(* add name to the context to avoid shadowing due to name clashes *)
let Γ1 := vass (nNamed (bytestring.String.of_string nm)) :: Γ in
let '(Γ2, vs1) := go Γ1 vs0 in
(Γ2, nm :: vs1)
end in
go Γ vs.
(** The [for_ind] flag tells the type printer whether the type is used in an inductive
type definition or in a function, since the syntax is different for these two cases
in CameLIGO *)
Definition print_type_var_name (for_ind : bool) (var_name : string) :=
let type_var_prefix := if for_ind then "'" else "" in
type_var_prefix ++ var_name.
(** Resolve a type level (index) into a variable name and print it *)
(* INVARIANT: the names in the context are distinct *)
Definition print_type_var (ty_ctx : list string) (for_ind : bool) (i : nat) :=
match nth_error ty_ctx i with
| Some var => print_type_var_name for_ind var
| None => "UnboundTypeVar"
end.
Definition print_box_type_aux (for_ind : bool) (ty_ctx : list string) (TT : env string)
: box_type -> string :=
fix go (bt : box_type) :=
match bt with
| TBox => "unit"
| TArr dom codom => parens (negb (is_arr dom)) (go dom) ++ " -> " ++ go codom
| TApp t1 t2 =>
let hd := get_tapp_hd t1 in
let args := map_targs go bt in
match hd with
| TInd ind =>
(* a special case of products - infix *)
if eq_kername <%% prod %%> ind.(inductive_mind) then
parens false (String.concat " * " args)
(* inductives are printed with args prefix style, e.g. "int option" *)
else print_uncurried args ++ " " ++ go hd
| _ => print_uncurried args ++ " " ++ go hd
end
| TVar i => print_type_var ty_ctx for_ind i (* TODO: pass context with type variables *)
| TInd s =>
let full_ind_name := string_of_kername s.(inductive_mind) in
with_default (print_type_name s.(inductive_mind)) (look TT full_ind_name)
| TConst s =>
let full_cst_name := string_of_kername s in
with_default (print_type_name s) (look TT full_cst_name)
| TAny => "UnknownType"
end.
Open Scope program_scope.
Definition print_box_type := print_box_type_aux false.
Definition print_ind_box_type := print_box_type_aux true.
Definition print_ctor
(TT : env string)
(ind_kn : kername)
(ty_ctx : list string)
(ctor : ident * list (name * box_type) * nat) :=
let '(nm,tys,_) := ctor in
let nm := print_ctor_name (fst ind_kn, nm) in
match tys with
| [] => nm
| _ => let ctor_type := parens (#|tys| <=? 1)%nat <| String.concat " * " (map (print_ind_box_type ty_ctx TT ∘ snd) tys)
in nm ^ " of " ^ ctor_type
end.
Definition print_proj (TT : env string) (ty_ctx : list string) (proj : ident * box_type) : string :=
let (nm, ty) := proj in
nm ^ " : " ^ print_ind_box_type ty_ctx TT ty.
Definition print_type_params (ty_ctx : list string) : string :=
if (Nat.eqb #|ty_ctx| 0) then ""
else
let ps := String.concat "," (map (print_type_var_name true) ty_ctx) in
parens (Nat.eqb #|ty_ctx| 1) ps ^ " ".
Definition print_type_declaration (nm : string) (ty_ctx : list string) (body : string) : string :=
"type " ^ print_type_params ty_ctx ^ nm ^ " = " ^ body.
Definition print_inductive (ind_kn : kername) (TT : env string)
(oib : ExAst.one_inductive_body) :=
let ind_nm := with_default (print_type_name (fst ind_kn, oib.(ExAst.ind_name)))
(lookup TT oib.(ExAst.ind_name)) in
(* a context of type variable names, potentially renamed to avoid clashes *)
let '(_, type_params_ctx) := fresh_string_names [] (map tvar_name oib.(ind_type_vars)) in
(* one-constructor inductives are interpreted/printed as records *)
match oib.(ExAst.ind_ctors), oib.(ExAst.ind_projs) with
| [build_record_ctor], _ :: _ =>
let '(_, ctors,_) := build_record_ctor in
let projs_and_ctors := combine oib.(ExAst.ind_projs) ctors in
let projs_and_ctors_printed := map (fun '(p, (na, ty)) => print_proj TT type_params_ctx (fst p, ty)) projs_and_ctors in
let body :=
<$ "{";
String.concat (";" ^ nl) projs_and_ctors_printed;
"}" $> in
print_type_declaration ind_nm type_params_ctx body
| _,_ =>
let body :=
<$ "";
" " ^ String.concat "| " (map (fun p => print_ctor TT ind_kn type_params_ctx p ^ nl) oib.(ExAst.ind_ctors)) $> in
print_type_declaration ind_nm type_params_ctx body
end.
Definition is_sort (t : Ast.term) :=
match t with
| Ast.tSort _ => true
| _ => false
end.
Definition is_prod (t : Ast.term) :=
match t with
| Ast.tProd _ _ _ => true
| _ => false
end.
Open Scope bool.
Fixpoint Edecompose_lam (t : term) : (list name) * term :=
match t with
| tLambda n b =>
let (ns, b) := Edecompose_lam b in
(n :: ns, b)
| _ => ([], t)
end.
Definition lookup_ind_decl ind i :=
match ExAst.lookup_env Σ ind with
| Some (ExAst.InductiveDecl {| ExAst.ind_bodies := l |}) =>
match nth_error l i with
| Some body => Some body
| None => None
end
| _ => None
end.
Definition get_constr_name (ind : inductive) (i : nat) : string :=
match lookup_ind_decl ind.(inductive_mind) ind.(inductive_ind) with
| Some oib =>
match nth_error oib.(ExAst.ind_ctors) i with
| Some (na, _,_) => bs_to_s na
| None =>
"UnboundConstruct(" ++ string_of_inductive ind ++ "," ++ string_of_nat i ++ ")"
end
| None =>
"UnboundConstruct(" ++ string_of_inductive ind ++ "," ++ string_of_nat i ++ ")"
end.
Definition is_pair_constr (ind : inductive) :=
eq_kername ind.(inductive_mind) <%% prod %%>.
Definition print_pair (f : term -> string) (t1 : term) (t2 : term) :=
parens false ((f t1) ++ " ," ++ (f t2)).
Definition is_list_cons (ind : inductive) (ctor_num : nat) :=
andb (eq_kername ind.(inductive_mind) <%% list %%>)
(Nat.eqb ctor_num 1).
Definition print_list_cons (f : term -> string) (t1 : term) (t2 : term) :=
(f t1) ++ " :: " ++ (f t2).
Definition is_record_constr (t : term) : option ExAst.one_inductive_body :=
match t with
| tConstruct (mkInd mind j as ind) i _ =>
match lookup_ind_decl mind i with
(* Check if it has only 1 constructor, and projections are specified *)
| Some oib => match ExAst.ind_projs oib, Nat.eqb 1 (List.length oib.(ExAst.ind_ctors)) with
| _::_,true => Some oib
| _,_ => None
end
| _ => None
end
| _ => None
end.
Definition is_name_remapped nm TT :=
match (look TT nm) with
| Some nm' => true
| None => false
end.
Definition app_args {A} (f : term -> A) :=
fix go (t : term) := match t with
| tApp t1 t2 => f t2 :: go t1
| _ => []
end.
Definition app_args_annot {A} (f : (∑ t, annots box_type t) -> A) :=
fix go (t : term) : annots box_type t -> list A :=
match t with
| tApp t1 t2 => fun '(bt, (hda, arga)) => (f (t2; arga)) :: go t1 hda
| _ => fun _ => []
end.
(** Builds a context for the branch *)
Definition get_ctx_from_branch (ctx : context) : nat -> term -> context :=
let fix go (ctx : context) (arity: nat) (branch : term) :=
match arity with
| 0 => []
| S n =>
match branch with
| tLambda na B =>
let na' := fresh_string_name ctx na in
go (vass (nNamed (bytestring.String.of_string na')) :: ctx) n B
| _ => []
end
end
in go [].
Definition print_pat (TT : env string) (ind_kn : kername)
(ctor : string) (infix : bool)
(pt : list string * string) :=
let vars := rev (fst pt) in
if infix then
String.concat (" " ++ ctor ++ " ") vars ++ " -> " ++ (snd pt)
else
let ctor_nm := with_default (print_ctor_name (fst ind_kn, bytestring.String.of_string ctor)) (look TT ctor) in
let ctor_nm := if ctor_nm =? "Pair" then "" else ctor_nm in
let print_parens := (Nat.ltb 1 (List.length (fst pt))) in
print_uncurried_app ctor_nm vars ++ " -> " ++ (snd pt).
Definition print_num_literal (TT : env string) (t : term) : option string :=
(* is it a natural number literal? *)
match nat_syn_to_nat t with
| Some n => Some (string_of_nat n ^ "n")
| None =>
(* is it a positive binary number [positive] literal? *)
match pos_syn_to_nat t with
| Some k => Some (string_of_nat k ^ "n")
| None => (* is it a binary natural number [N] literal? *)
match N_syn_to_nat t with
| Some m =>
(* NOTE: we check whether [N] is remapped to [int], if it's NOT,
we add "n", since we consider it a natural number *)
let N_remapped := with_default "" (look TT (string_of_kername <%% N %%>)) in
let units := if N_remapped =? "int" then "" else "n" in
Some (string_of_nat m ^ units)
| None =>
(* is it an integer number [Z] literal? *)
match Z_syn_to_Z t with
| Some z =>
(* NOTE: we check whether [Z] is remapped to [tez],
if so, we add "tz" to the literal *)
let Z_remapped := with_default "" (look TT (string_of_kername <%% BinInt.Z %%>)) in
let units := if Z_remapped =? "tez" then "tez" else "" in
Some (string_of_Z z ^ units)
| None => None
end
end
end
end.
Definition print_transfer (args : list string) :=
match args with
| [] => "MalformedTransfer()"
| [a1; a2] => "Tezos.transaction unit " ++ a2 ++ " (get_contract_unit " ++ a1 ++ ")"
| _ => "MalformedTransfer(" ++ String.concat "," args ++ ")"
end.
(** ** The pretty-printer *)
(** [TT] - translation table allowing for remapping constants and constructors to
CameLIGO primitives, if required.
[ctx] - context that gets updated when we go under lambda, let, pattern or
fixpoint.
[ty_ctx] - context of type variables. Note that we don't update it, meaning that
polymorphic definitions can be only top-level for now. To allow local polymorphic
definitions (not sure whether these are supported in LIGO), we need to update type
annotations, so they provide type context as well.
[top,inapp] - flags used to determine how to print parenthesis.
[t] - a term to be printed. *)
Fixpoint print_term (TT : env string)
(ctx : context)
(ty_ctx : list string)
(top : bool)
(inapp : bool)
(t : term)
{struct t} : annots box_type t -> string :=
match t return annots box_type t -> string with
| tBox => fun bt => "()" (* boxes become the constructor of the [unit] type *)
| tRel n => fun bt =>
match nth_error ctx n with
| Some {| decl_name := na |} =>
match na with
| nAnon => "Anonymous (" ++ string_of_nat n ++ ")"
| nNamed id => id
end
| None => "UnboundRel(" ++ string_of_nat n ++ ")"
end
| tVar n => fun bt => "Var(" ++ n ++ ")"
| tEvar ev args => fun bt => "Evar(" ++ string_of_nat ev ++ "[]" (* TODO *) ++ ")"
| tLambda na body => fun '(bt, a) =>
let na' := fresh_string_name ctx na in
let (dom_tys, _) := ExAst.decompose_arr bt in
let dom_ty := match nth_error dom_tys 0 with
| Some ty => print_box_type ty_ctx TT ty
| None => "LambdaError(NotAnArrowType)"
end in
parens top ("fun (" ++ na' ++ " : "
++ dom_ty ++ ")"
++ " -> " ++ print_term TT (vass (nNamed (bytestring.String.of_string na')) :: ctx) ty_ctx true false body a)
| tLetIn na def body => fun '(bt, (vala, bodya)) =>
let na' := fresh_string_name ctx na in
parens top ("let " ++ na' ++
" = " ++ print_term TT ctx ty_ctx true false def vala ++ " in " ++ nl ++
print_term TT (vdef (nNamed (bytestring.String.of_string na')) def :: ctx) ty_ctx true false body bodya)
| tApp f l as t => fun '(bt, (fa, la)) =>
let apps := rev (app_args_annot (fun '(t; a) => print_term TT ctx ty_ctx false false t a) t (bt, (fa, la))) in
let '((b; ba),argas) := Edecompose_app_annot f fa in
match apps with
| [] => print_term TT ctx ty_ctx false true f fa
| _ =>
match b with
| tConst c =>
let cst_name := string_of_kername c in
let nm := with_default (print_const_name c) (look TT cst_name) in
(* primitive projections instead of 'fst' and 'snd' *)
if nm =? "fst" then
(String.concat " " (map (parens true) apps)) ++ ".0"
else if nm =? "snd" then
(String.concat " " (map (parens true) apps)) ++ ".1"
else parens (top || inapp) (nm ++ " " ++ (String.concat " " (map (parens true) apps)))
| tConstruct (mkInd mind j as ind) i [] =>
let nm := get_constr_name ind i in
(* is it a pair ? *)
if (uncapitalize nm =? "pair") then
print_uncurried apps
(* is it a cons ? *)
else if (uncapitalize nm =? "cons") then
parens top (String.concat " :: " apps)
(* is it a transfer *)
else if (uncapitalize nm =? "act_transfer") then
print_transfer apps
(* is it a Ok *)
else if ((nm =? "Ok") && (eq_kername mind <%% ConCert.Execution.ResultMonad.result %%>)) then
"(" ++ parens false (print_uncurried_app nm apps) ++ ":" ++ print_box_type ty_ctx TT bt ++ ")"
else if ((nm =? "Err") && (eq_kername mind <%% ConCert.Execution.ResultMonad.result %%>)) then
"(" ++ parens false (print_uncurried_app nm apps) ++ ":" ++ print_box_type ty_ctx TT bt ++ ")"
else if (nm =? "_") then
fresh_id_from ctx 10 "a"
(* inductive constructors of 1 arg are treated as records *)
else
match print_num_literal TT t with
| Some s => s
| None =>
match is_name_remapped nm TT, is_record_constr b with
| false, Some oib =>
let projs_and_apps := combine (map (bytestring.String.to_string ∘ fst) oib.(ExAst.ind_projs)) apps in
let field_decls_printed := projs_and_apps |> map (fun '(proj, e) => proj ++ " = " ++ e)
|> String.concat "; " in
"({" ++ field_decls_printed ++ "}: " ++ print_box_type ty_ctx TT bt ++ ")"
| _,_ =>
let nm' := with_default (print_ctor_name (fst mind, bytestring.String.of_string nm)) (look TT nm) in
(* constructors take a single argument (uncurried),
so we wrap the args into a tuple *)
parens top (print_uncurried_app nm' apps)
end
end
| _ => parens (top || inapp) (print_term TT ctx ty_ctx false true f fa ++ " " ++ print_term TT ctx ty_ctx false false l la)
end
end
| tConst c => fun bt =>
let cst_name := string_of_kername c in
let nm_tt := with_default (print_const_name c) (look TT cst_name) in
(* NOTE: empty maps require type annotations *)
(* TODO: this looks ad-hoc and should be controlled in remapping instead *)
if (nm_tt =? "Map.empty") || (nm_tt =? "AddressMap.empty") then
"(Map.empty: " ++ print_box_type ty_ctx TT bt ++ ")"
else
nm_tt
| tConstruct ind l [] => fun bt =>
let nm := get_constr_name ind l in
match print_num_literal TT t with
| Some lit => lit
| None =>
let nm_tt := with_default (print_ctor_name (fst ind.(inductive_mind), bytestring.String.of_string nm)) (look TT nm) in
(* NOTE: print annotations for 0-ary constructors of polymorphic types (like [], None, and Map.empty) *)
(* TODO: this looks ad-hoc and should be controlled in remapping instead *)
if (uncapitalize nm =? "nil") && (eq_kername ind.(inductive_mind) <%% list %%>) then
"([]:" ++ print_box_type ty_ctx TT bt ++ ")"
else if ((nm =? "None") && (eq_kername ind.(inductive_mind) <%% option %%>)) ||
(nm_tt =? "Map.empty") then
"(" ++ nm_tt ++ ":" ++ print_box_type ty_ctx TT bt ++ ")"
else nm_tt
end
| tCase (mkInd mind i as ind, nparam) t brs =>
let print_branch ctx (br_ctx : list name) (br_body : term) : annots box_type br_body -> (list string * string) :=
fun a =>
let ctx' := map vass br_ctx in
let nas' := fresh_string_names (ctx ++ ctx')%list br_ctx in
let b := print_term TT (fst nas') ty_ctx false false br_body a in
(List.rev (snd nas'), b)%list in
match brs with
| [] => fun '(bt, (ta, trs)) => (parens false ("failwith 0 : " ^ print_box_type ty_ctx TT bt) ^ " (* absurd case *)")
| _ =>
(* [if-then-else] is a special case *)
if eq_kername mind <%% bool %%> then
match brs with
| [b1; b2] => fun '(bt, (ta, (b1a, (b2a, _)))) =>
parens top
("if " ++ print_term TT ctx ty_ctx true false t ta
++ " then " ++ print_term TT ctx ty_ctx true false (snd b1) b1a
++ " else " ++ print_term TT ctx ty_ctx true false (snd b2) b2a)
| _ => fun bt => "Error (Malformed pattern-mathing on bool: given "
++ string_of_nat #|brs| ++ " branches " ++ ")"
end
else
fun '(bt, (ta, trs)) =>
match lookup_ind_decl mind i with
| Some oib =>
let brs :=
map_with_bigprod _ (fun br tra => print_branch ctx (fst br) (snd br) tra) brs trs in
let brs_ := combine brs oib.(ExAst.ind_ctors) in
let brs_printed : string :=
Common.print_list (fun '(b, (na, _,_)) =>
(* [list] is a special case *)
let na := bs_to_s na in
if (eq_kername mind <%% list %%>) && (na =? "cons") then
print_pat TT mind "::" true b
(* else if (eq_kername mind <%% list %%>) && (na =? "nil") then *)
(* print_pat "" TT mind "[]" false b *)
else
print_pat TT mind na false b)
(nl ++ " | ") brs_ in
parens top
("match " ++ print_term TT ctx ty_ctx true false t ta
++ " with " ++ nl
++ brs_printed)
| None =>
"Case(" ++ string_of_inductive ind ++ "," ++ string_of_nat i ++ "," ++ string_of_term t ++ ","
++ MCString.string_of_list (fun b => string_of_term (snd b)) brs ++ ")"
end
end
| tConstruct ind l (_ :: _) => fun _ => "Error(constructors_as_blocks_not_supported)"
| tProj (Kernames.mkProjection (mkInd mind i) pars k) c => fun bt =>
let ind := mkInd mind i in
match lookup_ind_decl mind i with
| Some oib =>
match nth_error oib.(ExAst.ind_projs) k with
| Some (na, _) => print_term TT ctx ty_ctx false false c (snd bt) ++ "." ++ na
| None => "UnboundProj(" ++ string_of_inductive ind ++ "," ++ string_of_nat i ++ "," ++ string_of_nat k ++ ","
++ print_term TT ctx ty_ctx true false c (snd bt) ++ ")"
end
| None =>
"UnboundProj(" ++ string_of_inductive ind ++ "," ++ string_of_nat i ++ "," ++ string_of_nat k ++ ","
++ print_term TT ctx ty_ctx true false c (snd bt) ++ ")"
end
| tFix [fix_decl] n => fun '(bt, (fixa, _)) => (* NOTE: We assume that the fixpoints are not mutual *)
let (tys,ret_ty) := decompose_arr bt in
let fix_name := fresh_string_name ctx fix_decl.(dname) in
let body := fix_decl.(dbody) in
let '(args, _) := Edecompose_lam_annot body fixa in
(* NOTE: adding the fix variable to the context *)
let ctx := vass (nNamed (bytestring.String.of_string fix_name)) :: ctx in
let sret_ty := print_box_type ty_ctx TT ret_ty in
let (ctx, sargs) := fresh_string_names ctx args in
let targs := combine sargs (map (print_box_type ty_ctx TT) tys) in
let sargs_typed := String.concat " " (map (fun '(x,ty) => parens false (x ++ " : " ++ ty)) targs) in
let fix_call := parens false (fix_name ^ " : " ^ print_box_type ty_ctx TT bt) in
(* NOTE: we cannot directly use the result of decomposing with
[Edecompose_lam_annot] because the guardedness check cannot see through it *)
let fix_body := lam_body_annot_cont (fun body body_annot => print_term TT ctx ty_ctx true false body body_annot) fix_decl.(dbody) fixa in
parens top ("let rec " ++ fix_name ^ " " ^ sargs_typed ^ " : " ^ sret_ty ^ " = " ^ nl ^
fix_body ^ nl ^
" in " ^ fix_call)
| tFix [] _ => fun _ => "FixWithNoBody"
| tFix _ _ => fun _ => "NotSupportedMutualFix"
| tCoFix l n => fun _ => "NotSupportedCoFix"
| tPrim _ => fun _ => "NotSupportedCoqPrimitive"
| tLazy _ => fun _ => "NotSupportedLazy"
| tForce _ => fun _ => "NotSupportedForce"
end.
End PPTerm.
Section PPLigo.
Context `{CameLIGOPrintConfig}.
Fixpoint decompose_arr_n (n : nat) (bt : box_type) : list box_type * box_type :=
match n, bt with
| S m, TArr dom cod =>
let (args, res) := decompose_arr_n m cod in (dom :: args, res)
| _,_ => ([], bt)
end.
(* INVARIANT: the names in the contexts are distinct *)
Definition print_forall (ty_ctx : list string) : string :=
match ty_ctx with
| [] => ""
| _ => let binders := String.concat " " ty_ctx in
parens false ("type " ++ binders)
end.
Example print_forall_ex :
let (_, tys) := fresh_string_names []
[BasicAst.nNamed (bytestring.String.of_string "a"); BasicAst.nNamed (bytestring.String.of_string "b"); BasicAst.nAnon] in
print_forall tys = "(type a b a0)".
Proof. reflexivity. Qed.
Example print_forall_empty :
print_forall [] = "".
Proof. reflexivity. Qed.
Import bytestring.String.
Example print_ex1 :
(let (args,_) := Edecompose_lam (tLambda (BasicAst.nNamed (bytestring.String.of_string "a"))
(tLambda (BasicAst.nNamed (bytestring.String.of_string "b")) (tRel 0))) in
fresh_string_names [{|
Extract.E.decl_name := BasicAst.nNamed (String "b" EmptyString);
Extract.E.decl_body := None
|}] args) =
([{|
Extract.E.decl_name :=
BasicAst.nNamed (String "b" (String "0" EmptyString));
Extract.E.decl_body := None
|};
{|
Extract.E.decl_name := BasicAst.nNamed (String "a" EmptyString);
Extract.E.decl_body := None
|};
{|
Extract.E.decl_name := BasicAst.nNamed (String "b" EmptyString);
Extract.E.decl_body := None
|}], ["a"; "b0"]).
Proof. reflexivity. Qed.
Definition print_decl
(TT : env string) (* translation table *)
(env : ExAst.global_env)
(decl_kn : kername)
(wrap : string -> string)
(ty_in_ctx : list BasicAst.name * box_type)
(t : term)
(ta : annots box_type t) :=
let '(args,(lam_body; body_annot)) := Edecompose_lam_annot t ta in
let (ctx, args) := fresh_string_names [] args in
let '(ty_ctx, ty) := ty_in_ctx in
let '(_, forall_abstractions) := fresh_string_names [] ty_ctx in
let (tys,res_ty) := decompose_arr_n #|args| ty in
let targs := combine args (map (print_box_type forall_abstractions TT) tys) in
let printed_targs :=
map (fun '(x,ty) => parens false (x ^ " : " ^ ty)) targs in
let printed_res_ty := print_box_type forall_abstractions TT res_ty in
let printed_forall := print_forall forall_abstractions in
let decl := print_const_name decl_kn ^ printed_forall ^ " " ^ String.concat " " printed_targs in
"let" ^ " " ^ decl ^ " : " ^ printed_res_ty ^ " = " ^ nl
^ wrap (print_term env TT ctx forall_abstractions true false lam_body body_annot).
(* NOTE: we assume that [init] is not a polymorphic function, so we use an empty type
context when printing types *)
Definition print_init
(TT : env string) (* translation table *)
(build_call_ctx : string) (* a string that corresponds to a call context *)
(init_prelude : string) (* operations available in the [init] as local definitions.
CameLIGO does not allow referring to global definitions in [init]*)
(env : ExAst.global_env)
(cst : ExAst.constant_body)
: (constant_body_annots box_type cst) -> option string :=
match cst.(ExAst.cst_body) as body return match body with
| Some body => annots box_type body
| None => unit
end -> option string with
| Some b => fun cstAnnot =>
let '(args,(lam_body; body_annot)) := Edecompose_lam_annot b cstAnnot in
let ty := cst.(ExAst.cst_type) in
let (tys,inner_ret_ty) := decompose_arr (snd ty) in
let '(ctx, sargs) := fresh_string_names [] args in
let targs_inner := combine sargs (map (print_box_type [] TT) tys) in
let printed_targs_inner :=
map (fun '(x,ty) => parens false (x ++ " : " ++ ty)) targs_inner in
let decl_inner := "inner " ++ String.concat " " printed_targs_inner in
let printed_inner_ret_ty := print_box_type [] TT inner_ret_ty in
let printed_outer_ret_ty :=
match inner_ret_ty with
| TApp (TInd ind) t1 =>
if eq_kername <%% option %%> ind.(inductive_mind) then
print_box_type [] TT t1
else print_box_type [] TT inner_ret_ty
| _ => print_box_type [] TT inner_ret_ty
end in
let wrap t :=
"match " ++ t ++ " with" ++ nl ++
" Ok v -> Ok v"++ nl ++
"| Err e -> (failwith e: " ++ printed_outer_ret_ty ++ ")" in
let let_inner :=
"let " ++ decl_inner ++ " :" ++ printed_inner_ret_ty ++ " = " ++ nl
++ print_term env TT ctx [] true false lam_body body_annot
++ " in" in
let printed_targs_outer := printed_targs_inner in
let decl_outer :=
"init " ++ String.concat " " printed_targs_outer ++ " : " ++ printed_outer_ret_ty in
let inner_app := "inner " ++ String.concat " " sargs in
Some ("let " ++ decl_outer ++ " = "
++ let_inner
++ nl
++ wrap (parens false inner_app))
| None => fun _ => None
end.
Definition print_cst
(TT : env string) (* translation table *)
(env : ExAst.global_env)
(kn : kername)
(cst : ExAst.constant_body)
: (constant_body_annots box_type cst) -> option string :=
match cst.(ExAst.cst_body) as body return match body with
| Some body => annots box_type body
| None => unit
end -> option string with
| Some cst_body => fun annot =>
Some (print_decl TT env kn id cst.(ExAst.cst_type) cst_body annot)
| None => fun _ => None
end.
(* A wrapper type to separate declarations into type declarations ("type ... = ...")
and constant declarations ("let x = ...") *)
Inductive Decl a : Type :=
| TyDecl : a -> Decl a
| ConstDecl : a -> Decl a.
Global Arguments TyDecl {_}.
Global Arguments ConstDecl {_}.
Definition print_global_decl (TT : env string)
(nm : kername)
(env : ExAst.global_env)
(d : ExAst.global_decl) :
(global_decl_annots box_type d) -> Decl (kername * string) :=
match d return (global_decl_annots box_type d) -> Decl (kername * string) with
| ExAst.ConstantDecl cst => fun annot =>
match print_cst TT env nm cst annot with
| Some r => ConstDecl (nm, r)
| None => ConstDecl (nm, "print_global_decl ConstantDecl ERROR: " ++ string_of_kername nm)
end
| ExAst.InductiveDecl mib as d => fun annot =>
match mib.(ExAst.ind_bodies) with
| [oib] => TyDecl (nm, print_inductive nm TT oib)
| _ => TyDecl (nm,"Only non-mutual inductives are supported; " ++ string_of_kername nm)
end
| TypeAliasDecl (Some (params, ty)) =>
fun annot =>
let ta_nm := with_default (print_type_name nm) (lookup TT (string_of_kername nm)) in
let (_,ty_ctx) := fresh_string_names [] (map tvar_name params) in
TyDecl (nm, print_type_declaration ta_nm ty_ctx (print_box_type ty_ctx TT ty))
| TypeAliasDecl None => fun _ => TyDecl (nm, "")
end.
Fixpoint print_global_env (TT : env string)
(env : ExAst.global_env)
: (env_annots box_type env) -> list (Decl (kername * string)) :=
match env return (env_annots box_type env) -> list (Decl (kername * string)) with
| (kn, has_deps, decl) :: env' =>
fun '(a,b) =>
(* Filtering out empty type declarations *)
(* TODO: possibly, move to extraction (requires modifications of the correctness proof) *)
if is_empty_type_decl decl then print_global_env TT env' b
else
let printed :=
(* only print decls for which the environment includes dependencies *)
if has_deps then
print_global_decl TT kn env' decl a
else ConstDecl (kn, "") in
printed :: print_global_env TT env' b
| [] => fun _ => []
end.
Local Open Scope string_scope.
(** We un-overload operations and add definitions that are more convenient
to use during the pretty-printing phase. These part should be included
when printing contracts that use the corresponding operations. *)
Definition int_ops :=
<$
"[@inline] let addInt (i : int) (j : int) = i + j" ;
"[@inline] let subInt (i : int) (j : int) = i - j" ;
"[@inline] let subIntTruncated (a : int) (b : int) = let res = a - b in if res < 0 then 0 else res" ;
"[@inline] let multInt (i : int) (j : int) = i * j" ;
"[@inline] let divInt (i : int) (j : int) = i / j" ;
"[@inline] let modInt (a : int)(b : int) : int = int (a mod b)" ;
"[@inline] let leInt (i : int) (j : int) = i <= j" ;
"[@inline] let ltInt (i : int) (j : int) = i < j" ;
"[@inline] let eqInt (i : int) (j : int) = i = j"
$>.
Definition tez_ops :=
<$
"[@inline] let addTez (n : tez) (m : tez) = n + m" ;
"[@inline] let subTez (n : tez) (m : tez) : tez option = n - m" ;
"[@inline] let leTez (a : tez) (b : tez) = a <= b" ;
"[@inline] let ltTez (a : tez) (b : tez) = a < b" ;
"[@inline] let gtbTez (a : tez) (b : tez) = a > b" ;
"[@inline] let eqTez (a : tez) (b : tez) = a = b" ;
"[@inline] let natural_to_mutez (a: nat) : tez = a * 1mutez" ;
"[@inline] let divTez (a : tez) (b : tez) : tez = natural_to_mutez (a/b)" ;
"[@inline] let multTez (n : tez) (m : tez) = (n/1tez) * m";
"[@inline] let evenTez (i : tez) = (i mod 2n) = 0tez"
$>.
Definition nat_ops :=
<$
"[@inline] let addN (a : nat) (b : nat) = a + b" ;
"[@inline] let multN (a : nat) (b : nat) = a * b" ;
"[@inline] let modN (a : nat) (b : nat) = a mod b" ;
"[@inline] let divN (a : nat) (b : nat) = a / b" ;
"[@inline] let eqN (a : nat) (b : nat) = a = b" ;
"[@inline] let lebN (a : nat) (b : nat) = a <= b" ;
"[@inline] let ltbN (a : nat) (b : nat) = a < b";
"let divN_opt (n : nat) (m : nat) : nat option = match ediv n m with | Some (q,_) -> Some q | None -> None";
"let moduloN (n : nat) (m : nat) : nat = match ediv n m with | Some (_,r) -> r | None -> 0n";
"let subOption (n : nat) (m : nat) : nat option = if n < m then None else Some (abs (n-m))";
"let z_to_N (i : int) : nat = if i < 0 then 0n else abs i";
"let z_of_N (n : nat) : int = int (n)"
$>.
Definition bool_ops :=
<$
"[@inline] let andb (a : bool) (b : bool) = a && b" ;
"[@inline] let orb (a : bool) (b : bool) = a || b"
$>.
Definition time_ops :=
<$
"[@inline] let eqb_time (a1 : timestamp) (a2 : timestamp) = a1 = a2" ;
"[@inline] let leb_time (a1 : timestamp) (a2 : timestamp) = a1 <= a2" ;
"[@inline] let ltb_time (a1 : timestamp) (a2 : timestamp) = a1 < a2"
$>.
Definition address_ops :=
"[@inline] let eq_addr (a1 : address) (a2 : address) = a1 = a2".
Definition get_contract_def :=
<$
"let get_contract_unit (a : address) : unit contract =" ;
" match (Tezos.get_contract_opt a : unit contract option) with" ;
" Some c -> c" ;
" | None -> (failwith (""Contract not found."") : unit contract)"
$>.
Definition CameLIGO_call_ctx_type_name : string := "cctx".
Definition CameLIGO_call_ctx_type :=
<$ "(* ConCert's call context *)"
; "type " ^ CameLIGO_call_ctx_type_name ^ " = {"
; " ctx_origin_ : address;"
; " ctx_from_ : address;"