Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve pretty printing of large formulas #540

Open
facundominguez opened this issue Apr 7, 2022 · 2 comments
Open

Improve pretty printing of large formulas #540

facundominguez opened this issue Apr 7, 2022 · 2 comments

Comments

@facundominguez
Copy link
Collaborator

facundominguez commented Apr 7, 2022

In the .fq files generated from running LH, there are sometimes deeply indented expressions that span multiple lines and which are not particularly easy to read. Improving the formatting is important because it takes time to make sense of these expressions when debugging.

Here's an excerpt from stitch-lh

define Language.Stitch.LH.Check.checkBindings (ctx : (Language.Stitch.LH.Data.List.List Language.Stitch.LH.Type.Ty),  e : Language.Stitch.LH.Check.Exp) : bool = {((if (is$Language.Stitch.LH.Check.Var e) then
                                                                                                                                                                      ((Language.Stitch.LH.Data.List.elemAt (lqdc##$select##Language.Stitch.LH.Check.Var##1 e) ctx) =
                                                                                                                                                                         (lqdc##$select##Language.Stitch.LH.Check.Var##2 e))
                                                                                                                                                                    else
                                                                                                                                                                      (if (is$Language.Stitch.LH.Check.Lam e) then
                                                                                                                                                                         (Language.Stitch.LH.Check.checkBindings (Language.Stitch.LH.Data.List.Cons (lqdc##$select##Language.Stitch.LH.Check.Lam##2 e) ctx) (lqdc##$select##Language.Stitch.LH.Check.Lam##1 e))
                                                                                                                                                                       else
                                                                                                                                                                         (if (is$Language.Stitch.LH.Check.App e) then
                                                                                                                                                                            && [(Language.Stitch.LH.Check.checkBindings ctx (Language.Stitch.LH.Check.e1 e));
                                                                                                                                                                                (Language.Stitch.LH.Check.checkBindings ctx (lqdc##$select##Language.Stitch.LH.Check.App##1 e))]
                                                                                                                                                                          else
                                                                                                                                                                            (if (is$Language.Stitch.LH.Check.Let e) then
                                                                                                                                                                               && [(Language.Stitch.LH.Check.checkBindings ctx (lqdc##$select##Language.Stitch.LH.Check.Let##2 e));
                                                                                                                                                                                   (Language.Stitch.LH.Check.checkBindings (Language.Stitch.LH.Data.List.Cons (Language.Stitch.LH.Check.exprType (lqdc##$select##Language.Stitch.LH.Check.Let##2 e)) ctx) (lqdc##$select##Language.Stitch.LH.Check.Let##1 e))]
                                                                                                                                                                             else
                                                                                                                                                                               (if (is$Language.Stitch.LH.Check.Arith e) then
                                                                                                                                                                                  && [(Language.Stitch.LH.Check.checkBindings ctx (lqdc##$select##Language.Stitch.LH.Check.Arith##3 e));
                                                                                                                                                                                      (Language.Stitch.LH.Check.checkBindings ctx (lqdc##$select##Language.Stitch.LH.Check.Arith##1 e))]
                                                                                                                                                                                else
                                                                                                                                                                                  (if (is$Language.Stitch.LH.Check.Cond e) then
                                                                                                                                                                                     && [(Language.Stitch.LH.Check.checkBindings ctx (lqdc##$select##Language.Stitch.LH.Check.Cond##3 e));
                                                                                                                                                                                         && [(Language.Stitch.LH.Check.checkBindings ctx (Language.Stitch.LH.Check.a e));
                                                                                                                                                                                             (Language.Stitch.LH.Check.checkBindings ctx (lqdc##$select##Language.Stitch.LH.Check.Cond##1 e))]]
                                                                                                                                                                                   else
                                                                                                                                                                                     (if (is$Language.Stitch.LH.Check.Fix e) then
                                                                                                                                                                                        (Language.Stitch.LH.Check.checkBindings ctx (lqdc##$select##Language.Stitch.LH.Check.Fix##1 e))
                                                                                                                                                                                      else
                                                                                                                                                                                        (if (is$Language.Stitch.LH.Check.IntE e) then
                                                                                                                                                                                           true
                                                                                                                                                                                         else
                                                                                                                                                                                           true)))))))))}

Reading the above expression improves a lot by disabling line wrapping in text editors. Still the longer lines could be broken further:

define Language.Stitch.LH.Check.checkBindings
  ( ctx : (Language.Stitch.LH.Data.List.List Language.Stitch.LH.Type.Ty)
  ,  e : Language.Stitch.LH.Check.Exp
  ) : bool = {
    (  (if (is$Language.Stitch.LH.Check.Var e) then
          (  (Language.Stitch.LH.Data.List.elemAt
                (lqdc##$select##Language.Stitch.LH.Check.Var##1 e)
                ctx
             )
             = (lqdc##$select##Language.Stitch.LH.Check.Var##2 e)
          )
       else
         (  if (is$Language.Stitch.LH.Check.Lam e) then
              (Language.Stitch.LH.Check.checkBindings
                (Language.Stitch.LH.Data.List.Cons
                  (lqdc##$select##Language.Stitch.LH.Check.Lam##2 e)
                  ctx
                )
                (lqdc##$select##Language.Stitch.LH.Check.Lam##1 e)
              )
            else
              (if (is$Language.Stitch.LH.Check.App e) then
                &&
                  [ (Language.Stitch.LH.Check.checkBindings ctx (Language.Stitch.LH.Check.e1 e))
                  ; (Language.Stitch.LH.Check.checkBindings ctx
                       (lqdc##$select##Language.Stitch.LH.Check.App##1 e)
                    )
                  ]
               else
                 (if (is$Language.Stitch.LH.Check.Let e) then
                    &&
                    [ (Language.Stitch.LH.Check.checkBindings ctx (lqdc##$select##Language.Stitch.LH.Check.Let##2 e))
                    ; (Language.Stitch.LH.Check.checkBindings
                        (Language.Stitch.LH.Data.List.Cons
                          (Language.Stitch.LH.Check.exprType
                            (lqdc##$select##Language.Stitch.LH.Check.Let##2 e)
                          )
                          ctx
                        )
                        (lqdc##$select##Language.Stitch.LH.Check.Let##1 e)
                      )
                    ]
                  else
                    (if (is$Language.Stitch.LH.Check.Arith e) then
                       &&
                       [ (Language.Stitch.LH.Check.checkBindings ctx (lqdc##$select##Language.Stitch.LH.Check.Arith##3 e))
                       ; (Language.Stitch.LH.Check.checkBindings ctx (lqdc##$select##Language.Stitch.LH.Check.Arith##1 e))
                       ]
                     else
                       (if (is$Language.Stitch.LH.Check.Cond e) then
                          &&
                          [ (Language.Stitch.LH.Check.checkBindings ctx (lqdc##$select##Language.Stitch.LH.Check.Cond##3 e))
                          ;  &&
                             [ (Language.Stitch.LH.Check.checkBindings ctx (Language.Stitch.LH.Check.a e))
                             ; (Language.Stitch.LH.Check.checkBindings
                                  ctx
                                  (lqdc##$select##Language.Stitch.LH.Check.Cond##1 e)
                               )
                             ]
                          ]
                        else
                           (if (is$Language.Stitch.LH.Check.Fix e) then
                              (Language.Stitch.LH.Check.checkBindings
                                 ctx
                                 (lqdc##$select##Language.Stitch.LH.Check.Fix##1 e)
                              )
                            else
                              (if (is$Language.Stitch.LH.Check.IntE e) then true else true)
                           )
                       )
                  )
               )
            )
         )
       )
    )
}

Improving readability further from there will probably require to give special formatting to nested if-then-else statements. But perhaps we can leave that for another issue.

Other declarations in the .fq files suffer of long lines too. It would be worth doing a pass over them to break them down.

@facundominguez
Copy link
Collaborator Author

facundominguez commented Apr 9, 2022

Here's a harder to read example from a debugging session using the function Language.Fixpoint.Types.PrettyPrint.showFix.

((ReWrite8.mbind (ReWrite8.monad (mp##a1GY  :  (ReWrite8.MonadPlus m##a1KI))) (lqdc##$select##ReWrite8.OurMonad##1 (ReWrite8.monad (mp##a1GY  :  (ReWrite8.MonadPlus m##a1KI))) (ReWrite8.guard (mp##a1GY  :  (ReWrite8.MonadPlus m##a1KI)) ((x##a1H0  :  int) <=
                                                                                                                                                                                                                                               (p##a1GZ  :  int))) (ReWrite8.const' (ReWrite8.mreturn (ReWrite8.monad (mp##a1GY  :  (ReWrite8.MonadPlus m##a1KI))) GHC.Tuple.$40$$41$))) (ReWrite8.const' (ReWrite8.mreturn (ReWrite8.monad (mp##a1GY  :  (ReWrite8.MonadPlus m##a1KI))) (GHC.Tuple.$40$$44$$41$ (GHC.Types.$58$ (x##a1H0  :  int) (lqdc$35$$35$$36$select$35$$35$GHC.Tuple.$40$$44$$41$$35$$35$1 (ds_d2Mv  :  (Tuple [int] [int])))) (lqdc$35$$35$$36$select$35$$35$GHC.Tuple.$40$$44$$41$$35$$35$2 (ds_d2Mv  :  (Tuple [int] [int]))))))) =
   (lqdc##$select##ReWrite8.OurMonad##1 (ReWrite8.monad (mp##a1GY  :  (ReWrite8.MonadPlus m##a1KI))) (lqdc##$select##ReWrite8.OurMonad##1 (ReWrite8.monad (mp##a1GY  :  (ReWrite8.MonadPlus m##a1KI))) (ReWrite8.guard (mp##a1GY  :  (ReWrite8.MonadPlus m##a1KI)) ((x##a1H0  :  int) <=
                                                                                                                                                                                                                                                                      (p##a1GZ  :  int))) (ReWrite8.const' (ReWrite8.mreturn (ReWrite8.monad (mp##a1GY  :  (ReWrite8.MonadPlus m##a1KI))) GHC.Tuple.$40$$41$))) (ReWrite8.const' (ReWrite8.mreturn (ReWrite8.monad (mp##a1GY  :  (ReWrite8.MonadPlus m##a1KI))) (GHC.Tuple.$40$$44$$41$ (GHC.Types.$58$ (x##a1H0  :  int) (lqdc$35$$35$$36$select$35$$35$GHC.Tuple.$40$$44$$41$$35$$35$1 (ds_d2Mv  :  (Tuple [int] [int])))) (lqdc$35$$35$$36$select$35$$35$GHC.Tuple.$40$$44$$41$$35$$35$2 (ds_d2Mv  :  (Tuple [int] [int]))))))))

And here's an hypothetical and more approachable rendering:

((ReWrite8.mbind
   (ReWrite8.monad (mp##a1GY  :  (ReWrite8.MonadPlus m##a1KI)))
   (lqdc##$select##ReWrite8.OurMonad##1
     (ReWrite8.monad (mp##a1GY  :  (ReWrite8.MonadPlus m##a1KI)))
	 (ReWrite8.guard
	   (mp##a1GY  :  (ReWrite8.MonadPlus m##a1KI))
	   ((x##a1H0  :  int) <= (p##a1GZ  :  int))
	 )
	 (ReWrite8.const'
	   (ReWrite8.mreturn
	     (ReWrite8.monad (mp##a1GY  :  (ReWrite8.MonadPlus m##a1KI)))
		 GHC.Tuple.$40$$41$)
	 )
   )
   (ReWrite8.const'
     (ReWrite8.mreturn
	   (ReWrite8.monad (mp##a1GY  :  (ReWrite8.MonadPlus m##a1KI)))
	   (GHC.Tuple.$40$$44$$41$
	     (GHC.Types.$58$
		   (x##a1H0  :  int)
		   (lqdc$35$$35$$36$select$35$$35$GHC.Tuple.$40$$44$$41$$35$$35$1 (ds_d2Mv  :  (Tuple [int] [int])))
		 )
		 (lqdc$35$$35$$36$select$35$$35$GHC.Tuple.$40$$44$$41$$35$$35$2 (ds_d2Mv  :  (Tuple [int] [int])))
	   )
	 )
   )
  ) =
  (lqdc##$select##ReWrite8.OurMonad##1
    (ReWrite8.monad
	  (mp##a1GY  :  (ReWrite8.MonadPlus m##a1KI))
	)
	(lqdc##$select##ReWrite8.OurMonad##1
	  (ReWrite8.monad (mp##a1GY  :  (ReWrite8.MonadPlus m##a1KI)))
	  (ReWrite8.guard
	    (mp##a1GY  :  (ReWrite8.MonadPlus m##a1KI))
		((x##a1H0  :  int) <= (p##a1GZ  :  int))
	  )
	  (ReWrite8.const'
	    (ReWrite8.mreturn
		  (ReWrite8.monad (mp##a1GY  :  (ReWrite8.MonadPlus m##a1KI)))
		  GHC.Tuple.$40$$41$
		)
	  )
	)
	(ReWrite8.const'
	  (ReWrite8.mreturn
	    (ReWrite8.monad (mp##a1GY  :  (ReWrite8.MonadPlus m##a1KI)))
		(GHC.Tuple.$40$$44$$41$
		  (GHC.Types.$58$
		    (x##a1H0  :  int)
			(lqdc$35$$35$$36$select$35$$35$GHC.Tuple.$40$$44$$41$$35$$35$1 (ds_d2Mv  :  (Tuple [int] [int])))
		  )
		  (lqdc$35$$35$$36$select$35$$35$GHC.Tuple.$40$$44$$41$$35$$35$2 (ds_d2Mv  :  (Tuple [int] [int])))
		)
            )
        )
    )
)

@ranjitjhala
Copy link
Member

ranjitjhala commented Apr 9, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants