@@ -217,12 +217,12 @@ pretty0
217
217
}
218
218
term =
219
219
specialCases term \ case
220
- Var' ( Var. reset -> v) -> do
220
+ Var' v -> do
221
221
env <- ask
222
222
let name =
223
- if Set. member v env. boundTerms
223
+ if Set. member v env. freeTerms && Set. member v env . boundTerms
224
224
then HQ. fromName (Name. makeAbsolute (Name. unsafeParseVar v))
225
- else elideFQN im $ HQ. unsafeFromVar v
225
+ else elideFQN im $ HQ. unsafeFromVar ( Var. reset v)
226
226
pure . parenIfInfix name ic $ styleHashQualified'' (fmt S. Var ) name
227
227
Ref' r -> do
228
228
env <- ask
@@ -687,30 +687,21 @@ printLetBindings ::
687
687
m [Pretty SyntaxText ]
688
688
printLetBindings context = \ case
689
689
LetBindings bindings -> traverse (printLetBinding context) bindings
690
- LetrecBindings bindings -> traverse (printLetrecBinding context) bindings
690
+ LetrecBindings bindings ->
691
+ let boundVars = map fst bindings
692
+ in traverse (printLetrecBinding context boundVars) bindings
691
693
692
694
printLetBinding :: (MonadPretty v m ) => AmbientContext -> (v , Term3 v PrintAnnotation ) -> m (Pretty SyntaxText )
693
695
printLetBinding context (v, binding)
694
696
| Var. isAction v = pretty0 context binding
695
697
| otherwise =
696
- -- For a non-recursive let binding like "let x = y in z", variable "x" is not bound in "y". Yet, "x" may be free
697
- -- in "y" anyway, referring to some previous binding.
698
- --
699
- -- In Unison we don't have a syntax, for non-recusrive let, though, we just have this:
700
- --
701
- -- x = y
702
- -- z
703
- --
704
- -- So, render free "x" in "y" with a leading dot. This is because we happen to know that the only way to have
705
- -- a free "x" in "y" is if "x" is a top-level binding.
706
- renderPrettyBinding
707
- <$> local (over # boundTerms (Set. insert v1)) (prettyBinding0' context (HQ. unsafeFromVar v1) binding)
698
+ renderPrettyBinding <$> withBoundTerm v (prettyBinding0' context (HQ. unsafeFromVar v1) binding)
708
699
where
709
700
v1 = Var. reset v
710
701
711
- printLetrecBinding :: (MonadPretty v m ) => AmbientContext -> (v , Term3 v PrintAnnotation ) -> m (Pretty SyntaxText )
712
- printLetrecBinding context (v, binding) =
713
- renderPrettyBinding <$> prettyBinding0' context (HQ. unsafeFromVar (Var. reset v)) binding
702
+ printLetrecBinding :: (MonadPretty v m ) => AmbientContext -> [ v ] -> (v , Term3 v PrintAnnotation ) -> m (Pretty SyntaxText )
703
+ printLetrecBinding context vs (v, binding) =
704
+ renderPrettyBinding <$> withBoundTerms vs ( prettyBinding0' context (HQ. unsafeFromVar (Var. reset v)) binding)
714
705
715
706
prettyPattern ::
716
707
forall v loc .
@@ -735,7 +726,7 @@ prettyPattern n c@AmbientContext {imports = im} p vs patt = case patt of
735
726
Pattern. Unbound _ -> (fmt S. DelimiterChar $ l " _" , vs)
736
727
Pattern. Var _ ->
737
728
case vs of
738
- (v : tail_vs) -> (fmt S. Var $ l $ Var. nameStr v , tail_vs)
729
+ (v : tail_vs) -> (fmt S. Var $ l $ Var. nameStr ( Var. reset v) , tail_vs)
739
730
_ -> error " prettyPattern: Expected at least one var"
740
731
Pattern. Boolean _ b -> (fmt S. BooleanLiteral $ if b then l " true" else l " false" , vs)
741
732
Pattern. Int _ i -> (fmt S. NumericLiteral $ (if i >= 0 then l " +" else mempty ) <> l (show i), vs)
@@ -764,7 +755,7 @@ prettyPattern n c@AmbientContext {imports = im} p vs patt = case patt of
764
755
case vs of
765
756
(v : tail_vs) ->
766
757
let (printed, eventual_tail) = prettyPattern n c Prefix tail_vs pat
767
- in (paren (p >= Prefix ) (fmt S. Var (l $ Var. nameStr v ) <> fmt S. DelimiterChar (l " @" ) <> printed), eventual_tail)
758
+ in (paren (p >= Prefix ) (fmt S. Var (l $ Var. nameStr ( Var. reset v) ) <> fmt S. DelimiterChar (l " @" ) <> printed), eventual_tail)
768
759
_ -> error " prettyPattern: Expected at least one var"
769
760
Pattern. EffectPure _ pat ->
770
761
let (printed, eventual_tail) = prettyPattern n c Bottom vs pat
@@ -826,28 +817,28 @@ arity1Branches bs = [([pat], guard, body) | MatchCase pat guard body <- bs]
826
817
groupCases ::
827
818
(Ord v ) =>
828
819
[MatchCase' () (Term3 v ann )] ->
829
- [([Pattern () ], [v ], [(Maybe (Term3 v ann ), Term3 v ann )])]
830
- groupCases ms = go0 ms
820
+ [([Pattern () ], [v ], [(Maybe (Term3 v ann ), ([v ], Term3 v ann ))])]
821
+ groupCases = \ cases
822
+ [] -> []
823
+ ms@ ((p1, _, AbsN' vs1 _) : _) -> go (p1, vs1) [] ms
831
824
where
832
- go0 [] = []
833
- go0 ms@ ((p1, _, AbsN' vs1 _) : _) = go2 (p1, vs1) [] ms
834
- go2 (p0, vs0) acc [] = [(p0, vs0, reverse acc)]
835
- go2 (p0, vs0) acc ms@ ((p1, g1, AbsN' vs body) : tl)
836
- | p0 == p1 && vs == vs0 = go2 (p0, vs0) ((g1, body) : acc) tl
837
- | otherwise = (p0, vs0, reverse acc) : go0 ms
825
+ go (p0, vs0) acc [] = [(p0, vs0, reverse acc)]
826
+ go (p0, vs0) acc ms@ ((p1, g1, AbsN' vs body) : tl)
827
+ | p0 == p1 && vs == vs0 = go (p0, vs0) ((g1, (vs, body)) : acc) tl
828
+ | otherwise = (p0, vs0, reverse acc) : groupCases ms
838
829
839
830
printCase ::
831
+ forall m v .
840
832
(MonadPretty v m ) =>
841
833
Imports ->
842
834
DocLiteralContext ->
843
835
[MatchCase' () (Term3 v PrintAnnotation )] ->
844
836
m (Pretty SyntaxText )
845
- printCase im doc ms0 =
837
+ printCase im doc ms =
846
838
PP. orElse
847
839
<$> (PP. lines . alignGrid True <$> grid)
848
840
<*> (PP. lines . alignGrid False <$> grid)
849
841
where
850
- ms = groupCases ms0
851
842
justify rows =
852
843
zip (fmap fst . PP. align' $ fmap alignPatterns rows) $ fmap gbs rows
853
844
where
@@ -876,19 +867,19 @@ printCase im doc ms0 =
876
867
)
877
868
justified
878
869
justified = PP. leftJustify $ fmap (\ (g, b) -> (g, (arrow, b))) gbs
879
- grid = traverse go ms
880
- patLhs env vs pats =
881
- case pats of
882
- [pat] -> PP. group (fst (prettyPattern env (ac Annotation Block im doc) Bottom vs pat))
883
- pats -> PP. group
884
- . PP. sep (PP. indentAfterNewline " " $ " ," <> PP. softbreak)
885
- . (`evalState` vs)
886
- . for pats
887
- $ \ pat -> do
888
- vs <- State. get
889
- let (p, rem ) = prettyPattern env (ac Annotation Block im doc) Bottom vs pat
890
- State. put rem
891
- pure p
870
+ grid = traverse go (groupCases ms)
871
+ patLhs :: PrettyPrintEnv -> [ v ] -> [ Pattern () ] -> Pretty SyntaxText
872
+ patLhs ppe vs = \ cases
873
+ [pat] -> PP. group (fst (prettyPattern ppe (ac Annotation Block im doc) Bottom vs pat))
874
+ pats -> PP. group
875
+ . PP. sep (PP. indentAfterNewline " " $ " ," <> PP. softbreak)
876
+ . (`evalState` vs)
877
+ . for pats
878
+ $ \ pat -> do
879
+ vs <- State. get
880
+ let (p, rem ) = prettyPattern ppe (ac Annotation Block im doc) Bottom vs pat
881
+ State. put rem
882
+ pure p
892
883
arrow = fmt S. ControlKeyword " ->"
893
884
-- If there's multiple guarded cases for this pattern, prints as:
894
885
-- MyPattern x y
@@ -910,7 +901,7 @@ printCase im doc ms0 =
910
901
-- like any other variable, ex: case Foo x y | x < y -> ...
911
902
PP. spaceIfNeeded (fmt S. DelimiterChar " |" )
912
903
<$> pretty0 (ac Control Normal im doc) g
913
- printBody = pretty0 (ac Annotation Block im doc)
904
+ printBody (vs, body) = withBoundTerms vs ( pretty0 (ac Annotation Block im doc) body )
914
905
915
906
-- A pretty term binding, split into the type signature (possibly empty) and the term.
916
907
data PrettyBinding = PrettyBinding
@@ -989,7 +980,7 @@ prettyBinding0 ::
989
980
m PrettyBinding
990
981
prettyBinding0 ac v tm = do
991
982
env <- ask
992
- prettyBinding0' ac v (printAnnotate env. ppe tm)
983
+ local (set # freeTerms ( ABT. freeVars tm)) ( prettyBinding0' ac v (printAnnotate env. ppe tm) )
993
984
994
985
prettyBinding0' ::
995
986
(MonadPretty v m ) =>
0 commit comments