Skip to content

Commit

Permalink
[ prelude ] Make able to implement provably total showPrec recursively
Browse files Browse the repository at this point in the history
  • Loading branch information
buzden authored and gallais committed Dec 26, 2023
1 parent 82bb12c commit 0e831ed
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 3 additions & 3 deletions libs/prelude/Prelude/Show.idr
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ interface Show ty where

||| Surround a `String` with parentheses depending on a condition.
||| @ b whether to add parentheses
export
public export
showParens : (b : Bool) -> String -> String
showParens False s = s
showParens True s = "(" ++ s ++ ")"
Expand All @@ -83,7 +83,7 @@ showParens True s = "(" ++ s ++ ")"
||| Show a => Show (Ann a) where
||| showPrec d (MkAnn s x) = showCon d "MkAnn" $ showArg s ++ showArg x
||| ```
export
public export
showCon : (d : Prec) -> (conName : String) -> (shownArgs : String) -> String
showCon d conName shownArgs = showParens (d >= App) (conName ++ shownArgs)

Expand All @@ -92,7 +92,7 @@ showCon d conName shownArgs = showParens (d >= App) (conName ++ shownArgs)
|||
||| This adds a space to the front so the results can be directly concatenated.
||| See `showCon` for details and an example.
export
public export %tcinline
showArg : Show a => (x : a) -> String
showArg x = " " ++ showPrec App x

Expand Down
12 changes: 12 additions & 0 deletions tests/idris2/total/total020/Check.idr
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
%default total

--- Functor-related ---

data X : Type -> Type -> Type where
B : b -> X a b
R : X a b -> X b c -> X a c
Expand Down Expand Up @@ -29,3 +31,13 @@ data X : Type -> Type -> Type where
foldMap f rlr@(R l r) = do
let r' = map @{WithFunction} f r
concat $ assert_smaller rlr r'

--- Show-related ---

data Tree : Type -> Type where
Leaf : a -> Tree a
Node : a -> (left, right : Tree a) -> Tree a

Show a => Show (Tree a) where
showPrec d (Leaf x) = showCon d "Leaf" $ showArg x
showPrec d (Node x l r) = showCon d "Node" $ showArg x ++ showArg l ++ showArg r

0 comments on commit 0e831ed

Please sign in to comment.