Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. {-# LANGUAGE LambdaCase, EmptyCase
  2. , EmptyDataDecls, TypeOperators, LiberalTypeSynonyms,
  3. ExistentialQuantification, GADTSyntax, GADTs
  4.  
  5. , StandaloneDeriving
  6.  
  7. , InstanceSigs, FlexibleContexts, MultiParamTypeClasses,
  8. FlexibleInstances , TypeSynonymInstances , FunctionalDependencies
  9.  
  10. , TypeFamilies, TypeFamilyDependencies, DataKinds, PolyKinds,
  11. NoStarIsType , ConstraintKinds, QuantifiedConstraints , RankNTypes
  12.  
  13. , ExplicitForAll, KindSignatures, ScopedTypeVariables,
  14. TypeApplications, ImplicitParams #-}
  15.  
  16. {-# LANGUAGE TemplateHaskell #-}
  17. {-# LANGUAGE UnicodeSyntax #-}
  18.  
  19. -- {-# LANGUAGE AllowAmbiguousTypes #-}
  20. {-# LANGUAGE UndecidableInstances #-}
  21. {-# LANGUAGE UndecidableSuperClasses #-}
  22.  
  23. module Implicit () where
  24.  
  25. import Data.Kind
  26. import Data.Singletons.TH
  27. import Data.Constraint
  28.  
  29. -- | Define the natural numbers recursively and do all the singletons/promotion
  30. -- stuff to it:
  31. $(singletons [d|
  32. data Nat :: Type where
  33. Z :: Nat
  34. S :: Nat -> Nat
  35. |])
  36.  
  37. -- | The family of the types of finite sets.
  38. data Fin :: Nat -> Type where
  39. FZ :: Fin (S n)
  40. FS :: Fin n -> Fin (S n)
  41. -- For whatever reason, `singletons` can't singletonize GADTs like `Fin`; in
  42. -- case it's useful, see the bottom of this file for explicit implementations of
  43. -- it.
  44.  
  45. data family (x :: a) !< (y :: a) :: Type
  46. data instance (m :: Nat) !< (n :: Nat) where
  47. LTZ :: Z !< S n
  48. LTS :: m !< n -> S m !< S n
  49.  
  50. type family Super (c :: Constraint) :: Constraint
  51.  
  52. class Super (x &< y) => (x :: a) &< (y :: a) where
  53. lt_proof :: x !< y
  54.  
  55. type instance Super (Z &< S n) = ()
  56. instance Z &< S n where
  57. lt_proof = LTZ
  58.  
  59. type instance Super (S m &< S n) = (m &< n)
  60. instance m &< n => S m &< S n where
  61. lt_proof = LTS lt_proof
  62.  
  63. -- Doesn't work:
  64. inj :: ∀ (m :: Nat) (n :: Nat). (m &< n) => SNat m -> SNat n -> Fin n
  65. inj SZ (SS _) = FZ -- Fine; the `Z &< S n` instance gets the job done
  66. inj (SS n') (SS m') = FS (inj n' m') -- Everything breaks; the proof is uninferrable
  67. -- We get:
  68. {-
  69. • Could not deduce (n1 &< n2) arising from a use of ‘inj’
  70. from the context: m &< n
  71. bound by the type signature for:
  72. inj :: forall (m :: Nat) (n :: Nat).
  73. (m &< n) =>
  74. SNat m -> SNat n -> Fin n
  75. at Implicit.hs:56:1-69
  76. or from: m ~ 'S n1
  77. bound by a pattern with constructor:
  78. SS :: forall (n :: Nat). Sing n -> Sing ('S n),
  79. in an equation for ‘inj’
  80. at Implicit.hs:58:6-10
  81. or from: n ~ 'S n2
  82. bound by a pattern with constructor:
  83. SS :: forall (n :: Nat). Sing n -> Sing ('S n),
  84. in an equation for ‘inj’
  85. at Implicit.hs:58:14-18
  86. • In the first argument of ‘FS’, namely ‘(inj n' m')’
  87. In the expression: FS (inj n' m')
  88. In an equation for ‘inj’: inj (SS n') (SS m') = FS (inj n' m')
  89. |
  90. 58 | inj (SS n') (SS m') = FS (inj n' m') -- Everything breaks; the proof is uninferrable
  91. | ^^^^^^^^^
  92. -}
  93.  
  94. _LTD'' ∷ S m !< S n → m !< n
  95. _LTD'' (LTS lt) = lt
  96.  
  97. -- I think this is the real heart of the problem?
  98. _LTD' ∷ Dict (S m &< S n) → Dict (m &< n)
  99. _LTD' Dict = undefined
  100.  
  101. -- But how would you even use this?
  102. _LTD ∷ (S m &< S n) :- (m &< n)
  103. _LTD = unmapDict _LTD'
  104.  
  105. --------------------------------------------------------------------------------
  106. -- In case it's useful, here are singleton types and whatnot for `Fin`. (For
  107. -- reasons I don't understand, `singletons` can't automatically generate these
  108. -- for GADTs like `Fin`. Since everything below works, I guess `singletons`
  109. -- usually does more than this, and it's one or more of those other things that
  110. -- aren't possible.)
  111.  
  112. data instance Sing (_fn :: Fin n) where
  113. SFZ :: Sing FZ
  114. SFS :: Sing fn -> Sing (FS fn)
  115.  
  116. type SFin = (Sing :: Fin n -> Type)
  117.  
  118. instance SingI FZ where
  119. sing = SFZ
  120.  
  121. instance SingI fn => SingI (FS fn) where
  122. sing = SFS (sing :: Sing fn)
  123.  
  124. instance SingKind (Fin n) where
  125. type instance Demote (Fin n) = Fin n
  126. fromSing SFZ = FZ
  127. fromSing (SFS n) = FS (fromSing n)
  128. toSing FZ = SomeSing SFZ
  129. toSing (FS n) = case toSing n of
  130. SomeSing sfn -> SomeSing (SFS sfn)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement