@@ -48,7 +48,7 @@ public static void PopulateDeclarations(
48
48
public override object VisitEventDecl ( PParser . EventDeclContext context )
49
49
{
50
50
// EVENT name=Iden
51
- var pEvent = ( Event ) nodesToDeclarations . Get ( context ) ;
51
+ var pEvent = ( Event ) nodesToDeclarations . Get ( context ) ;
52
52
53
53
// cardinality?
54
54
var hasAssume = context . cardinality ( ) ? . ASSUME ( ) != null ;
@@ -633,63 +633,21 @@ public override object VisitPureDecl(PParser.PureDeclContext context)
633
633
// (COLON type)?
634
634
pure . Signature . ReturnType = ResolveType ( context . type ( ) ) ;
635
635
636
- if ( context . body is not null )
637
- {
638
- var exprVisitor = new ExprVisitor ( temporaryFunction , Handler ) ;
639
- var body = exprVisitor . Visit ( context . body ) ;
640
-
641
- if ( ! pure . Signature . ReturnType . IsSameTypeAs ( body . Type ) )
642
- {
643
- throw Handler . TypeMismatch ( context . body , body . Type , pure . Signature . ReturnType ) ;
644
- }
645
-
646
- pure . Body = body ;
647
- }
648
-
636
+ // body will be handled in a later stage
649
637
return pure ;
650
638
}
651
639
652
640
public override object VisitInvariantDecl ( PParser . InvariantDeclContext context )
653
641
{
654
642
// INVARIANT name=Iden body=Expr
655
643
var inv = ( Invariant ) nodesToDeclarations . Get ( context ) ;
656
-
657
- var temporaryFunction = new Function ( inv . Name , context ) ;
658
- temporaryFunction . Scope = CurrentScope . MakeChildScope ( ) ;
659
-
660
- var exprVisitor = new ExprVisitor ( temporaryFunction , Handler ) ;
661
-
662
- var body = exprVisitor . Visit ( context . body ) ;
663
-
664
- if ( ! PrimitiveType . Bool . IsSameTypeAs ( body . Type ) )
665
- {
666
- throw Handler . TypeMismatch ( context . body , body . Type , PrimitiveType . Bool ) ;
667
- }
668
-
669
- inv . Body = body ;
670
-
671
644
return inv ;
672
645
}
673
646
674
647
public override object VisitAxiomDecl ( PParser . AxiomDeclContext context )
675
648
{
676
649
// Axiom body=Expr
677
650
var inv = ( Axiom ) nodesToDeclarations . Get ( context ) ;
678
-
679
- var temporaryFunction = new Function ( inv . Name , context ) ;
680
- temporaryFunction . Scope = CurrentScope . MakeChildScope ( ) ;
681
-
682
- var exprVisitor = new ExprVisitor ( temporaryFunction , Handler ) ;
683
-
684
- var body = exprVisitor . Visit ( context . body ) ;
685
-
686
- if ( ! PrimitiveType . Bool . IsSameTypeAs ( body . Type ) )
687
- {
688
- throw Handler . TypeMismatch ( context . body , body . Type , PrimitiveType . Bool ) ;
689
- }
690
-
691
- inv . Body = body ;
692
-
693
651
return inv ;
694
652
}
695
653
@@ -699,153 +657,12 @@ public override object VisitInvariantGroupDecl(PParser.InvariantGroupDeclContext
699
657
invGroup . Invariants = context . invariantDecl ( ) . Select ( Visit ) . Cast < Invariant > ( ) . ToList ( ) ;
700
658
return invGroup ;
701
659
}
702
-
703
- private List < Invariant > ToInvariant ( IPExpr e , ParserRuleContext context )
704
- {
705
- if ( e is InvariantGroupRefExpr invGroupRef ) return invGroupRef . Invariants ;
706
- if ( e is InvariantRefExpr invRef ) return [ invRef . Invariant ] ;
707
- if ( ! PrimitiveType . Bool . IsSameTypeAs ( e . Type . Canonicalize ( ) ) )
708
- {
709
- throw Handler . TypeMismatch ( context , e . Type , PrimitiveType . Bool ) ;
710
- }
711
- Invariant inv = new Invariant ( $ "tmp_inv_{ Guid . NewGuid ( ) } ", e , context ) ;
712
- return [ inv ] ;
713
- }
714
-
715
- public override object VisitProofBlock ( PParser . ProofBlockContext context )
716
- {
717
- var proofBlock = ( ProofBlock ) nodesToDeclarations . Get ( context ) ;
718
- proofBlock . Commands = context . proofBody ( ) . proofItem ( ) . Select ( Visit ) . Cast < ProofCommand > ( ) . ToList ( ) ;
719
- proofBlock . Commands . ForEach ( x => x . ProofBlock = proofBlock . Name ) ;
720
- return proofBlock ;
721
- }
722
-
723
- public override object VisitProveUsingCmd ( PParser . ProveUsingCmdContext context )
724
- {
725
- var proofCmd = ( ProofCommand ) nodesToDeclarations . Get ( context ) ;
726
- var temporaryFunction = new Function ( proofCmd . Name , context ) ;
727
- temporaryFunction . Scope = CurrentScope . MakeChildScope ( ) ;
728
- var exprVisitor = new ExprVisitor ( temporaryFunction , Handler ) ;
729
- List < IPExpr > premises = [ ] ;
730
- List < IPExpr > goals = [ ] ;
731
- List < IPExpr > excepts = context . _excludes . Select ( exprVisitor . Visit ) . ToList ( ) ;
732
- if ( context . premisesAll == null )
733
- {
734
- premises = context . _premises . Select ( exprVisitor . Visit ) . ToList ( ) ;
735
- }
736
- else
737
- {
738
- premises = CurrentScope . AllDecls . OfType < Invariant > ( ) . Select ( x => ( IPExpr ) new InvariantRefExpr ( x , context ) ) . ToList ( ) ;
739
- }
740
-
741
- if ( context . goalsAll == null && context . goalsDefault == null )
742
- {
743
- goals = context . _targets . Select ( exprVisitor . Visit ) . ToList ( ) ;
744
- }
745
- else if ( context . goalsDefault != null )
746
- {
747
- goals = [ new InvariantRefExpr ( new Invariant ( context ) , context ) ] ;
748
- }
749
- else
750
- {
751
- goals = CurrentScope . AllDecls . OfType < Invariant > ( ) . Select ( x => ( IPExpr ) new InvariantRefExpr ( x , context ) ) . ToList ( ) ;
752
- }
753
-
754
- if ( premises . Count == context . _premises . Count )
755
- {
756
- proofCmd . Premises = premises . Zip ( context . _premises , ( x , y ) => ToInvariant ( x , y ) ) . SelectMany ( x => x ) . ToList ( ) ;
757
- }
758
- else
759
- {
760
- proofCmd . Premises = premises . SelectMany ( x => ToInvariant ( x , context ) ) . ToList ( ) ;
761
- }
762
-
763
- if ( goals . Count == context . _targets . Count )
764
- {
765
- proofCmd . Goals = goals . Zip ( context . _targets , ( x , y ) => ToInvariant ( x , y ) ) . SelectMany ( x => x ) . ToList ( ) ;
766
- }
767
- else
768
- {
769
- proofCmd . Goals = goals . SelectMany ( x => ToInvariant ( x , context ) ) . ToList ( ) ;
770
- }
771
-
772
- proofCmd . Excepts = excepts . Zip ( context . _excludes , ( x , y ) => ToInvariant ( x , y ) ) . SelectMany ( x => x ) . ToList ( ) ;
773
- proofCmd . Premises = proofCmd . Premises . Except ( proofCmd . Excepts ) . ToList ( ) ;
774
- proofCmd . Goals = proofCmd . Goals . Except ( proofCmd . Excepts ) . ToList ( ) ;
775
-
776
- // prove A using B, ..., C means A -> B, ..., A -> C
777
- // If there is a cycle in the graph formed by all prove-using commands, then we should throw an error.
778
- // We could do this incrementally but the number of prove-using commands will probably be very small anyway
779
- // so we are just going to do a topological sort every time (https://gist.github.com/Sup3rc4l1fr4g1l1571c3xp14l1d0c10u5/3341dba6a53d7171fe3397d13d00ee3f)
780
- // TODO: using _ to pick out sub invariants?
781
- var nodes = new System . Collections . Generic . HashSet < string > ( ) ;
782
- var edges = new System . Collections . Generic . HashSet < ( string , string ) > ( ) ;
783
- foreach ( var cmd in CurrentScope . ProofCommands )
784
- {
785
- if ( cmd . Goals is null ) continue ;
786
- foreach ( var source in cmd . Goals . Select ( inv => inv . Name ) )
787
- {
788
- if ( cmd . Premises is null ) continue ;
789
- foreach ( var target in cmd . Premises . Select ( inv => inv . Name ) )
790
- {
791
- nodes . Add ( source ) ;
792
- nodes . Add ( target ) ;
793
- edges . Add ( ( source , target ) ) ;
794
- }
795
- }
796
- }
797
-
798
- // Set of all nodes with no incoming edges
799
- var S = new System . Collections . Generic . HashSet < string > ( nodes . Where ( n => edges . All ( e => e . Item2 . Equals ( n ) == false ) ) ) ;
800
-
801
- // while S is non-empty do
802
- while ( S . Any ( ) ) {
803
-
804
- // remove a node n from S
805
- var n = S . First ( ) ;
806
- S . Remove ( n ) ;
807
-
808
- // for each node m with an edge e from n to m do
809
- foreach ( var e in edges . Where ( e => e . Item1 . Equals ( n ) ) . ToList ( ) ) {
810
- var m = e . Item2 ;
811
-
812
- // remove edge e from the graph
813
- edges . Remove ( e ) ;
814
-
815
- // if m has no other incoming edges then
816
- if ( edges . All ( me => me . Item2 . Equals ( m ) == false ) ) {
817
- S . Add ( m ) ;
818
- }
819
- }
820
- }
821
-
822
- // if graph has edges then
823
- if ( edges . Any ( ) ) {
824
- throw Handler . CyclicProof ( proofCmd . SourceLocation , proofCmd ) ;
825
- }
826
-
827
- return proofCmd ;
828
- }
829
660
830
661
public override object VisitAssumeOnStartDecl ( PParser . AssumeOnStartDeclContext context )
831
662
{
832
663
// assume on start: body=Expr
833
664
var assume = ( AssumeOnStart ) nodesToDeclarations . Get ( context ) ;
834
-
835
- var temporaryFunction = new Function ( assume . Name , context ) ;
836
- temporaryFunction . Scope = CurrentScope . MakeChildScope ( ) ;
837
-
838
- var exprVisitor = new ExprVisitor ( temporaryFunction , Handler ) ;
839
-
840
- var body = exprVisitor . Visit ( context . body ) ;
841
-
842
- if ( ! PrimitiveType . Bool . IsSameTypeAs ( body . Type ) )
843
- {
844
- throw Handler . TypeMismatch ( context . body , body . Type , PrimitiveType . Bool ) ;
845
- }
846
-
847
- assume . Body = body ;
848
-
665
+ // body will be handled in a later stage
849
666
return assume ;
850
667
}
851
668
0 commit comments