@@ -11,7 +11,6 @@ module Kore.Internal.TermLike.TermLike
11
11
, Evaluated (.. )
12
12
, TermLike (.. )
13
13
, TermLikeF (.. )
14
- , externalizeFreshVariables
15
14
, extractAttributes
16
15
, freeVariables
17
16
, mapVariables
@@ -34,12 +33,8 @@ import Control.DeepSeq
34
33
)
35
34
import qualified Control.Lens as Lens
36
35
import qualified Control.Lens.Combinators as Lens.Combinators
37
- import Control.Monad.Reader
38
- ( Reader
39
- )
40
36
import qualified Control.Monad.Reader as Reader
41
37
import qualified Data.Bifunctor as Bifunctor
42
- import qualified Data.Foldable as Foldable
43
38
import Data.Functor.Adjunction
44
39
( Adjunction (.. )
45
40
)
@@ -62,11 +57,6 @@ import qualified Data.Generics.Product as Lens.Product
62
57
import Data.List
63
58
( foldl'
64
59
)
65
- import qualified Data.Map.Strict as Map
66
- import Data.Set
67
- ( Set
68
- )
69
- import qualified Data.Set as Set
70
60
import qualified Generics.SOP as SOP
71
61
import qualified GHC.Generics as GHC
72
62
import qualified GHC.Stack as GHC
@@ -126,7 +116,6 @@ import Kore.Syntax.Or
126
116
import Kore.Syntax.Rewrites
127
117
import Kore.Syntax.StringLiteral
128
118
import Kore.Syntax.Top
129
- import Kore.Syntax.Variable as Variable
130
119
import Kore.TopBottom
131
120
import Kore.Unparser
132
121
( Unparse (.. )
@@ -136,7 +125,6 @@ import Kore.Variables.Binding
136
125
import Kore.Variables.Fresh
137
126
( FreshPartialOrd
138
127
)
139
- import qualified Kore.Variables.Fresh as Fresh
140
128
import qualified Pretty
141
129
import qualified SQL
142
130
@@ -294,11 +282,9 @@ instance NFData variable => NFData (TermLike variable) where
294
282
rnf (Recursive. project -> annotation :< pat) =
295
283
rnf annotation `seq` rnf pat
296
284
297
- instance
298
- (From variable VariableName , Ord variable ) => Unparse (TermLike variable )
299
- where
285
+ instance (Unparse variable , Ord variable ) => Unparse (TermLike variable ) where
300
286
unparse term =
301
- case Recursive. project freshVarTerm of
287
+ case Recursive. project term of
302
288
(attrs :< termLikeF)
303
289
| hasKnownCreator created ->
304
290
Pretty. sep
@@ -350,18 +336,10 @@ instance
350
336
constructorLike =
351
337
Pattern. getConstructorLike
352
338
(Attribute. constructorLikeAttribute attrs)
353
- where
354
- freshVarTerm =
355
- externalizeFreshVariables
356
- $ mapVariables (pure toVariableName) term
357
339
358
340
unparse2 term =
359
- case Recursive. project freshVarTerm of
341
+ case Recursive. project term of
360
342
(_ :< pat) -> unparse2 pat
361
- where
362
- freshVarTerm =
363
- externalizeFreshVariables
364
- $ mapVariables (pure toVariableName) term
365
343
366
344
type instance Base (TermLike variable ) =
367
345
CofreeF (TermLikeF variable ) (Attribute. Pattern variable )
@@ -816,219 +794,6 @@ sequenceAdjunct gsequence =
816
794
contract = counit
817
795
{-# INLINE sequenceAdjunct #-}
818
796
819
- {- | Reset the 'variableCounter' of all 'Variables'.
820
-
821
- @externalizeFreshVariables@ resets the 'variableCounter' of all variables, while
822
- ensuring that no 'Variable' in the result is accidentally captured.
823
-
824
- -}
825
- externalizeFreshVariables :: TermLike VariableName -> TermLike VariableName
826
- externalizeFreshVariables termLike =
827
- Reader. runReader
828
- (Recursive. fold externalizeFreshVariablesWorker termLike)
829
- renamedFreeVariables
830
- where
831
- -- | 'originalFreeVariables' are present in the original pattern; they do
832
- -- not have a generated counter. 'generatedFreeVariables' have a generated
833
- -- counter, usually because they were introduced by applying some axiom.
834
- originalFreeVariables, generatedFreeVariables
835
- :: Set (SomeVariable VariableName )
836
- (originalFreeVariables, generatedFreeVariables) =
837
- Set. partition (foldSomeVariable (pure Variable. isOriginalVariableName))
838
- $ FreeVariables. toSet $ freeVariables termLike
839
-
840
- -- | The map of generated free variables, renamed to be unique from the
841
- -- original free variables.
842
- renamedFreeVariables :: VariableNameMap VariableName VariableName
843
- (renamedFreeVariables, _) =
844
- Foldable. foldl' rename initial generatedFreeVariables
845
- where
846
- initial
847
- :: ( VariableNameMap VariableName VariableName
848
- , FreeVariables VariableName
849
- )
850
- initial = (mempty , foldMap freeVariable originalFreeVariables)
851
- rename
852
- :: ( VariableNameMap VariableName VariableName
853
- , FreeVariables VariableName
854
- )
855
- -> SomeVariable VariableName
856
- -> ( VariableNameMap VariableName VariableName
857
- , FreeVariables VariableName
858
- )
859
- rename (renaming, avoiding) variable =
860
- case variableName variable of
861
- SomeVariableNameElement elementVariableName ->
862
- let
863
- elementVariableName' =
864
- safeElementVariable avoiding elementVariableName
865
- elementVariable' = variable $> elementVariableName'
866
- renaming' =
867
- renameElementVariable
868
- ((,)
869
- <$> elementVariableName
870
- <*> elementVariableName'
871
- )
872
- renaming
873
- variable' = inject elementVariable'
874
- avoiding' = freeVariable variable' <> avoiding
875
- in
876
- (renaming', avoiding')
877
- SomeVariableNameSet setVariableName ->
878
- let
879
- setVariableName' =
880
- safeSetVariable avoiding setVariableName
881
- setVariable' = variable $> setVariableName'
882
- renaming' =
883
- renameSetVariable
884
- ((,)
885
- <$> setVariableName
886
- <*> setVariableName'
887
- )
888
- renaming
889
- variable' = inject setVariable'
890
- avoiding' = freeVariable variable' <> avoiding
891
- in
892
- (renaming', avoiding')
893
-
894
- lookupElementVariable
895
- :: VariableName
896
- -> Reader (VariableNameMap VariableName VariableName ) VariableName
897
- lookupElementVariable elementVariableName =
898
- Reader. asks
899
- $ fromMaybe elementVariableName
900
- . Map. lookup elementVariableName
901
- . unElementVariableName
902
- . adjSomeVariableNameElement
903
-
904
- lookupSetVariable
905
- :: VariableName
906
- -> Reader (VariableNameMap VariableName VariableName ) VariableName
907
- lookupSetVariable setVariableName =
908
- Reader. asks
909
- $ fromMaybe setVariableName
910
- . Map. lookup setVariableName
911
- . unSetVariableName
912
- . adjSomeVariableNameSet
913
-
914
- lookupVariable =
915
- AdjSomeVariableName
916
- { adjSomeVariableNameElement = ElementVariableName lookupElementVariable
917
- , adjSomeVariableNameSet = SetVariableName lookupSetVariable
918
- }
919
-
920
- {- | Externalize a variable safely.
921
-
922
- The variable's counter is incremented until its externalized form is unique
923
- among the set of avoided variables. The externalized form is returned.
924
-
925
- -}
926
- safeVariable
927
- :: Injection (SomeVariableName VariableName ) (f VariableName )
928
- => (Functor f , FreshPartialOrd (f VariableName ))
929
- => FreeVariables VariableName
930
- -> f VariableName
931
- -> f VariableName
932
- safeVariable avoiding variable =
933
- head
934
- $ dropWhile wouldCapture
935
- $ fmap externalize
936
- $ iterate Fresh. nextVariable variable
937
- where
938
- wouldCapture var = isFreeVariable (inject var) avoiding
939
- externalize = fmap Variable. externalizeFreshVariableName
940
-
941
- safeElementVariable
942
- :: FreeVariables VariableName
943
- -> ElementVariableName VariableName
944
- -> ElementVariableName VariableName
945
- safeElementVariable avoiding = safeVariable avoiding
946
-
947
- safeSetVariable
948
- :: FreeVariables VariableName
949
- -> SetVariableName VariableName
950
- -> SetVariableName VariableName
951
- safeSetVariable avoiding = safeVariable avoiding
952
-
953
- underElementBinder freeVariables' variable child = do
954
- let variable' = safeElementVariable freeVariables' <$> variable
955
- names = (,) <$> variableName variable <*> variableName variable'
956
- child' <- Reader. local (renameElementVariable names) child
957
- return (variable', child')
958
-
959
- underSetBinder freeVariables' variable child = do
960
- let variable' = safeSetVariable freeVariables' <$> variable
961
- names = (,) <$> variableName variable <*> variableName variable'
962
- child' <- Reader. local (renameSetVariable names) child
963
- return (variable', child')
964
-
965
- externalizeFreshVariablesWorker
966
- :: Base
967
- (TermLike VariableName )
968
- (RenamingT VariableName VariableName Identity
969
- (TermLike VariableName )
970
- )
971
- -> RenamingT VariableName VariableName Identity (TermLike VariableName )
972
- externalizeFreshVariablesWorker (attrs :< patt) = do
973
- attrs' <- Attribute. traverseVariables lookupVariable attrs
974
- let freeVariables' = Attribute. freeVariables attrs'
975
- patt' <-
976
- case patt of
977
- ExistsF exists -> do
978
- let Exists { existsVariable, existsChild } = exists
979
- (existsVariable', existsChild') <-
980
- underElementBinder
981
- freeVariables'
982
- existsVariable
983
- existsChild
984
- let exists' =
985
- exists
986
- { existsVariable = existsVariable'
987
- , existsChild = existsChild'
988
- }
989
- return (ExistsF exists')
990
- ForallF forall -> do
991
- let Forall { forallVariable, forallChild } = forall
992
- (forallVariable', forallChild') <-
993
- underElementBinder
994
- freeVariables'
995
- forallVariable
996
- forallChild
997
- let forall' =
998
- forall
999
- { forallVariable = forallVariable'
1000
- , forallChild = forallChild'
1001
- }
1002
- return (ForallF forall')
1003
- MuF mu -> do
1004
- let Mu { muVariable, muChild } = mu
1005
- (muVariable', muChild') <-
1006
- underSetBinder
1007
- freeVariables'
1008
- muVariable
1009
- muChild
1010
- let mu' =
1011
- mu
1012
- { muVariable = muVariable'
1013
- , muChild = muChild'
1014
- }
1015
- return (MuF mu')
1016
- NuF nu -> do
1017
- let Nu { nuVariable, nuChild } = nu
1018
- (nuVariable', nuChild') <-
1019
- underSetBinder
1020
- freeVariables'
1021
- nuVariable
1022
- nuChild
1023
- let nu' =
1024
- nu
1025
- { nuVariable = nuVariable'
1026
- , nuChild = nuChild'
1027
- }
1028
- return (NuF nu')
1029
- _ -> traverseVariablesF lookupVariable patt >>= sequence
1030
- (return . Recursive. embed) (attrs' :< patt')
1031
-
1032
797
updateCallStack
1033
798
:: forall variable
1034
799
. HasCallStack
0 commit comments