Skip to content

Commit d3597dc

Browse files
committed
Add strictness annotations to some of the proof data structures.
Perhaps this will plug some space leaks.
1 parent 1986544 commit d3597dc

File tree

1 file changed

+62
-60
lines changed

1 file changed

+62
-60
lines changed

src/SAWScript/Proof.hs

+62-60
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,12 @@ simplifySequent sc ss (UnfocusedSequent hs gs) =
290290
do (a, hs') <- simplifyProps sc ss hs
291291
(b, gs') <- simplifyProps sc ss gs
292292
return (Set.union a b, UnfocusedSequent hs' gs')
293-
simplifySequent sc ss (GoalFocusedSequent hs (gs1,g,gs2)) =
293+
simplifySequent sc ss (GoalFocusedSequent hs (FB gs1 g gs2)) =
294294
do (a, g') <- simplifyProp sc ss g
295-
return (a, GoalFocusedSequent hs (gs1, g', gs2))
296-
simplifySequent sc ss (HypFocusedSequent (hs1, h, hs2) gs) =
295+
return (a, GoalFocusedSequent hs (FB gs1 g' gs2))
296+
simplifySequent sc ss (HypFocusedSequent (FB hs1 h hs2) gs) =
297297
do (a, h') <- simplifyProp sc ss h
298-
return (a, HypFocusedSequent (hs1, h', hs2) gs)
298+
return (a, HypFocusedSequent (FB hs1 h' hs2) gs)
299299

300300

301301
hoistIfsInGoal :: SharedContext -> Prop -> IO Prop
@@ -385,15 +385,17 @@ ppProp opts nenv (Prop tm) = ppTermWithNames opts nenv tm
385385
-- TODO, I'd like to add metadata here
386386
type SequentBranch = Prop
387387

388+
data FocusedBranch = FB ![SequentBranch] !SequentBranch ![SequentBranch]
389+
388390
data Sequent
389-
= UnfocusedSequent [SequentBranch] [SequentBranch]
390-
| GoalFocusedSequent [SequentBranch] ([SequentBranch], SequentBranch, [SequentBranch])
391-
| HypFocusedSequent ([SequentBranch], SequentBranch, [SequentBranch]) [SequentBranch]
391+
= UnfocusedSequent ![SequentBranch] ![SequentBranch]
392+
| GoalFocusedSequent ![SequentBranch] !FocusedBranch
393+
| HypFocusedSequent !FocusedBranch ![SequentBranch]
392394

393395
unfocus :: Sequent -> ([SequentBranch],[SequentBranch])
394396
unfocus (UnfocusedSequent hs gs) = (hs,gs)
395-
unfocus (GoalFocusedSequent hs (gs1,g,gs2)) = (hs, gs1 ++ g : gs2)
396-
unfocus (HypFocusedSequent (hs1,h,hs2) gs) = (hs1 ++ h : hs2, gs)
397+
unfocus (GoalFocusedSequent hs (FB gs1 g gs2)) = (hs, gs1 ++ g : gs2)
398+
unfocus (HypFocusedSequent (FB hs1 h hs2) gs) = (hs1 ++ h : hs2, gs)
397399

398400
unfocusSequent :: Sequent -> Sequent
399401
unfocusSequent sqt = UnfocusedSequent hs gs
@@ -403,22 +405,22 @@ focusOnGoal :: Integer -> Sequent -> Maybe Sequent
403405
focusOnGoal i sqt =
404406
let (hs,gs) = unfocus sqt in
405407
case genericDrop i gs of
406-
(g:gs2) -> Just (GoalFocusedSequent hs (genericTake i gs, g, gs2))
408+
(g:gs2) -> Just (GoalFocusedSequent hs (FB (genericTake i gs) g gs2))
407409
[] -> Nothing
408410

409411
focusOnHyp :: Integer -> Sequent -> Maybe Sequent
410412
focusOnHyp i sqt =
411413
let (hs,gs) = unfocus sqt in
412414
case genericDrop i hs of
413-
(h:hs2) -> Just (HypFocusedSequent (genericTake i hs, h, hs2) gs)
415+
(h:hs2) -> Just (HypFocusedSequent (FB (genericTake i hs) h hs2) gs)
414416
[] -> Nothing
415417

416418
sequentToRawSequent :: Sequent -> RawSequent Prop
417419
sequentToRawSequent sqt =
418420
case sqt of
419-
UnfocusedSequent hs gs -> RawSequent hs gs
420-
GoalFocusedSequent hs (gs1, g, gs2) -> RawSequent hs (gs1 ++ g : gs2)
421-
HypFocusedSequent (hs1, h, hs2) gs -> RawSequent (hs1 ++ h : hs2) gs
421+
UnfocusedSequent hs gs -> RawSequent hs gs
422+
GoalFocusedSequent hs (FB gs1 g gs2) -> RawSequent hs (gs1 ++ g : gs2)
423+
HypFocusedSequent (FB hs1 h hs2) gs -> RawSequent (hs1 ++ h : hs2) gs
422424

423425

424426
sequentConstantSet :: Sequent -> Map VarIndex (NameInfo, Term, Maybe Term)
@@ -454,21 +456,20 @@ convertibleSequents sc sqt1 sqt2 =
454456
RawSequent hs2 gs2 = sequentToRawSequent sqt2
455457

456458

457-
458459
data SequentState
459460
= Unfocused
460461
| GoalFocus Prop (Prop -> Sequent)
461462
| HypFocus Prop (Prop -> Sequent)
462463

463464
propToSequent :: Prop -> Sequent
464-
propToSequent p = GoalFocusedSequent [] ([], p, [])
465+
propToSequent p = GoalFocusedSequent [] (FB [] p [])
465466

466467
booleansToSequent :: SharedContext -> [Term] -> [Term] -> IO Sequent
467468
booleansToSequent sc hs gs =
468469
do hs' <- mapM (boolToProp sc []) hs
469470
gs' <- mapM (boolToProp sc []) gs
470471
case gs' of
471-
[g] -> return (GoalFocusedSequent hs' ([],g,[]))
472+
[g] -> return (GoalFocusedSequent hs' (FB [] g []))
472473
_ -> return (UnfocusedSequent hs' gs')
473474

474475
sequentToProp :: SharedContext -> Sequent -> IO Prop
@@ -508,15 +509,15 @@ ppRawSequent sqt (RawSequent hs gs) =
508509
turnstile = [ pretty (take 40 (repeat '=')) ]
509510
focused doc = "<<" <> doc <> ">>"
510511
ppHyp (i, tm)
511-
| HypFocusedSequent (hs1,_h,_hs2) _gs <- sqt
512+
| HypFocusedSequent (FB hs1 _h _hs2) _gs <- sqt
512513
, length hs1 == i
513514
= focused ("H" <> pretty i) <+> tm
514515

515516
| otherwise
516517
= "H" <> pretty i <> ":" <+> tm
517518

518519
ppGoal (i, tm)
519-
| GoalFocusedSequent _hs (gs1,_g,_gs2) <- sqt
520+
| GoalFocusedSequent _hs (FB gs1 _g _gs2) <- sqt
520521
, length gs1 == i
521522
= focused ("G" <> pretty i) <+> tm
522523

@@ -537,10 +538,10 @@ filterPosList pss xs = map snd $ filter f $ zip [0..] xs
537538
where
538539
f (i,_) = cofinSetMember i pss
539540

540-
filterFocusedList :: CofinSet Integer -> ([a],a,[a]) -> Either [a] ([a],a,[a])
541-
filterFocusedList pss (xs1,x,xs2) =
541+
filterFocusedList :: CofinSet Integer -> FocusedBranch -> Either [SequentBranch] FocusedBranch
542+
filterFocusedList pss (FB xs1 x xs2) =
542543
if cofinSetMember idx pss then
543-
Right (xs1',x,xs2')
544+
Right (FB xs1' x xs2')
544545
else
545546
Left (xs1' ++ xs2')
546547
where
@@ -572,19 +573,19 @@ filterGoals pss (GoalFocusedSequent hs gs) =
572573
addHypothesis :: Prop -> Sequent -> Sequent
573574
addHypothesis p (UnfocusedSequent hs gs) = UnfocusedSequent (hs ++ [p]) gs
574575
addHypothesis p (GoalFocusedSequent hs gs) = GoalFocusedSequent (hs ++ [p]) gs
575-
addHypothesis p (HypFocusedSequent (hs1,h,hs2) gs) = HypFocusedSequent (hs1,h,hs2++[p]) gs
576+
addHypothesis p (HypFocusedSequent (FB hs1 h hs2) gs) = HypFocusedSequent (FB hs1 h (hs2++[p])) gs
576577

577578
addNewFocusedGoal :: Prop -> Sequent -> Sequent
578579
addNewFocusedGoal p sqt =
579580
let RawSequent hs gs = sequentToRawSequent sqt
580-
in GoalFocusedSequent hs (gs,p,[])
581+
in GoalFocusedSequent hs (FB gs p [])
581582

582583
sequentState :: Sequent -> SequentState
583584
sequentState (UnfocusedSequent _ _) = Unfocused
584-
sequentState (GoalFocusedSequent hs (gs1,g,gs2)) =
585-
GoalFocus g (\g' -> GoalFocusedSequent hs (gs1,g',gs2))
586-
sequentState (HypFocusedSequent (hs1,h,hs2) gs) =
587-
HypFocus h (\h' -> HypFocusedSequent (hs1,h',hs2) gs)
585+
sequentState (GoalFocusedSequent hs (FB gs1 g gs2)) =
586+
GoalFocus g (\g' -> GoalFocusedSequent hs (FB gs1 g' gs2))
587+
sequentState (HypFocusedSequent (FB hs1 h hs2) gs) =
588+
HypFocus h (\h' -> HypFocusedSequent (FB hs1 h' hs2) gs)
588589

589590
sequentSharedSize :: Sequent -> Integer
590591
sequentSharedSize sqt = scSharedSizeMany (map unProp (hs ++ gs))
@@ -599,34 +600,34 @@ sequentTreeSize sqt = scTreeSizeMany (map unProp (hs ++ gs))
599600
traverseSequentWithFocus :: Applicative m => (Prop -> m Prop) -> Sequent -> m Sequent
600601
traverseSequentWithFocus f (UnfocusedSequent hs gs) =
601602
UnfocusedSequent <$> traverse f hs <*> traverse f gs
602-
traverseSequentWithFocus f (GoalFocusedSequent hs (gs1, g, gs2)) =
603-
(\g' -> GoalFocusedSequent hs (gs1, g', gs2)) <$> f g
604-
traverseSequentWithFocus f (HypFocusedSequent (hs1, h, hs2) gs) =
605-
(\h' -> HypFocusedSequent (hs1, h', hs2) gs) <$> f h
603+
traverseSequentWithFocus f (GoalFocusedSequent hs (FB gs1 g gs2)) =
604+
(\g' -> GoalFocusedSequent hs (FB gs1 g' gs2)) <$> f g
605+
traverseSequentWithFocus f (HypFocusedSequent (FB hs1 h hs2) gs) =
606+
(\h' -> HypFocusedSequent (FB hs1 h' hs2) gs) <$> f h
606607

607608
traverseSequent :: Applicative m => (Prop -> m Prop) -> Sequent -> m Sequent
608609
traverseSequent f (UnfocusedSequent hs gs) =
609610
UnfocusedSequent <$> traverse f hs <*> traverse f gs
610-
traverseSequent f (GoalFocusedSequent hs (gs1, g, gs2)) =
611+
traverseSequent f (GoalFocusedSequent hs (FB gs1 g gs2)) =
611612
GoalFocusedSequent <$>
612613
(traverse f hs) <*>
613-
( (,,) <$> traverse f gs1 <*> f g <*> traverse f gs2)
614+
( FB <$> traverse f gs1 <*> f g <*> traverse f gs2)
614615

615-
traverseSequent f (HypFocusedSequent (hs1, h, hs2) gs) =
616+
traverseSequent f (HypFocusedSequent (FB hs1 h hs2) gs) =
616617
HypFocusedSequent <$>
617-
( (,,) <$> traverse f hs1 <*> f h <*> traverse f hs2) <*>
618+
( FB <$> traverse f hs1 <*> f h <*> traverse f hs2) <*>
618619
(traverse f gs)
619620

620621
checkSequent :: SharedContext -> PPOpts -> Sequent -> IO ()
621622
checkSequent sc ppOpts (UnfocusedSequent hs gs) =
622623
do forM_ hs (checkProp sc ppOpts)
623624
forM_ gs (checkProp sc ppOpts)
624-
checkSequent sc ppOpts (GoalFocusedSequent hs (gs1,g,gs2)) =
625+
checkSequent sc ppOpts (GoalFocusedSequent hs (FB gs1 g gs2)) =
625626
do forM_ hs (checkProp sc ppOpts)
626627
forM_ gs1 (checkProp sc ppOpts)
627628
checkProp sc ppOpts g
628629
forM_ gs2 (checkProp sc ppOpts)
629-
checkSequent sc ppOpts (HypFocusedSequent (hs1,h,hs2) gs) =
630+
checkSequent sc ppOpts (HypFocusedSequent (FB hs1 h hs2) gs) =
630631
do forM_ hs1 (checkProp sc ppOpts)
631632
checkProp sc ppOpts h
632633
forM_ hs2 (checkProp sc ppOpts)
@@ -740,42 +741,42 @@ instance Semigroup TheoremSummary where
740741
data Evidence
741742
= -- | The given term provides a direct programs-as-proofs witness
742743
-- for the truth of its type (qua proposition).
743-
ProofTerm Term
744+
ProofTerm !Term
744745

745746
-- | This type of evidence refers to a local assumption that
746747
-- must have been introduced by a surrounding @AssumeEvidence@
747748
-- constructor.
748-
| LocalAssumptionEvidence Prop TheoremNonce
749+
| LocalAssumptionEvidence !Prop !TheoremNonce
749750

750751
-- | This type of evidence is produced when the given proposition
751752
-- has been dispatched to a solver which has indicated that it
752753
-- was able to prove the proposition. The included @SolverStats@
753754
-- give some details about the solver run.
754-
| SolverEvidence SolverStats Sequent
755+
| SolverEvidence !SolverStats !Sequent
755756

756757
-- | This type of evidence is produced when the given proposition
757758
-- has been randomly tested against input vectors in the style
758759
-- of quickcheck. The included number is the number of successfully
759760
-- passed test vectors.
760-
| QuickcheckEvidence Integer Sequent
761+
| QuickcheckEvidence !Integer !Sequent
761762

762763
-- | This type of evidence is produced when the given proposition
763764
-- has been explicitly assumed without other evidence at the
764765
-- user's direction.
765-
| Admitted Text Pos Sequent
766+
| Admitted !Text !Pos !Sequent
766767

767768
-- | This type of evidence is produced when a proposition can be deconstructed
768769
-- along a conjunction into two subgoals, each of which is supported by
769770
-- the included evidence.
770-
| SplitEvidence Evidence Evidence
771+
| SplitEvidence !Evidence !Evidence
771772

772773
-- | This type of evidence is produced when a previously-proved theorem is
773774
-- applied via backward reasoning to prove a goal. Pi-quantified variables
774775
-- of the theorem may be specialized either by giving an explicit @Term@ to
775776
-- instantiate the variable, or by giving @Evidence@ for @Prop@ hypotheses.
776777
-- After specializing the given @Theorem@ the result must match the
777778
-- current goal.
778-
| ApplyEvidence Theorem [Either Term Evidence]
779+
| ApplyEvidence !Theorem ![Either Term Evidence]
779780

780781
-- | This type of evidence is used to prove an implication. The included
781782
-- proposition must match the hypothesis of the goal, and the included
@@ -784,53 +785,53 @@ data Evidence
784785
-- | AssumeEvidence TheoremNonce Prop Evidence
785786

786787
-- | This type of evidence is used to prove a universally-quantified statement.
787-
| IntroEvidence (ExtCns Term) Evidence
788+
| IntroEvidence !(ExtCns Term) !Evidence
788789

789790
-- | This type of evidence is used to apply the "cut rule" of sequent calculus.
790791
-- The given proposition is added to the hypothesis list in the first
791792
-- deriviation, and into the conclusion list in the second, where it is focused.
792-
| CutEvidence Prop Evidence Evidence
793+
| CutEvidence !Prop !Evidence !Evidence
793794

794795
-- | This type of evidence is used to modify a goal to prove via rewriting.
795796
-- The goal to prove is rewritten by the given simpset; then the provided
796797
-- evidence is used to check the modified goal.
797-
| RewriteEvidence (Simpset TheoremNonce) Evidence
798+
| RewriteEvidence !(Simpset TheoremNonce) !Evidence
798799

799800
-- | This type of evidence is used to modify a goal to prove via unfolding
800801
-- constant definitions. The goal to prove is modified by unfolding
801802
-- constants identified via the given set of @VarIndex@; then the provided
802803
-- evidence is used to check the modified goal.
803-
| UnfoldEvidence (Set VarIndex) Evidence
804+
| UnfoldEvidence !(Set VarIndex) !Evidence
804805

805806
-- | This type of evidence is used to modify a goal to prove via evaluation
806807
-- into the the What4 formula representation. During evaluation, the
807808
-- constants identified by the given set of @VarIndex@ are held
808809
-- uninterpreted (i.e., will not be unfolded). Then, the provided
809810
-- evidence is use to check the modified goal.
810-
| EvalEvidence (Set VarIndex) Evidence
811+
| EvalEvidence !(Set VarIndex) !Evidence
811812

812813
-- | This type of evidence is used to modify a focused part of the goal.
813814
-- The modified goal should be equivalent up to conversion.
814-
| ConversionEvidence Sequent Evidence
815+
| ConversionEvidence !Sequent !Evidence
815816

816817
-- | This type of evidence is used to modify a goal to prove by applying
817818
-- 'hoistIfsInGoal'.
818-
| HoistIfsEvidence Evidence
819+
| HoistIfsEvidence !Evidence
819820

820821
-- | Change the state of the sequent in some "structural" way. This
821822
-- can involve changing focus, reordering or applying weakening rules.
822-
| StructuralEvidence Sequent Evidence
823+
| StructuralEvidence !Sequent !Evidence
823824

824825
-- | Change the state of the sequent in some way that is governed by
825826
-- the "reversable" L/R rules of the sequent calculus, e.g.,
826827
-- conjunctions in hypotheses can be split into multiple hypotheses,
827828
-- negated conclusions become positive hypotheses, etc.
828-
| NormalizeSequentEvidence Sequent Evidence
829+
| NormalizeSequentEvidence !Sequent !Evidence
829830

830831
-- | Change the sate of th sequent by invoking the term evaluator
831832
-- on the focused sequent branch (or all branches, if unfocused).
832833
-- Treat the given variable indexes as opaque.
833-
| NormalizePropEvidence (Set VarIndex) Evidence
834+
| NormalizePropEvidence !(Set VarIndex) !Evidence
834835

835836
-- | This type of evidence is used when the current sequent, after
836837
-- applying structural rules, is an instance of the basic
@@ -1044,7 +1045,7 @@ data ProofGoal =
10441045
, goalLoc :: String
10451046
, goalDesc :: String
10461047
, goalTags :: Set String
1047-
, goalSequent :: Sequent
1048+
, goalSequent :: !Sequent
10481049
}
10491050

10501051

@@ -1080,11 +1081,11 @@ predicateToProp sc quant = loop []
10801081
Prop <$> toPi argTs t
10811082

10821083

1083-
-- | A ProofState represents a sequent, where the collection of goals
1084-
-- implies the conclusion.
1084+
-- | A ProofState consists of a sequents of goals, represented by sequents.
1085+
-- If each subgoal is provable, that implies the ultimate conclusion.
10851086
data ProofState =
10861087
ProofState
1087-
{ _psGoals :: [ProofGoal]
1088+
{ _psGoals :: ![ProofGoal]
10881089
, _psConcl :: (Sequent,Pos,Maybe ProgramLoc,Text)
10891090
, _psStats :: SolverStats
10901091
, _psTimeout :: Maybe Integer
@@ -1541,7 +1542,8 @@ withFirstGoal (Tactic f) (ProofState goals concl stats timeout evidenceCont star
15411542
do let (es1, es2) = splitAt (length gs') es
15421543
e <- buildTacticEvidence es1
15431544
evidenceCont (e:es2)
1544-
return (Right (x, ProofState (gs' <> gs) concl (stats <> stats') timeout evidenceCont' start))
1545+
let ps' = ProofState (gs' <> gs) concl (stats <> stats') timeout evidenceCont' start
1546+
seq ps' (return (Right (x, ps')))
15451547

15461548
predicateToSATQuery :: SharedContext -> Set VarIndex -> Term -> IO SATQuery
15471549
predicateToSATQuery sc unintSet tm0 =

0 commit comments

Comments
 (0)