-
Notifications
You must be signed in to change notification settings - Fork 10
/
command.ml
992 lines (941 loc) · 35.5 KB
/
command.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
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
open Bwd
open Dim
open Util
open List_extra
open Core
open Notation
open Postprocess
open Unparse
open Format
open Uuseg_string
open Print
open Reporter
open User
open Modifier
module Trie = Yuujinchou.Trie
type def = {
wsdef : Whitespace.t list;
name : Trie.path;
loc : Asai.Range.t option;
wsname : Whitespace.t list;
parameters : Parameter.t list;
wscolon : Whitespace.t list;
ty : observation option;
wscoloneq : Whitespace.t list;
tm : observation;
}
module Command = struct
type t =
| Axiom of {
wsaxiom : Whitespace.t list;
name : Trie.path;
loc : Asai.Range.t option;
wsname : Whitespace.t list;
parameters : Parameter.t list;
wscolon : Whitespace.t list;
ty : observation;
}
| Def of def list
(* "synth" is almost just like "echo", so we implement them as one command distinguished by an "eval" flag. *)
| Echo of { wsecho : Whitespace.t list; tm : observation; eval : bool }
| Notation : {
fixity : ('left, 'tight, 'right) fixity;
wsnotation : Whitespace.t list;
wstight : Whitespace.t list; (* Empty for outfix *)
wsellipsis : Whitespace.t list; (* Empty for non-associative *)
name : Trie.path;
loc : Asai.Range.t option;
wsname : Whitespace.t list;
wscolon : Whitespace.t list;
pattern : ('left, 'right) User.Pattern.t;
wscoloneq : Whitespace.t list;
head : [ `Constr of string | `Constant of Trie.path ];
wshead : Whitespace.t list;
args : (string * Whitespace.t list) list;
}
-> t
| Import of {
wsimport : Whitespace.t list;
export : bool;
origin : [ `File of string | `Path of Trie.path ];
wsorigin : Whitespace.t list;
op : (Whitespace.t list * modifier) option;
}
| Solve of {
wssolve : Whitespace.t list;
number : int;
wsnumber : Whitespace.t list;
wscoloneq : Whitespace.t list;
tm : observation;
}
| Show of {
wsshow : Whitespace.t list;
what : [ `Hole of Whitespace.t list * int | `Holes ];
wswhat : Whitespace.t list;
}
| Undo of { wsundo : Whitespace.t list; count : int; wscount : Whitespace.t list }
| Section of {
wssection : Whitespace.t list;
prefix : string list;
wsprefix : Whitespace.t list;
wscoloneq : Whitespace.t list;
}
| End of { wsend : Whitespace.t list }
| Quit of Whitespace.t list
| Bof of Whitespace.t list
| Eof
end
include Command
module Parse = struct
open Parse
module C = Combinators (Command)
open C.Basic
let token x = step "" (fun state _ (tok, w) -> if tok = x then Some (w, state) else None)
let ident =
step "" (fun state _ (tok, w) ->
match tok with
| Ident name -> Some ((name, w), state)
| _ -> None)
let variable =
step "" (fun state _ (tok, w) ->
match tok with
| Ident [ x ] -> Some ((Some x, w), state)
| Ident xs -> fatal (Invalid_variable xs)
| Underscore -> Some ((None, w), state)
| _ -> None)
let parameter =
let* wslparen = token LParen in
let* name, names = one_or_more variable in
let names = name :: names in
let* wscolon = token Colon in
let* ty = C.term [ RParen ] in
let* wsrparen = token RParen in
return ({ wslparen; names; wscolon; ty; wsrparen } : Parameter.t)
let axiom =
let* wsaxiom = token Axiom in
let* nameloc, (name, wsname) = located ident in
let loc = Some (Range.convert nameloc) in
let* parameters = zero_or_more parameter in
let* wscolon = token Colon in
let* ty = C.term [] in
return (Command.Axiom { wsaxiom; name; loc; wsname; parameters; wscolon; ty })
let def tok =
let* wsdef = token tok in
let* nameloc, (name, wsname) = located ident in
let loc = Some (Range.convert nameloc) in
let* parameters = zero_or_more parameter in
let* wscolon, ty, wscoloneq, tm =
(let* wscolon = token Colon in
let* ty = C.term [ Coloneq ] in
let* wscoloneq = token Coloneq in
let* tm = C.term [] in
return (wscolon, Some ty, wscoloneq, tm))
</>
let* wscoloneq = token Coloneq in
let* tm = C.term [] in
return ([], None, wscoloneq, tm) in
return ({ wsdef; name; loc; wsname; parameters; wscolon; ty; wscoloneq; tm } : def)
let def_and =
let* first = def Def in
let* rest = zero_or_more (def And) in
return (Command.Def (first :: rest))
let echo =
let* wsecho = token Echo in
let* tm = C.term [] in
return (Command.Echo { wsecho; tm; eval = true })
let synth =
let* wsecho = token Synth in
let* tm = C.term [] in
return (Command.Echo { wsecho; tm; eval = false })
let tightness_and_name :
(No.wrapped option * Whitespace.t list * Trie.path * Asai.Range.t option * Whitespace.t list)
t =
let* tloc, tight_or_name = located ident in
(let* nameloc, (name, wsname) = located ident in
let loc = Some (Range.convert nameloc) in
let tight, wstight = tight_or_name in
let tight = String.concat "." tight in
match No.of_rat (Q.of_string tight) with
| Some tight -> return (Some tight, wstight, name, loc, wsname)
| None | (exception Invalid_argument _) ->
fatal ~loc:(Range.convert tloc) (Invalid_tightness tight))
</>
let name, wsname = tight_or_name in
return (None, [], name, Some (Range.convert tloc), wsname)
let pattern_token =
step "" (fun state _ (tok, ws) ->
match tok with
| String str -> (
match Lexer.single str with
(* Currently we hard code a `Nobreak after each symbol in a notation. *)
| Some tok -> Some (`Op (tok, `Nobreak, ws), state)
| None -> fatal (Invalid_notation_symbol str))
| _ -> None)
let pattern_var =
let* x, ws = ident in
match x with
(* Currently we hard code a `Break after each variable in a notation. *)
| [ x ] -> return (`Var (x, `Break, ws))
| _ -> fatal (Invalid_variable x)
let pattern_ellipsis =
let* ws = token Ellipsis in
return (`Ellipsis ws)
(* The function fixity_of_pattern "typechecks" a user notation pattern, verifying all the invariants and producing an element of User.Pattern.t in which those invariants are statically guaranteed. It also interprets ellipses to produce a fixity: a starting ellipse before a variable means left-associative, an ending ellipse after a variable means right-associative, and any other use of ellipses is an error. *)
type fixity_and_pattern =
| Fix_pat :
('left, 'tight, 'right) fixity * ('left, 'right) User.Pattern.t
-> fixity_and_pattern
let fixity_of_pattern pat tight =
let rec go :
type left right.
[ `Var of User.Pattern.var | `Op of User.Pattern.op | `Ellipsis of Whitespace.t list ] Bwd.t ->
(left, right) User.Pattern.t ->
right User.Pattern.right_t =
fun bwd_pat new_pat ->
match bwd_pat with
| Emp -> Right new_pat
| Snoc (bwd_pat, `Var v) -> go bwd_pat (User.Pattern.var v new_pat)
| Snoc (bwd_pat, `Op v) -> go bwd_pat (Op (v, new_pat))
| Snoc (_, `Ellipsis _) -> fatal (Unimplemented "internal ellipses in notation") in
match pat with
| [] -> fatal (Invalid_notation_pattern "empty")
| [ `Ellipsis _ ] -> fatal (Invalid_notation_pattern "has no symbols")
| `Ellipsis _ :: `Op _ :: _ ->
fatal (Invalid_notation_pattern "prefix/outfix notation can't be left-associative")
| `Ellipsis _ :: `Ellipsis _ :: _ -> fatal (Invalid_notation_pattern "too many ellipses")
| `Op v :: pat -> (
match Bwd.of_list pat with
| Emp -> (Fix_pat (Outfix, Op_nil v), [])
| Snoc (bwd_pat, `Op w) ->
if Option.is_some tight then fatal Fixity_mismatch;
let (Right new_pat) = go bwd_pat (Op_nil w) in
(Fix_pat (Outfix, Op (v, new_pat)), [])
| Snoc (Snoc (bwd_pat, `Op o), `Var w) ->
let (No.Wrap tight) = tight <|> Fixity_mismatch in
let (Right new_pat) = go bwd_pat (Var_nil (o, w)) in
(Fix_pat (Prefix tight, Op (v, new_pat)), [])
| Snoc (Emp, `Var w) ->
let (No.Wrap tight) = tight <|> Fixity_mismatch in
(Fix_pat (Prefix tight, Var_nil (v, w)), [])
| Snoc (Snoc (_, `Var _), `Var _) ->
fatal (Invalid_notation_pattern "missing symbol between variables")
| Snoc (Snoc (_, `Ellipsis _), `Var _) ->
fatal (Unimplemented "internal ellipses in notation")
| Snoc (Snoc (Snoc (bwd_pat, `Op o), `Var w), `Ellipsis ws) ->
let (No.Wrap tight) = tight <|> Fixity_mismatch in
let (Right new_pat) = go bwd_pat (Var_nil (o, w)) in
(Fix_pat (Prefixr tight, Op (v, new_pat)), ws)
| Snoc (Snoc (_, `Op _), `Ellipsis _) | Snoc (_, `Ellipsis _) ->
fatal (Invalid_notation_pattern "postfix/outfix notation can't be right-associative"))
| `Var v :: pat -> (
match Bwd.of_list pat with
| Emp | Snoc (Emp, `Ellipsis _) -> fatal (Invalid_notation_pattern "has no symbols")
| Snoc (bwd_pat, `Op w) ->
let (No.Wrap tight) = tight <|> Fixity_mismatch in
let (Right new_pat) = go bwd_pat (Op_nil w) in
(Fix_pat (Postfix tight, User.Pattern.var v new_pat), [])
| Snoc (Snoc (bwd_pat, `Op o), `Var w) ->
let (No.Wrap tight) = tight <|> Fixity_mismatch in
let (Right new_pat) = go bwd_pat (Var_nil (o, w)) in
(Fix_pat (Infix tight, User.Pattern.var v new_pat), [])
| Snoc (Snoc (Snoc (bwd_pat, `Op o), `Var w), `Ellipsis ws) ->
let (No.Wrap tight) = tight <|> Fixity_mismatch in
let (Right new_pat) = go bwd_pat (Var_nil (o, w)) in
(Fix_pat (Infixr tight, User.Pattern.var v new_pat), ws)
| Snoc (Snoc (_, `Var _), `Var _)
| Snoc (Emp, `Var _)
| Snoc (Snoc (Snoc (_, `Var _), `Var _), `Ellipsis _)
| Snoc (Snoc (Emp, `Var _), `Ellipsis _) ->
fatal (Invalid_notation_pattern "missing symbol between variables")
| Snoc (Snoc (_, `Ellipsis _), `Var _)
| Snoc (Snoc (Snoc (_, `Ellipsis _), `Var _), `Ellipsis _) ->
fatal (Unimplemented "internal ellipses in notation")
| Snoc (Snoc (_, `Op _), `Ellipsis _) ->
fatal (Invalid_notation_pattern "postfix/outfix notation can't be right-associative")
| Snoc (Snoc (_, `Ellipsis _), `Ellipsis _) ->
fatal (Invalid_notation_pattern "too many ellipses"))
| `Ellipsis ws :: `Var v :: pat -> (
match Bwd.of_list pat with
| Emp -> fatal (Invalid_notation_pattern "has no symbols")
| Snoc (bwd_pat, `Op w) ->
let (No.Wrap tight) = tight <|> Fixity_mismatch in
let (Right new_pat) = go bwd_pat (Op_nil w) in
(Fix_pat (Postfixl tight, User.Pattern.var v new_pat), ws)
| Snoc (Snoc (bwd_pat, `Op o), `Var w) ->
let (No.Wrap tight) = tight <|> Fixity_mismatch in
let (Right new_pat) = go bwd_pat (Var_nil (o, w)) in
(Fix_pat (Infixl tight, User.Pattern.var v new_pat), ws)
| Snoc (Snoc (_, `Var _), `Var _) | Snoc (Emp, `Var _) ->
fatal (Invalid_notation_pattern "missing symbol between variables")
| Snoc (Snoc (_, `Ellipsis _), `Var _) ->
fatal (Unimplemented "internal ellipses in notation")
| Snoc (_, `Ellipsis _) ->
fatal (Invalid_notation_pattern "can't be both right and left associative"))
let notation_head =
step "" (fun state _ (tok, ws) ->
match tok with
| Ident name -> Some ((`Constant name, ws), state)
| Constr c -> Some ((`Constr c, ws), state)
| _ -> None)
let notation_var =
let* x, ws = ident in
match x with
| [ x ] -> return (x, ws)
| _ -> fatal (Invalid_variable x)
let notation =
let* wsnotation = token Notation in
let* tight, wstight, name, loc, wsname = tightness_and_name in
let* wscolon = token Colon in
let* pat, pattern = one_or_more (pattern_token </> pattern_var </> pattern_ellipsis) in
let pattern = pat :: pattern in
let Fix_pat (fixity, pattern), wsellipsis = fixity_of_pattern pattern tight in
let* wscoloneq = token Coloneq in
let* head, wshead = notation_head in
let* args = zero_or_more notation_var in
return
(Command.Notation
{
fixity;
wsnotation;
wstight;
wsellipsis;
name;
loc;
wsname;
wscolon;
pattern;
wscoloneq;
head;
wshead;
args;
})
let path =
ident
</> let* wsdot = token Dot in
return ([], wsdot)
let rec modifier () =
let* m =
step "" (fun state _ (tok, ws) ->
match tok with
| Ident [ "all" ] -> Some (`All ws, state)
| Ident [ "id" ] -> Some (`Id ws, state)
| Ident [ "only" ] -> Some (`Only ws, state)
| In -> Some (`In ws, state)
| Ident [ "none" ] -> Some (`None ws, state)
| Ident [ "except" ] -> Some (`Except ws, state)
| Ident [ "renaming" ] -> Some (`Renaming ws, state)
| Ident [ "seq" ] -> Some (`Seq ws, state)
| Ident [ "union" ] -> Some (`Union ws, state)
| _ -> None) in
match m with
| `All wsall -> return (All { wsall })
| `Id wsid -> return (Id { wsid })
| `Only wsonly ->
let* path, wspath = path in
return (Only { wsonly; path; wspath })
| `In wsin ->
let* path, wspath = path in
let* op = modifier () in
return (In { wsin; path; wspath; op })
| `None wsnone -> return (MNone { wsnone })
| `Except wsexcept ->
let* path, wspath = path in
return (Except { wsexcept; path; wspath })
| `Renaming wsrenaming ->
let* source, wssource = path in
let* target, wstarget = path in
return (Renaming { wsrenaming; source; wssource; target; wstarget })
| `Seq wsseq ->
let* wslparen = token LParen in
let* ops =
zero_or_more_fold_left Emp
(fun x y -> return (Snoc (x, y)))
(backtrack
(let* op = modifier () in
let* wssemi = token (Op ",") in
return (op, wssemi))
"") in
let* lastop = optional (modifier ()) in
let ops =
Bwd.fold_right
(fun x y -> x :: y)
ops
(Option.fold ~none:[] ~some:(fun x -> [ (x, []) ]) lastop) in
let* wsrparen = token RParen in
return (Seq { wsseq; wslparen; ops; wsrparen })
| `Union wsunion ->
let* wslparen = token LParen in
let* ops =
zero_or_more_fold_left Emp
(fun x y -> return (Snoc (x, y)))
(backtrack
(let* op = modifier () in
let* wssemi = token (Op ",") in
return (op, wssemi))
"") in
let* lastop = optional (modifier ()) in
let ops =
Bwd.fold_right
(fun x y -> x :: y)
ops
(Option.fold ~none:[] ~some:(fun x -> [ (x, []) ]) lastop) in
let* wsrparen = token RParen in
return (Union { wsunion; wslparen; ops; wsrparen })
let import =
let* wsimport, export =
(let* wsimport = token Import in
return (wsimport, false))
</> let* wsimport = token Export in
return (wsimport, true) in
let* origin, wsorigin =
step "" (fun state _ (tok, ws) ->
match tok with
| String file -> Some ((`File file, ws), state)
| Ident x -> Some ((`Path x, ws), state)
| Dot -> Some ((`Path [], ws), state)
| _ -> None) in
let* op =
optional
(backtrack
(let* wsbar = token (Op "|") in
let* m = modifier () in
return (wsbar, m))
"") in
return (Import { wsimport; export; origin; wsorigin; op })
let integer =
step "" (fun state _ (tok, ws) ->
match tok with
| Ident [ num ] -> Some ((int_of_string num, ws), state)
| _ -> None)
let solve =
let* wssolve = token Solve in
let* number, wsnumber = integer in
let* wscoloneq = token Coloneq in
let* tm = C.term [] in
return (Solve { wssolve; number; wsnumber; wscoloneq; tm })
let show =
let* wsshow = token Show in
let* what =
step "" (fun state _ (tok, ws) ->
match tok with
| Ident [ "hole" ] -> Some (`Hole ws, state)
| Ident [ "holes" ] -> Some (`Holes ws, state)
| _ -> None) in
let* what, wswhat =
match what with
| `Hole ws ->
let* number, wsnumber = integer in
return (`Hole (ws, number), wsnumber)
| `Holes ws -> return (`Holes, ws) in
return (Show { wsshow; what; wswhat })
let undo =
let* wsundo = token Undo in
let* count, wscount = integer in
return (Command.Undo { wsundo; count; wscount })
let section =
let* wssection = token Section in
let* prefix, wsprefix = ident in
let* wscoloneq = token Coloneq in
return (Command.Section { wssection; prefix; wsprefix; wscoloneq })
let endcmd =
let* wsend = token End in
return (Command.End { wsend })
let quit =
let* wsquit = token Quit in
return (Command.Quit wsquit)
let bof =
let* ws = C.bof in
return (Command.Bof ws)
let eof =
let* () = expect_end () in
return Command.Eof
let command () =
bof
</> axiom
</> def_and
</> echo
</> synth
</> notation
</> import
</> solve
</> show
</> undo
</> section
</> endcmd
</> quit
</> eof
let command_or_echo () =
command ()
</> let* tm = C.term [] in
return (Command.Echo { wsecho = []; tm; eval = true })
type open_source = Range.Data.t * [ `String of int * string | `File of In_channel.t ]
let start_parse ?(or_echo = false) source : C.Lex_and_parse.t * open_source =
let (env : Range.Data.t), run =
match source with
| `String src ->
( { source = `String src; length = Int64.of_int (String.length src.content) },
fun p ->
let n, p = C.Lex_and_parse.run_on_string_at 0 src.content p in
(`String (n, src.content), p) )
| `File name -> (
try
let ic = In_channel.open_text name in
( { source = `File name; length = In_channel.length ic },
fun p -> (`File ic, C.Lex_and_parse.run_on_channel ic p) )
with Sys_error _ -> fatal (No_such_file name)) in
Range.run ~env @@ fun () ->
let p =
C.Lex_and_parse.make Lexer.Parser.start
(C.Basic.make_partial () (if or_echo then command_or_echo () else command ())) in
let out, p = run p in
(C.ensure_success p, (env, out))
let restart_parse ?(or_echo = false) (p : C.Lex_and_parse.t) ((env, source) : open_source) :
C.Lex_and_parse.t * open_source =
let run =
match source with
| `String (n, content) ->
fun p ->
let n, p = C.Lex_and_parse.run_on_string_at n content p in
(`String (n, content), p)
| `File ic -> fun p -> (`File ic, C.Lex_and_parse.run_on_channel ic p) in
Range.run ~env @@ fun () ->
let p =
C.Lex_and_parse.make_next p
(C.Basic.make_partial () (if or_echo then command_or_echo () else command ())) in
let out, p = run p in
(C.ensure_success p, (env, out))
let final p = C.Lex_and_parse.final p
let has_consumed_end p = C.Lex_and_parse.has_consumed_end p
end
let parse_single (content : string) : Whitespace.t list * Command.t option =
let src : Asai.Range.source = `String { content; title = Some "interactive input" } in
let p, src = Parse.start_parse ~or_echo:true src in
match Parse.final p with
| Bof ws ->
let p, src = Parse.restart_parse ~or_echo:true p src in
let cmd = Parse.final p in
if cmd <> Eof then
let p, _ = Parse.restart_parse ~or_echo:true p src in
let eof = Parse.final p in
if eof = Eof then (ws, Some cmd) else Core.Reporter.fatal Too_many_commands
else (ws, None)
| _ -> Core.Reporter.fatal (Anomaly "interactive parse doesn't start with Bof")
let show_hole err = function
| Eternity.Find_number (m, { tm = `Undefined; termctx; ty; energy = _ }, { vars; _ }) ->
emit (Hole (Meta.name m, Termctx.PHole (vars, termctx, ty)))
| _ -> fatal err
let to_string : Command.t -> string = function
| Axiom _ -> "axiom"
| Def _ -> "def"
| Echo { eval = true; _ } -> "echo"
| Echo { eval = false; _ } -> "synth"
| Notation _ -> "notation"
| Import _ -> "import"
| Solve _ -> "solve"
| Show _ -> "show"
| Quit _ -> "quit"
| Undo _ -> "undo"
| Section _ -> "section"
| End _ -> "end"
| Bof _ -> "bof"
| Eof -> "eof"
(* Whether a command requires an interactive mode (i.e. not interactive mode and not ProofGeneral interaction). *)
let needs_interactive : Command.t -> bool = function
| Solve _ | Show _ | Undo _ -> true
| _ -> false
let allows_holes : Command.t -> (unit, string) Result.t = function
| Axiom _ | Def _ | Solve _ -> Ok ()
| cmd -> Error (to_string cmd)
(* Most execution of commands we can do here, but there are a couple things where we need to call out to the executable: noting when an effectual action like 'echo' is taken (for recording warnings in compiled files), and loading another file. So this function takes a couple of callbacks as arguments. *)
let execute : action_taken:(unit -> unit) -> get_file:(string -> Scope.trie) -> Command.t -> unit =
fun ~action_taken ~get_file cmd ->
if needs_interactive cmd && not (Core.Command.Mode.read ()).interactive then
fatal (Forbidden_interactive_command (to_string cmd));
Global.with_holes (allows_holes cmd) @@ fun () ->
match cmd with
| Axiom { name; loc; parameters; ty = Term ty; _ } ->
History.do_command @@ fun () ->
Scope.check_name name loc;
let const = Scope.define (Compunit.Current.read ()) ?loc name in
Reporter.try_with ~fatal:(fun d ->
Scope.modify_visible (Yuujinchou.Language.except name);
Scope.modify_export (Yuujinchou.Language.except name);
Reporter.fatal_diagnostic d)
@@ fun () ->
let (Processed_tel (params, ctx)) = process_tel Emp parameters in
Core.Command.execute (Axiom (const, params, process ctx ty))
| Def defs ->
History.do_command @@ fun () ->
let [ names; cdefs ] =
Mlist.pmap
(fun [ d ] ->
Scope.check_name d.name d.loc;
let c = Scope.define (Compunit.Current.read ()) ?loc:d.loc d.name in
[ d.name; (c, d) ])
[ defs ] (Cons (Cons Nil)) in
Reporter.try_with ~fatal:(fun d ->
List.iter
(fun c ->
Scope.modify_visible (Yuujinchou.Language.except c);
Scope.modify_export (Yuujinchou.Language.except c))
names;
Reporter.fatal_diagnostic d)
@@ fun () ->
let defs =
List.map
(function
| const, { parameters; ty; tm = Term tm; _ } -> (
let (Processed_tel (params, ctx)) = process_tel Emp parameters in
match ty with
| Some (Term ty) ->
( const,
Core.Command.Def_check { params; ty = process ctx ty; tm = process ctx tm } )
| None -> (
match process ctx tm with
| { value = Synth tm; loc } ->
(const, Def_synth { params; tm = { value = tm; loc } })
| _ -> fatal (Nonsynthesizing "body of def without specified type"))))
cdefs in
Core.Command.execute (Def defs)
| Echo { tm = Term tm; eval; _ } -> (
let rtm = process Emp tm in
action_taken ();
match rtm.value with
| Synth stm ->
Readback.Display.run ~env:true @@ fun () ->
let ctm, ety = Check.synth (Kinetic `Nolet) Ctx.empty { value = stm; loc = rtm.loc } in
let btm =
if eval then
let etm = Norm.eval_term (Emp D.zero) ctm in
Readback.readback_at Ctx.empty etm ety
else ctm in
let bty = Readback.readback_at Ctx.empty ety (Syntax.universe D.zero) in
let utm = unparse Names.empty btm Interval.entire Interval.entire in
let uty = unparse Names.empty bty Interval.entire Interval.entire in
let ppf = Format.std_formatter in
pp_open_vbox ppf 2;
pp_term `None ppf (Term utm);
pp_print_cut ppf ();
pp_tok ppf Colon;
pp_print_string ppf " ";
pp_term `None ppf (Term uty);
pp_close_box ppf ();
pp_print_newline ppf ();
pp_print_newline ppf ()
| _ -> fatal (Nonsynthesizing "argument of echo"))
| Notation { fixity; name; loc; pattern; head; args; _ } ->
History.do_command @@ fun () ->
let notation_name = "notations" :: name in
Scope.check_name notation_name loc;
let key =
match head with
| `Constr c -> `Constr (Constr.intern c, List.length args)
| `Constant c -> (
match Scope.lookup c with
| Some c -> `Constant c
| None -> fatal (Invalid_notation_head (String.concat "." c))) in
(* Find the "unbound" variables, if any, in the notation definition. *)
let rec unbounds :
type left right.
(string * Whitespace.t list) list ->
string list ->
(left, right) User.Pattern.t ->
(string * Whitespace.t list) list =
fun args seen pat ->
let check_var x =
if List.mem x seen then fatal (Duplicate_notation_variable x);
let found, rest = List.partition (fun (y, _) -> x = y) args in
match found with
| [ _ ] -> rest
| [] -> fatal (Unused_notation_variable x)
| _ -> fatal (Notation_variable_used_twice x) in
match pat with
| User.Pattern.Var ((x, _, _), pat) ->
let rest = check_var x in
unbounds rest (x :: seen) pat
| Op (_, pat) -> unbounds args seen pat
| Op_nil _ -> args
| Var_nil (_, (x, _, _)) -> check_var x in
(match unbounds args [] pattern with
| [] -> ()
| _ :: _ as unbound -> fatal (Unbound_variable_in_notation (List.map fst unbound)));
let user =
User { name = String.concat "." name; fixity; pattern; key; val_vars = List.map fst args }
in
let notn, shadow = Situation.Current.add_user user in
Scope.include_singleton (notation_name, ((`Notation (user, notn), loc), ()));
(if shadow then
let keyname =
match notn.key with
| `Constr (c, _) -> Constr.to_string c ^ "."
| `Constant c -> String.concat "." (Scope.name_of c) in
emit (Head_already_has_notation keyname));
emit (Notation_defined (String.concat "." name))
| Import { export; origin; op; _ } ->
History.do_command @@ fun () ->
let trie =
match origin with
| `File file ->
if FilePath.check_extension file "ny" then emit (Library_has_extension file);
let file = FilePath.add_extension file "ny" in
get_file file
| `Path path -> Trie.find_subtree path (Scope.get_visible ()) in
let trie =
match op with
| Some (_, op) -> Scope.Mod.modify (process_modifier op) trie
| None -> trie in
if export then Scope.include_subtree ([], trie) else Scope.import_subtree ([], trie);
Seq.iter
(fun (_, ((data, _), _)) ->
match data with
| `Notation (user, _) ->
let _ = Situation.Current.add_user user in
()
| _ -> ())
(Trie.to_seq (Trie.find_subtree [ "notations" ] trie))
| Solve { number; tm = Term tm; _ } -> (
(* Solve does NOT create a new history entry because it is NOT undoable. *)
let (Find_number
(m, { tm = metatm; termctx; ty; energy = _ }, { global; scope; status; vars })) =
Eternity.find_number number in
match metatm with
| `Undefined ->
History.run_with_scope ~init_visible:scope @@ fun () ->
Core.Command.execute
(Solve (global, status, termctx, process vars tm, ty, Eternity.solve m))
| `Defined _ | `Axiom ->
(* Yes, this is an anomaly and not a user error, because find_number should only be looking at the unsolved holes. *)
fatal (Anomaly "hole already defined"))
| Show { what; _ } -> (
action_taken ();
match what with
| `Hole (_, number) -> show_hole (No_such_hole number) (Eternity.find_number number)
| `Holes -> (
match Eternity.all_holes () with
| [] -> emit No_open_holes
| holes -> List.iter (show_hole (Anomaly "defined hole in undefined list")) holes))
| Undo { count; _ } ->
History.undo count;
emit (Commands_undone count)
| Section { prefix; _ } ->
History.do_command @@ fun () ->
Scope.start_section prefix;
emit (Section_opened prefix)
| End _ -> (
History.do_command @@ fun () ->
match Scope.end_section () with
| Some prefix -> emit (Section_closed prefix)
| None -> fatal No_such_section)
| Quit _ -> fatal (Quit None)
| Bof _ -> ()
| Eof -> fatal (Anomaly "EOF cannot be executed")
let tightness_of_fixity : type left tight right. (left, tight, right) fixity -> string option =
function
| Infix tight
| Infixl tight
| Infixr tight
| Prefix tight
| Prefixr tight
| Postfix tight
| Postfixl tight -> Some (No.to_string tight)
| Outfix -> None
let pp_parameter : formatter -> Parameter.t -> unit =
fun ppf { wslparen; names; wscolon; ty; wsrparen } ->
pp_tok ppf LParen;
pp_ws `None ppf wslparen;
List.iter
(fun (name, wsname) ->
pp_var ppf name;
pp_ws `Break ppf wsname)
names;
pp_tok ppf Colon;
pp_ws `Nobreak ppf wscolon;
pp_term `None ppf ty;
pp_tok ppf RParen;
pp_ws `Break ppf wsrparen
let rec pp_defs : formatter -> Token.t -> Whitespace.t list -> def list -> Whitespace.t list =
fun ppf tok ws defs ->
match defs with
| [] -> ws
| { wsdef; name; loc = _; wsname; parameters; wscolon; ty; wscoloneq; tm = Term tm } :: defs ->
pp_ws `None ppf ws;
pp_open_hvbox ppf 2;
pp_tok ppf tok;
pp_ws `Nobreak ppf wsdef;
pp_utf_8 ppf (String.concat "." name);
pp_ws `Break ppf wsname;
List.iter (pp_parameter ppf) parameters;
(match ty with
| Some ty ->
pp_tok ppf Colon;
pp_ws `Nobreak ppf wscolon;
pp_term `Break ppf ty
| None -> ());
pp_tok ppf Coloneq;
pp_ws `Nobreak ppf wscoloneq;
let tm, rest = split_ending_whitespace tm in
pp_term `None ppf (Term tm);
pp_close_box ppf ();
pp_defs ppf And rest defs
let pp_command : formatter -> t -> Whitespace.t list =
fun ppf cmd ->
match cmd with
| Axiom { wsaxiom; name; loc = _; wsname; parameters; wscolon; ty = Term ty } ->
pp_open_hvbox ppf 2;
pp_tok ppf Axiom;
pp_ws `Nobreak ppf wsaxiom;
pp_utf_8 ppf (String.concat "." name);
pp_ws `Break ppf wsname;
List.iter (pp_parameter ppf) parameters;
pp_tok ppf Colon;
pp_ws `Nobreak ppf wscolon;
let ty, rest = split_ending_whitespace ty in
pp_term `None ppf (Term ty);
pp_close_box ppf ();
rest
| Def defs -> pp_defs ppf Def [] defs
| Echo { wsecho; tm = Term tm; eval } ->
pp_open_hvbox ppf 2;
pp_tok ppf (if eval then Echo else Synth);
pp_ws `Nobreak ppf wsecho;
let tm, rest = split_ending_whitespace tm in
pp_term `None ppf (Term tm);
pp_close_box ppf ();
rest
| Notation
{
fixity;
wsnotation;
wstight;
wsellipsis;
name;
loc = _;
wsname;
wscolon;
pattern;
wscoloneq;
head;
wshead;
args;
} ->
pp_open_hvbox ppf 2;
pp_tok ppf Notation;
pp_ws `Nobreak ppf wsnotation;
(match tightness_of_fixity fixity with
| Some str -> pp_print_string ppf str
| None -> ());
pp_ws `Nobreak ppf wstight;
pp_utf_8 ppf (String.concat "." name);
pp_ws `Break ppf wsname;
pp_tok ppf Colon;
pp_ws `Nobreak ppf wscolon;
(match fixity with
| Infixl _ | Postfixl _ ->
pp_tok ppf Ellipsis;
pp_ws `Break ppf wsellipsis
| _ -> ());
User.pp_pattern ppf pattern;
(match fixity with
| Infixr _ | Prefixr _ ->
pp_tok ppf Ellipsis;
pp_ws `Break ppf wsellipsis
| _ -> ());
pp_tok ppf Coloneq;
pp_ws `Nobreak ppf wscoloneq;
(match head with
| `Constr c -> pp_constr ppf c
| `Constant c -> pp_utf_8 ppf (String.concat "." c));
let rest =
match split_last args with
| None ->
let wshead, rest = Whitespace.split wshead in
pp_ws `None ppf wshead;
rest
| Some (args, (last, wslast)) ->
List.iter
(fun (arg, wsarg) ->
pp_utf_8 ppf arg;
pp_ws `Break ppf wsarg)
args;
pp_utf_8 ppf last;
let wslast, rest = Whitespace.split wslast in
pp_ws `None ppf wslast;
rest in
pp_close_box ppf ();
rest
| Import { wsimport; export; origin; wsorigin; op } -> (
pp_open_hvbox ppf 2;
pp_tok ppf (if export then Export else Import);
pp_ws `Nobreak ppf wsimport;
(match origin with
| `File file ->
pp_print_string ppf "\"";
pp_print_string ppf file;
pp_print_string ppf "\""
| `Path [] -> pp_tok ppf Dot
| `Path path -> pp_utf_8 ppf (String.concat "." path));
match op with
| None ->
let ws, rest = Whitespace.split wsorigin in
pp_ws `None ppf ws;
pp_close_box ppf ();
rest
| Some (wsbar, op) ->
pp_ws `Break ppf wsorigin;
pp_tok ppf (Op "|");
pp_ws `Nobreak ppf wsbar;
let ws, rest = Whitespace.split (pp_modifier ppf op) in
pp_ws `None ppf ws;
pp_close_box ppf ();
rest)
| Solve { wssolve; number; wsnumber; wscoloneq; tm = Term tm } ->
pp_open_hvbox ppf 2;
pp_tok ppf Solve;
pp_ws `Nobreak ppf wssolve;
pp_print_int ppf number;
pp_ws `Break ppf wsnumber;
pp_tok ppf Coloneq;
pp_ws `Nobreak ppf wscoloneq;
let tm, rest = split_ending_whitespace tm in
pp_term `None ppf (Term tm);
pp_close_box ppf ();
rest
| Show { wsshow; what; wswhat } ->
pp_open_hvbox ppf 2;
pp_tok ppf Show;
pp_ws `Nobreak ppf wsshow;
(match what with
| `Hole (ws, number) ->
pp_print_string ppf "hole";
pp_ws `Nobreak ppf ws;
pp_print_int ppf number
| `Holes -> pp_print_string ppf "holes");
let ws, rest = Whitespace.split wswhat in
pp_ws `None ppf ws;
rest
| Undo { wsundo; count; wscount } ->
pp_tok ppf Undo;
pp_ws `Nobreak ppf wsundo;
pp_print_int ppf count;
let ws, rest = Whitespace.split wscount in
pp_ws `None ppf ws;
rest
| Section { wssection; prefix; wsprefix; wscoloneq } ->
pp_tok ppf Section;
pp_ws `Nobreak ppf wssection;
pp_utf_8 ppf (String.concat "." prefix);
pp_ws `Nobreak ppf wsprefix;
let ws, rest = Whitespace.split wscoloneq in
pp_tok ppf Coloneq;
pp_open_vbox ppf 2;
pp_ws `None ppf ws;
rest
| End { wsend } ->
pp_close_box ppf ();
pp_tok ppf End;
let ws, rest = Whitespace.split wsend in
pp_ws `None ppf ws;
rest
| Quit ws -> ws
| Bof ws -> ws
| Eof -> []