-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpSTLCa_Defs.v
1992 lines (1585 loc) · 69.7 KB
/
cpSTLCa_Defs.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
(* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% *)
(* Simply Typed Lambda Calculus with simple Concept Parameters
:: version a
Definitions
Definitions of STLC are based on
Sofware Foundations, v.4
Last Update: Mon, 5 Jun 2017
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% *)
(* ***************************************************************** *)
(** * cpSTLCa Syntax and Semantics Definition
(Simply Typed Lambda Calculus with simple Concept Parameters
:: version a) *)
(* ***************************************************************** *)
(* ***************************************************************** *)
Add LoadPath "../..".
Require Import ConceptParams.BasicPLDefs.Identifier.
Require Import ConceptParams.BasicPLDefs.Maps.
Require Import ConceptParams.BasicPLDefs.Relations.
Require Import ConceptParams.BasicPLDefs.Utils.
Require Import ConceptParams.AuxTactics.LibTactics.
Require Import ConceptParams.AuxTactics.BasicTactics.
Require Import ConceptParams.SetMapLib.List2Set.
Require Import ConceptParams.SetMapLib.ListPair2FMap.
Require Import ConceptParams.GenericModuleLib.SharedDataDefs.
Require Import ConceptParams.GenericModuleLib.SimpleModule.
Require Import ConceptParams.GenericModuleLib.SinglePassModule.
Require Import ConceptParams.GenericModuleLib.SinglePassImplModule.
Require Import Coq.Lists.List.
Import ListNotations.
Require Import Coq.Bool.Bool.
Require Import Coq.omega.Omega.
Require Import Coq.FSets.FMapInterface.
(** We will use list-2-set and list-pair-2-map machinery
** when dealing with modules. *)
(** [List2MSet] module for [id] type of identifiers. *)
Module IdLS' := MList2MSetAVL IdUOT.
Module IdLS := IdLS'.M.
Definition id_set := IdLS.id_set.
(** [ListPair2FMap] module for [id] type of identifiers. *)
Module IdLPM' := MListPair2FMapAVL IdUOTOrig.
Module IdLPM := IdLPM'.M.
Definition id_map := IdLPM.id_map.
Definition set_of_keys {X : Type} (m : id_map X) : id_set :=
IdLS.set_from_list (map fst (IdLPM.IdMap.elements m)).
(** Identifier Data Module *)
Module MId <: IdentifiersBase.
Module IdDT := IdLS'.M.IdDT.
Module IdSET := IdLS'.IdSetAVL.
Module IdLS := IdLS.
Module IdDT' := IdLPM'.M.IdDT.
Module IdMAP := IdLPM'.IdMapAVL.
Module IdLPM := IdLPM.
Definition id := id.
End MId.
(* ################################################################# *)
(** ** Syntax *)
(* ################################################################# *)
(** cpSTLCa — Symply Typed Lambda Calculus
with simple _concept parameters_.
Types are STLC types with the type [C # T],
where [C] is a concept name, [T] is a type.
Terms are STLC terms with the terms:
- [\c#C. t] (concept parametrization) where [c] is a concept parameter;
- [t # M] (model application) where [M] is a model.
- [c.f] (method invocation) where [f] is a field of the concept [c]
- [m.f] (method invocation) where [f] is a field of the model [m]
<<
CSec ::= Concept declarations
| <empty>
| CDef CSec
CDef ::= Concept definition
| concept Id NameDecls endc
MSec ::= Model declarations
| <empty>
| MDef MSec
MDef ::=
| model Id of Id NameDefs endm
NameDecls ::= List of name declarations
| <empty>
| NameDecl ; NameDecls
NameDefs ::= List of name definitions
| <empty>
| NameDef ; NameDefs
NameDecl ::= Name declaration
| Id : T
NameDef ::= Name definition
| Id = t
C metavariable means concept name (Id)
T ::= Types
| Nat
| Bool
| T -> T function
| C # T concept dependency
x metavariable means variable name (Id)
n metavariable means nat constant
c metavariable means concept parameter name (Id)
M metavariable means model name (Id)
t ::= Terms
| x variable
| \x:T.t function
| t t function application
| \c#C.t concept parameter
| t # M model application
| c.f concept element invocation
| true
| false
| if t then t else t
| n
| succ t
| pred t
| plus t t
| minus t t
| mult t t
| eqnat t t nat equality
| le t t nat less-than
| let x = t in t let binding
>>
_Program_ consists of concept and model declarations,
and a term.
<<
p ::=
| CSec MSec t
>>
*)
(* ----------------------------------------------------------------- *)
(** **** Types *)
(* ----------------------------------------------------------------- *)
Inductive ty : Type :=
| TBool : ty
| TNat : ty
| TArrow : ty -> ty -> ty (* T1 -> T2 *)
| TConceptPrm : id -> ty -> ty (* C # T *)
.
(* ----------------------------------------------------------------- *)
(** **** Terms *)
(* ----------------------------------------------------------------- *)
Inductive tm : Type :=
| tvar : id -> tm (* x *)
| tapp : tm -> tm -> tm (* t1 t2 *)
| tabs : id -> ty -> tm -> tm (* \x:T11.t12 *)
| tmapp : tm -> id -> tm (* t1 # M *)
| tcabs : id -> id -> tm -> tm (* \c#C.t1 *)
| tcinvk : id -> id -> tm (* c.f *)
| ttrue : tm
| tfalse : tm
| tif : tm -> tm -> tm -> tm (* if t1 then t2 else t3 *)
| tnat : nat -> tm (* n *)
| tsucc : tm -> tm (* succ t1 *)
| tpred : tm -> tm (* pred t1 *)
| tplus : tm -> tm -> tm (* plus t1 t2 *)
| tminus : tm -> tm -> tm (* minus t1 t2 *)
| tmult : tm -> tm -> tm (* mult t1 t2 *)
| teqnat : tm -> tm -> tm (* eqnat t1 t2 *)
| tlenat : tm -> tm -> tm (* lenat t1 t2 *)
| tlet : id -> tm -> tm -> tm (* let x = t1 in t2 *)
.
(* ----------------------------------------------------------------- *)
(** **** Concepts *)
(* ----------------------------------------------------------------- *)
(** Name declaration *)
Inductive namedecl : Type :=
| nm_decl : id -> ty -> namedecl (* f : T *)
.
(** Auxiliary function to transform name declaration into pair *)
Definition namedecl_to_pair (nmdecl : namedecl) : (id * ty) :=
match nmdecl with
nm_decl fname ftype => (fname, ftype)
end.
(** List of name declarations *)
Definition namedecl_list : Type := list namedecl.
Hint Unfold namedecl_list.
(** Concept definition *)
Inductive conceptdef : Type :=
| cpt_def : id -> namedecl_list -> conceptdef (* concept Id NameDefs endc *)
.
(** Auxiliary function to transform concept definition into pair *)
Definition conceptdef_to_pair (C : conceptdef) : (id * namedecl_list) :=
match C with
cpt_def Cname Cbody => (Cname, Cbody)
end.
Definition conceptdef__get_name (C : conceptdef) : id :=
match C with cpt_def nm nmdecls => nm end.
(** Concept declarations Section *)
Definition conceptsec : Type := list conceptdef.
Hint Unfold conceptsec.
(* ----------------------------------------------------------------- *)
(** **** Models *)
(* ----------------------------------------------------------------- *)
(** Name definition *)
Inductive namedef : Type :=
| nm_def : id -> tm -> namedef (* f = t *)
.
(** Auxiliary function to transform name defintion into pair *)
Definition namedef_to_pair (nmdef : namedef) : (id * tm) :=
match nmdef with
nm_def fname fdef => (fname, fdef)
end.
(** List of name definitions *)
Definition namedef_list : Type := list namedef.
Hint Unfold namedef_list.
(** Model definition *)
Inductive modeldef : Type :=
(* model Id of Id NameDefs endm *)
| mdl_def : id -> id -> namedef_list -> modeldef
.
Definition modeldef__get_name (M : modeldef) : id :=
match M with mdl_def nm C nmdefs => nm end.
(** Model declarations Section *)
Definition modelsec : Type := list modeldef.
Hint Unfold modelsec.
(* ----------------------------------------------------------------- *)
(** **** Program *)
(* ----------------------------------------------------------------- *)
Inductive program : Type :=
| tprog : conceptsec -> modelsec -> tm -> program
.
(** There are examples of concepts and programs in [cpSTLCa_Examples.v]. *)
(* ################################################################# *)
(** ** Typing *)
(* ################################################################# *)
(** To typecheck terms with concept parameters, we need
information about concepts and models.
So we need a kind of a _symbol table_. It is defined later.
We start with defining types of concepts and models.
*)
(** [tycontext] is a typing context: a map from ids to types. *)
Definition tycontext := partial_map ty.
Hint Unfold tycontext.
(** Concept type [cty] contains information about members' types.
To check welldefinedness of a concept, it is enough to use
[tycontext] (functional map from ids to types) to store
information about names and types of concept members.
But we also need to check welldefinedness of models. To do so,
we have to check that a model defines all concept members.
Thus, there must be a way to extract information about all
concept members from [cty].
Therefore, we will use AVL Map from ids to types to express
information about concept members.
*)
(** AVL map from [id] to [ty]. *)
Definition id_ty_map := IdLPM.id_map ty.
Hint Unfold id_ty_map.
(** Concept type (AVL-map from ids into types) *)
Inductive cty : Type := CTdef : id_ty_map -> cty.
(** Lookup of types in a map *)
Definition find_ty := @IdLPM.IdMap.find ty.
Hint Unfold find_ty.
(** Models are always defined for some concepts. Therefore,
a list of members and their types must be the same as in
the corresponding concept.
For simplicity, we do not allow any extra members in the model.
The only thing we need to know about a model to typecheck
model application, is its concept.
But to implement terms reduction, we have to provide
information about members' implementation.
Thus, model type [mty] will contain both concept name
and a map from ids to terms. *)
(** AVL map from [id] to [tm]. *)
Definition id_tm_map := IdLPM.id_map tm.
Hint Unfold id_tm_map.
(** Model type *)
Inductive mty : Type := MTdef : id -> id_tm_map -> mty.
(** Lookup of terms in a map *)
Definition find_tm := @IdLPM.IdMap.find tm.
Hint Unfold find_tm.
(** For further checks we need a _symbol table_ to store
information about concepts and models. We can either
store both in one table, or have separate tables
for concepts and models.
For simplicity, we choose the second option. *)
(** Concept symbol table is a map from concept names
to concept types [Ci -> CTi]. *)
Definition cptcontext : Type := IdLPM.id_map cty.
Definition cstempty : cptcontext := IdLPM.IdMap.empty cty.
(** Model symbol table is a map from model names
to model types [Mi -> MTi]. *)
Definition mdlcontext : Type := IdLPM.id_map mty.
Definition mstempty : mdlcontext := @IdLPM.IdMap.empty mty.
(* ================================================================= *)
(** *** Checking Types Validity *)
(* ================================================================= *)
(* ----------------------------------------------------------------- *)
(** **** Checking Concept Definitions *)
(* ----------------------------------------------------------------- *)
(** Concept definition is Ok if names of concept elements are
distinct, and types of elements are valid.
The only problem in types is with concept dependency [C # T]:
if C is undefined concept name, type is not valid.
So to check types validity, we need symbol table already.
*)
(** Now let's define a property "type is valid".
This property must be checked againts concrete symbol table.
*)
Definition concept_defined (st : cptcontext) (nm : id) : Prop :=
IdLPM.IdMap.find nm st <> None.
Inductive type_valid (st : cptcontext) : ty -> Prop :=
| type_valid_nat : type_valid st TNat
| type_valid_bool : type_valid st TBool
| type_valid_arrow : forall T1 T2,
type_valid st T1 ->
type_valid st T2 ->
type_valid st (TArrow T1 T2)
| type_valid_cpt : forall C T,
concept_defined st C ->
type_valid st T ->
type_valid st (TConceptPrm C T)
.
Hint Constructors type_valid.
(* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! *)
(** This part is using GenericModulesLib *)
(* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! *)
Module IdModuleBase <: ModuleBase.
Module MId := MId.
Definition id := MId.id.
Definition id_set := MId.IdLS.id_set.
Definition id_map := MId.IdLPM.id_map.
End IdModuleBase.
(** Here we are going to use SimpleModule generic module
to check concept definitions. *)
Module MCptMem_DataC <: DataC.
(** Type of Data *)
Definition t := ty.
(** Type of Context *)
Definition ctx := cptcontext.
End MCptMem_DataC.
Module MCptMem_DataCOkDef <: DataCOkDef MCptMem_DataC.
Definition is_ok := type_valid.
End MCptMem_DataCOkDef.
Module MCptMem_SimpleMBase <: SimpleModuleBase.
Include IdModuleBase.
Module MD := MCptMem_DataC.
End MCptMem_SimpleMBase.
(** SimpleModule definitions for checking concept members. *)
Module MCptMem_Defs := SimpleModuleDefs MCptMem_SimpleMBase MCptMem_DataCOkDef.
(** Now we are ready to define a property "concept is well defined" *)
Definition concept_welldefined (st : cptcontext) (C : conceptdef) : Prop :=
match C with
cpt_def cname cbody =>
let pnds := map namedecl_to_pair cbody in
MCptMem_Defs.module_ok st pnds
end.
Hint Unfold concept_welldefined.
(** We have a symbol table for concepts, concept types, but
we have not defined yet a relation on concept definitions and
concept types. *)
Definition concept_has_type (cst : cptcontext) (C : conceptdef) (CT : cty) : Prop :=
(** concept def must be well-defined *)
concept_welldefined cst C
/\ match CT with CTdef cnmtys =>
match C with cpt_def cname cbody =>
let pnds := map namedecl_to_pair cbody in
(** and the map [cnmtys] has to be equal to the AST [cbody] *)
IdLPM.eq_list_map pnds cnmtys
end end.
(** Concept section (list of concept definitions) must also be well-formed.
As further concept definitions can refer to previously defined ones,
we need SinglePass Module Machinery.
*)
Module MCptDef_DataLC <: DataLC.
(** One concept definition is a member in this case. *)
Definition t := conceptdef.
(** We have no global context, only local. *)
Definition ctx := unit.
(** Local context contains information about previously defined
concepts. *)
Definition ctxloc := cptcontext.
End MCptDef_DataLC.
Module MCptDef_DataLCOkDef <: DataLCOkDef MCptDef_DataLC.
Definition is_ok (c : unit) (cl : cptcontext) (cpt : conceptdef) : Prop
:= concept_welldefined cl cpt.
End MCptDef_DataLCOkDef.
Module MCptDef_SinglePassMBase <: SinglePassModuleBase.
Include IdModuleBase.
Module MD := MCptDef_DataLC.
(** Initial local context *)
Definition ctxl_init : cptcontext := cstempty.
(** Update local context *)
Definition upd_ctxloc (cl : cptcontext) (c : unit)
(nm : id) (cpt : conceptdef) : cptcontext :=
match cpt with
cpt_def Cname Cbody =>
let nmtys := map namedecl_to_pair Cbody in
(* convert declarations into finite map,
and add this map into the context *)
IdLPM.IdMap.add Cname (CTdef (IdLPM.map_from_list nmtys)) cl
end.
End MCptDef_SinglePassMBase.
Module MCptDef_SinglePassMDefs :=
SinglePassModuleDefs MCptDef_SinglePassMBase MCptDef_DataLCOkDef.
Definition conceptdef_pair_with_id (C : conceptdef) : id * conceptdef :=
match C with cpt_def Cname _ => (Cname, C) end.
(** What it means for a concept section to be well-defined. *)
Definition conceptsec_welldefined (cpts : conceptsec) : Prop :=
let pcpts := map conceptdef_pair_with_id cpts in
MCptDef_SinglePassMDefs.module_ok tt pcpts.
Definition namedecl_list_to_cty (decls : namedecl_list) : cty :=
let nmtys := map namedecl_to_pair decls in
CTdef (IdLPM.map_from_list nmtys).
Definition conceptdef_to_pair_id_cty (C : conceptdef) : id * cty :=
match C with
cpt_def Cname Cbody => (Cname, namedecl_list_to_cty Cbody)
end.
(** What it means for a concept context to be well-defined.
** [cst] is well-defined if exists a well-defined AST
** equal to the concept context.*)
Definition cptcontext_welldefined (cst : cptcontext) : Prop :=
exists (cpts : conceptsec),
conceptsec_welldefined cpts
/\ let pctys := map conceptdef_to_pair_id_cty cpts in
IdLPM.IdMap.Equal cst (IdLPM.map_from_list pctys).
(* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! *)
(** At this point we cannot do more on contexts. To check models,
we have to be able to typecheck terms (function definitions).
But terms may conist of model applications.
*)
(* ================================================================= *)
(** *** Typing of Terms *)
(* ================================================================= *)
(** To typecheck terms we need:
- context [Gamma], which (in contrast to STLC) contains not only
variables, but also concept variables;
- concept symbol table [CTable];
- model symbol table [MTable].
*)
(** Informal typing rules are listed below.
We can read the five-place relation
[CTable * MTable ; Gamma |- t \in T] as:
"to the term [t] we can assign the type [T],
if types of free variables of [t] are specified in [Gamma],
free concept variables of [t] are specified in [Gamma],
context types are specified in [CTable],
model types are specified in [MTable]."
[dom( * )] is a domain of a map (map keys -- ids),
[range( * )] is a range of a map (map values -- types).
Gamma \has x:T
-------------------------------- (T_Var)
CTable * MTable ; Gamma |- x : T
CTable * MTable ; Gamma |- t1 : T11->T12
CTable * MTable ; Gamma |- t2 : T11
---------------------------------------- (T_App)
CTable * MTable ; Gamma |- t1 t2 : T12
CTable * MTable ; (Gamma , x:T11) |- t12 : T12
------------------------------------------------ (T_Abs)
CTable * MTable ; Gamma |- \x:T11.t12 : T11->T12
- MTable contains info about model M
- Model M implements concept C
(MTable(M) = ... of C ...)
M \in dom(MTable)
MTable(M) = ... of C ...
CTable * MTable ; Gamma |- t1 : (C # T1)
---------------------------------------- (T_MApp)
CTable * MTable ; Gamma |- (t1 # M) : T1
C \in dom(CTable)
CTable * MTable ; (Gamma , c#C) |- t1 : T1
-------------------------------------------------- (T_CAbs)
CTable * MTable ; Gamma |- \c#C.t1 : (C # T1)
- CTable contains info about concept C;
- C contains member f and its type is TF
(CTable(C) = ... f : TF ... )
Gamma \has c#C
C \in dom(CTable)
CTable(C) = ... f : TF ...
----------------------------------- (T_CInvk)
CTable * MTable ; Gamma |- c.f : TF
M#C \notin Gamma
M \in dom(MTable)
MTable(M) = ... of C ...
C \in dom(CTable)
CTable(C) = ... f : TF ...
----------------------------------- (T_MInvk)
CTable * MTable ; Gamma |- M.f : TF
------------------------------------ (T_True)
CTable * MTable ; Gamma |- true : Bool
------------------------------------ (T_False)
CTable * MTable ; Gamma |- false : Bool
CTable * MTable ; Gamma |- t1 : Bool
CTable * MTable ; Gamma |- t2 : T
CTable * MTable ; Gamma |- t3 : T
--------------------------------------------------- (T_If)
CTable * MTable ; Gamma |- if t1 then t2 else t3 : T
------------------------------------- (T_Nat)
CTable * MTable ; Gamma |- tnat n : Nat
CTable * MTable ; Gamma |- t1 : Nat
------------------------------------------ (T_Succ)
CTable * MTable ; Gamma |- succ t1 : Nat
CTable * MTable ; Gamma |- t1 : Nat
------------------------------------------ (T_Pred)
CTable * MTable ; Gamma |- pred t1 : Nat
CTable * MTable ; Gamma |- t1 : Nat
CTable * MTable ; Gamma |- t2 : Nat
---------------------------------------- (T_Plus)
CTable * MTable ; Gamma |- t1 + t2 : Nat
CTable * MTable ; Gamma |- t1 : Nat
CTable * MTable ; Gamma |- t2 : Nat
---------------------------------------- (T_Minus)
CTable * MTable ; Gamma |- t1 - t2 : Nat
CTable * MTable ; Gamma |- t1 : Nat
CTable * MTable ; Gamma |- t2 : Nat
--------------------------------------- (T_Mult)
CTable * MTable ; Gamma |- t1 * t2 : Nat
CTable * MTable ; Gamma |- t1 : Nat
CTable * MTable ; Gamma |- t2 : Nat
------------------------------------------ (T_EqNat)
CTable * MTable ; Gamma |- t1 = t2 : Bool
CTable * MTable ; Gamma |- t1 : Nat
CTable * MTable ; Gamma |- t2 : Nat
------------------------------------------ (T_LeNat)
CTable * MTable ; Gamma |- t1 <= t2 : Bool
CTable * MTable ; Gamma |- t1 : T1
CTable * MTable ; (Gamma , x:T1) |- t2 : T2
---------------------------------------------- (T_Let)
CTable * MTable ; Gamma |- let x=t1 in t2 : T2
*)
(** In SLTC Gamma consists of only variable types,
but now it can also have information about concept parameters.
*)
Inductive ctxvarty : Type :=
(* type of term variable [x : T] -- normal type *)
| tmtype : ty -> ctxvarty
(* type of concept parameter [c # C] -- concept name *)
| cpttype : id -> ctxvarty
.
Definition context : Type := partial_map ctxvarty.
Definition ctxempty : context := @empty ctxvarty.
(** Aux function for typing relation *)
Definition concept_fun_member (CTable : cptcontext)
(C : id) (f : id) (TF : ty) : Prop :=
match IdLPM.IdMap.find C CTable with
| None => False
| Some (CTdef fundefs) => find_ty f fundefs = Some TF
end.
Reserved Notation "CTable '$' MTable ';' Gamma '|-' t '\in' T" (at level 50).
(** Here is our typing relation for cpSTLCa terms. *)
Inductive has_type : cptcontext -> mdlcontext -> context -> tm -> ty -> Prop :=
| T_Var : forall CTable MTable Gamma x T,
Gamma x = Some (tmtype T) ->
CTable $ MTable ; Gamma |- tvar x \in T
| T_App : forall CTable MTable Gamma t1 t2 T11 T12,
CTable $ MTable ; Gamma |- t1 \in (TArrow T11 T12) ->
CTable $ MTable ; Gamma |- t2 \in T11 ->
CTable $ MTable ; Gamma |- tapp t1 t2 \in T12
| T_Abs : forall CTable MTable Gamma x t12 T11 T12,
CTable $ MTable ; (update Gamma x (tmtype T11)) |- t12 \in T12 ->
CTable $ MTable ; Gamma |- tabs x T11 t12 \in (TArrow T11 T12)
| T_MApp : forall CTable MTable Gamma t1 M C Mbody T1,
IdLPM.IdMap.find M MTable = Some (MTdef C Mbody) ->
CTable $ MTable ; Gamma |- t1 \in TConceptPrm C T1 ->
CTable $ MTable ; Gamma |- tmapp t1 M \in T1
| T_CAbs : forall CTable MTable Gamma c t1 C Cbody T1,
IdLPM.IdMap.find C CTable = Some (CTdef Cbody) ->
CTable $ MTable ; (update Gamma c (cpttype C)) |- t1 \in T1 ->
CTable $ MTable ; Gamma |- tcabs c C t1 \in TConceptPrm C T1
| T_CInvk : forall CTable MTable Gamma c f C TF,
Gamma c = Some (cpttype C) ->
concept_fun_member CTable C f TF ->
CTable $ MTable ; Gamma |- tcinvk c f \in TF
| T_MInvk : forall CTable MTable Gamma M C Mbody f TF,
(~ exists MC, Gamma M = Some (cpttype MC)) ->
IdLPM.IdMap.find M MTable = Some (MTdef C Mbody) ->
concept_fun_member CTable C f TF ->
CTable $ MTable ; Gamma |- tcinvk M f \in TF
| T_True : forall CTable MTable Gamma,
CTable $ MTable ; Gamma |- ttrue \in TBool
| T_False : forall CTable MTable Gamma,
CTable $ MTable ; Gamma |- tfalse \in TBool
| T_If : forall CTable MTable Gamma t1 t2 t3 T,
CTable $ MTable ; Gamma |- t1 \in TBool ->
CTable $ MTable ; Gamma |- t2 \in T ->
CTable $ MTable ; Gamma |- t3 \in T ->
CTable $ MTable ; Gamma |- tif t1 t2 t3 \in T
| T_Nat : forall CTable MTable Gamma n,
CTable $ MTable ; Gamma |- tnat n \in TNat
| T_Succ : forall CTable MTable Gamma t1,
CTable $ MTable ; Gamma |- t1 \in TNat ->
CTable $ MTable ; Gamma |- tsucc t1 \in TNat
| T_Pred : forall CTable MTable Gamma t1,
CTable $ MTable ; Gamma |- t1 \in TNat ->
CTable $ MTable ; Gamma |- tpred t1 \in TNat
| T_Plus : forall CTable MTable Gamma t1 t2,
CTable $ MTable ; Gamma |- t1 \in TNat ->
CTable $ MTable ; Gamma |- t2 \in TNat ->
CTable $ MTable ; Gamma |- tplus t1 t2 \in TNat
| T_Minus : forall CTable MTable Gamma t1 t2,
CTable $ MTable ; Gamma |- t1 \in TNat ->
CTable $ MTable ; Gamma |- t2 \in TNat ->
CTable $ MTable ; Gamma |- tminus t1 t2 \in TNat
| T_Mult : forall CTable MTable Gamma t1 t2,
CTable $ MTable ; Gamma |- t1 \in TNat ->
CTable $ MTable ; Gamma |- t2 \in TNat ->
CTable $ MTable ; Gamma |- tmult t1 t2 \in TNat
| T_EqNat : forall CTable MTable Gamma t1 t2,
CTable $ MTable ; Gamma |- t1 \in TNat ->
CTable $ MTable ; Gamma |- t2 \in TNat ->
CTable $ MTable ; Gamma |- teqnat t1 t2 \in TBool
| T_LeNat : forall CTable MTable Gamma t1 t2,
CTable $ MTable ; Gamma |- t1 \in TNat ->
CTable $ MTable ; Gamma |- t2 \in TNat ->
CTable $ MTable ; Gamma |- tlenat t1 t2 \in TBool
| T_Let : forall CTable MTable Gamma x t1 t2 T1 T2,
CTable $ MTable ; Gamma |- t1 \in T1 ->
CTable $ MTable ; (update Gamma x (tmtype T1)) |- t2 \in T2 ->
CTable $ MTable ; Gamma |- tlet x t1 t2 \in T2
where "CTable '$' MTable ';' Gamma '|-' t '\in' T"
:= (has_type CTable MTable Gamma t T) : stlca_scope.
Hint Constructors has_type.
(* ----------------------------------------------------------------- *)
(** **** Checking Model Definitions *)
(* ----------------------------------------------------------------- *)
(** Model definition is Ok if:
- concept name is defined;
- all concept members are defined in a model;
- model member types coincide with concept member types.
*)
Definition model_defined (st : mdlcontext) (nm : id) : Prop :=
IdLPM.IdMap.find nm st <> None.
(** [model_member_valid] is a proposition stating that the given
model member [nd] (name definition, [f := t]) is valid against
the concept [cpt] (name declarations, [f : T])
and previously defined members of the model.
*)
(** UPD
As we found out when trying to prove soundness,
bound variables in terms should not have names
that interfere with model names.
As we check model members before we "know" all the models,
there is not enough information about models names
in the context yet.
So we have to:
* provide information on all forbidden (model) names;
* check that bound variables do not use these names.
*)
(** Returns set of names of all bound concept variables used in the term. *)
Fixpoint bound_cvars (t : tm) : id_set :=
match t with
(* BCV(x) = {} *)
| tvar x => IdLS.IdSet.empty
(* BCV(t1 t2) = BCV(t1) \union BCV(t2) *)
| tapp t1 t2 => IdLS.IdSet.union (bound_cvars t1) (bound_cvars t2)
(* BCV(\x:T.t) = BCV(t) *)
| tabs x T t => bound_cvars t
(* BCV(t # M) = BCV(t) *)
| tmapp t M => bound_cvars t
(* BCV(\c#C.t) = BCV(t) \union {c} *)
| tcabs c C t => IdLS.IdSet.add c (bound_cvars t)
(* BCV(c.f) = {} *)
| tcinvk c f => IdLS.IdSet.empty
(* BCV(true) = {} *)
| ttrue => IdLS.IdSet.empty
(* BCV(false) = {} *)
| tfalse => IdLS.IdSet.empty
(* BCV(if t1 then t2 else t3) = BCV(t1) \union BCV(t2) \union BCV(t3) *)
| tif t1 t2 t3 => IdLS.IdSet.union
(IdLS.IdSet.union (bound_cvars t1) (bound_cvars t2))
(bound_cvars t3)
(* BCV(n) = {} *)
| tnat n => IdLS.IdSet.empty
(* BCV(succ t) = BCV(t) *)
| tsucc t => bound_cvars t
(* BCV(pred t) = BCV(t) *)
| tpred t => bound_cvars t
(* BCV(plus t1 t2) = BCV(t1) \union BCV(t2) *)
| tplus t1 t2 => IdLS.IdSet.union (bound_cvars t1) (bound_cvars t2)
(* BCV(minus t1 t2) = BCV(t1) \union BCV(t2) *)
| tminus t1 t2 => IdLS.IdSet.union (bound_cvars t1) (bound_cvars t2)
(* BCV(mult t1 t2) = BCV(t1) \union BCV(t2) *)
| tmult t1 t2 => IdLS.IdSet.union (bound_cvars t1) (bound_cvars t2)
(* BCV(eqnat t1 t2) = BCV(t1) \union BCV(t2) *)
| teqnat t1 t2 => IdLS.IdSet.union (bound_cvars t1) (bound_cvars t2)
(* BCV(lenat t1 t2) = BCV(t1) \union BCV(t2) *)
| tlenat t1 t2 => IdLS.IdSet.union (bound_cvars t1) (bound_cvars t2)
(* BCV(let x=t1 in t2) = BCV(t1) \union BCV(t2) \union {x} *)
| tlet x t1 t2 => IdLS.IdSet.union (bound_cvars t1) (bound_cvars t2)
end.
(*
(** Returns set of names of all bound variables used in the term. *)
Fixpoint bound_vars (t : tm) : id_set :=
match t with
(* BV(x) = {} *)
| tvar x => IdLS.IdSet.empty
(* BV(t1 t2) = BV(t1) \union BV(t2) *)
| tapp t1 t2 => IdLS.IdSet.union (bound_vars t1) (bound_vars t2)
(* BV(\x:T.t) = BV(t) \union {x} *)
| tabs x T t => IdLS.IdSet.union (IdLS.IdSet.singleton x) (bound_vars t)
(* BV(t # M) = BV(t) *)
| tmapp t M => bound_vars t
(* BV(\c#C.t) = BV(t) \union {c} *)
| tcabs c C t => IdLS.IdSet.union (IdLS.IdSet.singleton c) (bound_vars t)
(* BV(c.f) = {} *)
| tcinvk c f => IdLS.IdSet.empty
(* BV(true) = {} *)
| ttrue => IdLS.IdSet.empty
(* BV(false) = {} *)
| tfalse => IdLS.IdSet.empty
(* BV(if t1 then t2 else t3) = BV(t1) \union BV(t2) \union BV(t3) *)
| tif t1 t2 t3 => IdLS.IdSet.union
(IdLS.IdSet.union (bound_vars t1) (bound_vars t2))
(bound_vars t3)
(* BV(n) = {} *)
| tnat n => IdLS.IdSet.empty
(* BV(succ t) = BV(t) *)
| tsucc t => bound_vars t
(* BV(pred t) = BV(t) *)
| tpred t => bound_vars t
(* BV(plus t1 t2) = BV(t1) \union BV(t2) *)
| tplus t1 t2 => IdLS.IdSet.union (bound_vars t1) (bound_vars t2)
(* BV(minus t1 t2) = BV(t1) \union BV(t2) *)
| tminus t1 t2 => IdLS.IdSet.union (bound_vars t1) (bound_vars t2)
(* BV(mult t1 t2) = BV(t1) \union BV(t2) *)
| tmult t1 t2 => IdLS.IdSet.union (bound_vars t1) (bound_vars t2)
(* BV(eqnat t1 t2) = BV(t1) \union BV(t2) *)
| teqnat t1 t2 => IdLS.IdSet.union (bound_vars t1) (bound_vars t2)
(* BV(lenat t1 t2) = BV(t1) \union BV(t2) *)
| tlenat t1 t2 => IdLS.IdSet.union (bound_vars t1) (bound_vars t2)
(* BV(let x=t1 in t2) = BV(t1) \union BV(t2) \union {x} *)
| tlet x t1 t2 => IdLS.IdSet.union
(IdLS.IdSet.singleton x)
(IdLS.IdSet.union (bound_vars t1) (bound_vars t2))
end.
*)
(** There are no bound concept variables in [t]
** with names from [badnames]. *)
Definition no_bound_cvar_names_in_term
(badnames : id_set) (t : tm) : Prop :=
forall (x : id),
IdLS.IdSet.In x badnames ->
~ IdLS.IdSet.In x (bound_cvars t).
(*
(** There are no bound variables in [t] with names from [badnames]. *)
Definition no_bound_var_names_in_term
(badnames : id_set) (t : tm) : Prop :=
forall (x : id),
IdLS.IdSet.In x badnames ->
~ IdLS.IdSet.In x (bound_vars t).
*)
Definition model_member_valid (CTbl : cptcontext) (MTbl : mdlcontext)
(mdlnames : id_set) (cpt : id_ty_map) (Gamma : context)
(nm : id) (t : tm) : Prop :=
exists (T : ty),
(** there is [nm : T] in a concept *)
find_ty nm cpt = Some T
(** no model names from [mdlnames] are used for names
of bound concept vars *)
/\ no_bound_cvar_names_in_term mdlnames t
(** and [T] is a type of [t], that is
[CTbl $ MTbl ; Gamma |- t : T] *)
/\ has_type CTbl MTbl Gamma t T.
(* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! *)
(** This part is using GenericModulesLib *)
(* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! *)
(** Here we are going to use [SinglePassImplModule] generic module. *)
Module MMdlMem_DataLCI <: DataLCI.
(** Type of Data -- term *)
Definition t := tm.
(** Type of Context needed for checking WD of terms.
We need both concept and model table,
and also forbidden model names. *)
Definition ctx := (cptcontext * mdlcontext * id_set) % type.
(** Type of Local Context which is needed for checking WD of terms
(here we need types of previously defined members). *)
Definition ctxloc := context.
(** Type of Concept representation in symbol table *)
Definition intrfs := id_ty_map.
End MMdlMem_DataLCI.
Module MMdlMem_DataLCIOkDef <: DataLCIOkDef MId MMdlMem_DataLCI.
(* Element [t] must be ok with respect
to global [ctx] and local [ctxloc] contexts. *)
Definition is_ok (cm_nms : cptcontext * mdlcontext * id_set)
(cpt : id_ty_map) (Gamma : context)
(nm : id) (t : tm) : Prop :=
let (cm, mdlnms) := cm_nms in
let (c, m) := cm in
model_member_valid c m mdlnms cpt Gamma nm t.
End MMdlMem_DataLCIOkDef.
Module MMdlMem_SinglePassImplMBase <: SinglePassImplModuleBase.
Include IdModuleBase.
Module MD := MMdlMem_DataLCI.
(** Initial local context (Gamma) *)
Definition ctxl_init := ctxempty.
(** Update local context *)
Definition upd_ctxloc (Gamma : context)
(cm_nms : cptcontext * mdlcontext * id_set)
(cpt : id_ty_map) (nm : id) (t : tm) : context :=
match find_ty nm cpt with
| Some tp => update Gamma nm (tmtype tp)
| None => Gamma
end.
(** Members which have to be defined by an interface *)
Definition members_to_define (cpt : id_ty_map) : list id
:= map fst (IdLPM.IdMap.elements cpt).
End MMdlMem_SinglePassImplMBase.
Module MMdlMem_SinglePassImplMDefs :=
SinglePassImplModuleDefs MMdlMem_SinglePassImplMBase MMdlMem_DataLCIOkDef.