Skip to content

Commit

Permalink
allow datypes with indices
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelineVQ committed Jun 9, 2020
1 parent 20dda18 commit 471467d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
47 changes: 22 additions & 25 deletions src/Language/Elab/Deriving/Show.idr
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ intercalate_str sep ss = fastAppend (intersperse sep ss)
-- actually used, since otherwise users have to provide it.
showClaim : (opname : Name) -> TypeInfo -> Visibility -> Elab Decl
showClaim op tyinfo vis = do
let varnames = map (show . argName) (pullExplicits tyinfo)
ty = appTyCon varnames
tysig = `( ~(ty) -> String )
let conargs = pullExplicits tyinfo
varnames = map (show . argName) (filter (isType . argType) conargs)
tysig = `( ~(appTyCon (map (show . argName) conargs)) -> String )
autoimps = foldr (\v,tt => `(Show ~(iBindVar v) => ~(tt))) tysig varnames
pure $ iClaim MW vis [] (mkTy op autoimps)
where
Expand Down Expand Up @@ -108,14 +108,16 @@ deriveShow vis n = do
fun <- pure $ mapName ("show" ++) n -- create a human readable function name
cons <- getCons name

r <- pure $ `[ data Foo : Type -> Nat -> Type where ]
logDecls 1 "beb" r


tyinfo <- makeTypeInfo name
c <- showClaim fun tyinfo vis

cs <- traverse (showCon fun tyinfo) cons
let g = IDef EmptyFC fun cs
logDecls 1 "fefa" [c]
logDecls 1 "fofo" [g]


declare [c,g]


Expand All @@ -137,13 +139,6 @@ data Foo4 : Type -> Type -> Type where
data Foo5 : Type -> Type -> Type -> Type where
Bor5 : a -> b -> c -> Foo5 a b c

data Foo6 : Type -> Type -> Type -> Type where
Zor6 : a -> b -> Foo6 a b c
Gor6 : b -> Foo6 a b c
Nor6A : a -> b -> c -> Foo6 a b c
Nor6B : a -> (0 _ : b) -> c -> Foo6 a b c -- 0 Use args are skipped
Bor6 : Foo6 a b c

-- NB c is never used, so Show shouldn't be required for it
data Foo7 : Type -> Type -> Type -> Type where
Zor7 : a -> Foo7 a b c
Expand All @@ -158,8 +153,20 @@ data Foo7' : Type -> Type -> Type -> Type where
Nor7' : b -> c -> Foo7' a b c
Bor7' : Foo7' a b c

forfo : Show (Foo6 a b c)
forfo = ?forfo_rhs
data Foo6 : Type -> Type -> Type -> Nat -> Type where
Zor6 : a -> b -> Foo6 a b c Z
Gor6 : b -> Foo6 a b c (S k)
Nor6A : a -> b -> c -> Foo6 a b c n
Nor6B : a -> (0 _ : b) -> c -> Foo6 a b c n -- 0 Use args are skipped
Bor6 : Foo6 a b c n

-- reference impl
showFoo6' : (Show a, Show b, Show c) => Foo6 a b c n -> String
showFoo6' (Zor6 x y) = "Zor6" ++ show x ++ show y
showFoo6' (Gor6 x) = "Gor6" ++ show x
showFoo6' (Nor6A x y z) = "Nor6A" ++ show x ++ show y ++ show z
showFoo6' (Nor6B x _ z) = "Nor6B" ++ show x ++ "_0" ++ show z -- skip 0 use?
showFoo6' (Bor6) = "Bor6"

-- %runElab deriveShow Export `{{Foo}}
%runElab deriveShow Export `{{Foo2}}
Expand All @@ -173,13 +180,3 @@ forfo = ?forfo_rhs
%runElab deriveShow Private `{{Foo6}}
%runElab deriveShow Private `{{Foo7}}
%runElab deriveShow Private `{{Foo7'}}
-- showFoo6 (Nor6B 'c' 'd' 'e')

-- reference impl
showFoo6' : (Show a, Show b, Show c) => Foo6 a b c -> String
showFoo6' (Zor6 x y) = "Zor6" ++ show x ++ show y
showFoo6' (Gor6 x) = "Gor6" ++ show x
showFoo6' (Nor6A x y z) = "Nor6A" ++ show x ++ show y ++ show z
showFoo6' (Nor6B x _ z) = "Nor6B" ++ show x ++ "_0" ++ show z -- skip 0 use?
showFoo6' (Bor6) = "Bor6"

12 changes: 11 additions & 1 deletion src/Language/Elab/Syntax.idr
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ export
logDecls : Nat -> String -> List Decl -> Elab ()
logDecls n s d = logTerm n s $ ILocal EmptyFC d `( () )

-----------------------------
-- Predicates
-- Replace these with something better down the road, even just Eq?
-----------------------------

export
isUse0 : Count -> Bool
isUse0 M0 = True
Expand All @@ -172,4 +177,9 @@ isUse1 _ = False
export
isUseW : Count -> Bool
isUseW MW = True
isUseW _ = False
isUseW _ = False

export
isType : TTImp -> Bool
isType (IType _) = True
isType _ = False

0 comments on commit 471467d

Please sign in to comment.