11{-# LANGUAGE GADTs #-}
2- {-# LANGUAGE KindSignatures #-}
3- {-# LANGUAGE QuantifiedConstraints #-}
4- {-# LANGUAGE ScopedTypeVariables #-}
5- {-# LANGUAGE TypeOperators #-}
6- {-# LANGUAGE UndecidableInstances #-}
72module Analysis.Syntax
83( -- * Syntax
94 Term (.. )
@@ -18,36 +13,30 @@ module Analysis.Syntax
1813, Vec (.. )
1914) where
2015
21- import Control.Monad ( guard )
22- import Data.Kind ( Type )
16+ import Data.Foldable ( toList )
17+ import Data.Functor.Classes
2318import qualified Data.Set as Set
24- import Data.Typeable
25- import Unsafe.Coerce (unsafeCoerce )
2619
2720-- Syntax
2821
2922-- | (Currently) untyped term representations.
30- data Term ( sig :: Type -> Type ) v where
23+ data Term sig v where
3124 Var :: v -> Term sig v
32- (:$:) :: KnownNat n => sig n -> Vec n (Term sig v ) -> Term sig v
25+ (:$:) :: sig n -> Vec n (Term sig v ) -> Term sig v
3326
34- instance (forall n . Eq ( sig n ) , Eq v ) => Eq (Term sig v ) where
27+ instance (Eq1 sig , Eq v ) => Eq (Term sig v ) where
3528 Var v1 == Var v2 = v1 == v2
36- a :$: as == b :$: bs = case sameNat a b of
37- Just Refl -> a == b && as == bs
38- _ -> False
29+ a :$: as == b :$: bs = liftEq (\ _ _ -> True ) a b && toList as == toList bs
3930 _ == _ = False
4031
41- instance (forall n . Ord ( sig n ) , Ord v ) => Ord (Term sig v ) where
32+ instance (Ord1 sig , Ord v ) => Ord (Term sig v ) where
4233 compare (Var v1) (Var v2) = compare v1 v2
4334 compare (Var _) _ = LT
44- compare (a :$: as) (b :$: bs) = case sameNat a b of
45- Just Refl -> compare a b <> compare as bs
46- _ -> reifyNat a `compare` reifyNat b -- lol
35+ compare (a :$: as) (b :$: bs) = liftCompare (\ _ _ -> EQ ) a b <> compare (toList as) (toList bs)
4736 compare _ _ = GT
4837
4938
50- subterms :: (forall n . Ord ( sig n ) , Ord v ) => Term sig v -> Set. Set (Term sig v )
39+ subterms :: (Ord1 sig , Ord v ) => Term sig v -> Set. Set (Term sig v )
5140subterms t = case t of
5241 Var _ -> Set. singleton t
5342 _ :$: ts -> Set. insert t (foldMap subterms ts)
@@ -64,22 +53,6 @@ type N2 = S N1
6453type N3 = S N2
6554
6655
67- -- | Reify 'Nat's back from type-level singletons.
68- class KnownNat n where
69- reifyNat :: proxy n -> Int
70-
71- instance KnownNat Z where
72- reifyNat _ = 0
73-
74- instance KnownNat n => KnownNat (S n ) where
75- reifyNat _ = 1 + reifyNat (undefined :: proxy n )
76-
77-
78- -- | Test the equality of type-level 'Nat's at runtime, generating a type-level equality if equal.
79- sameNat :: forall a b proxy1 proxy2 . (KnownNat a , KnownNat b ) => proxy1 a -> proxy2 b -> Maybe (a :~: b )
80- sameNat a b = unsafeCoerce Refl <$ guard (reifyNat a == reifyNat b)
81-
82-
8356-- FIXME: move this into its own module, or use a dependency, or something.
8457data Vec n a where
8558 Nil :: Vec Z a
0 commit comments