@@ -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
@@ -827,7 +818,7 @@ groupCases ::
827
818
(Ord v ) =>
828
819
[MatchCase' () (Term3 v ann )] ->
829
820
[([Pattern () ], [v ], [(Maybe (Term3 v ann ), Term3 v ann )])]
830
- groupCases ms = go0 ms
821
+ groupCases = go0
831
822
where
832
823
go0 [] = []
833
824
go0 ms@ ((p1, _, AbsN' vs1 _) : _) = go2 (p1, vs1) [] ms
@@ -842,12 +833,11 @@ printCase ::
842
833
DocLiteralContext ->
843
834
[MatchCase' () (Term3 v PrintAnnotation )] ->
844
835
m (Pretty SyntaxText )
845
- printCase im doc ms0 =
836
+ printCase im doc ms =
846
837
PP. orElse
847
838
<$> (PP. lines . alignGrid True <$> grid)
848
839
<*> (PP. lines . alignGrid False <$> grid)
849
840
where
850
- ms = groupCases ms0
851
841
justify rows =
852
842
zip (fmap fst . PP. align' $ fmap alignPatterns rows) $ fmap gbs rows
853
843
where
@@ -876,19 +866,18 @@ printCase im doc ms0 =
876
866
)
877
867
justified
878
868
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
869
+ grid = traverse go (groupCases ms)
870
+ patLhs ppe vs = \ cases
871
+ [pat] -> PP. group (fst (prettyPattern ppe (ac Annotation Block im doc) Bottom vs pat))
872
+ pats -> PP. group
873
+ . PP. sep (PP. indentAfterNewline " " $ " ," <> PP. softbreak)
874
+ . (`evalState` vs)
875
+ . for pats
876
+ $ \ pat -> do
877
+ vs <- State. get
878
+ let (p, rem ) = prettyPattern ppe (ac Annotation Block im doc) Bottom vs pat
879
+ State. put rem
880
+ pure p
892
881
arrow = fmt S. ControlKeyword " ->"
893
882
-- If there's multiple guarded cases for this pattern, prints as:
894
883
-- MyPattern x y
@@ -989,7 +978,7 @@ prettyBinding0 ::
989
978
m PrettyBinding
990
979
prettyBinding0 ac v tm = do
991
980
env <- ask
992
- prettyBinding0' ac v (printAnnotate env. ppe tm)
981
+ local (set # freeTerms ( ABT. freeVars tm)) ( prettyBinding0' ac v (printAnnotate env. ppe tm) )
993
982
994
983
prettyBinding0' ::
995
984
(MonadPretty v m ) =>
0 commit comments