1 {-# LANGUAGE UndecidableInstances #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3 -- | Symantic for '[]'.
4 module Language.Symantic.Lib.List where
6 import Data.Semigroup ((<>))
7 import Prelude hiding (zipWith)
8 import qualified Data.Functor as Functor
9 import qualified Data.List as List
10 import qualified Data.MonoTraversable as MT
11 import qualified Data.Sequences as Seqs
12 import qualified Data.Text as Text
13 import qualified Data.Traversable as Traversable
15 import Language.Symantic
16 import Language.Symantic.Grammar as G
17 import Language.Symantic.Lib.Function (a0, b1, c2)
18 import Language.Symantic.Lib.MonoFunctor (Element)
21 type instance Sym [] = Sym_List
22 class Sym_List term where
23 list_empty :: term [a]
24 list_cons :: term a -> term [a] -> term [a]; infixr 5 `list_cons`
25 list :: [term a] -> term [a]
26 zipWith :: term (a -> b -> c) -> term [a] -> term [b] -> term [c]
28 default list_empty :: Sym_List (UnT term) => Trans term => term [a]
29 default list_cons :: Sym_List (UnT term) => Trans term => term a -> term [a] -> term [a]
30 default list :: Sym_List (UnT term) => Trans term => [term a] -> term [a]
31 default zipWith :: Sym_List (UnT term) => Trans term => term (a -> b -> c) -> term [a] -> term [b] -> term [c]
33 list_empty = trans list_empty
34 list_cons = trans2 list_cons
35 list l = trans (list (unTrans Functor.<$> l))
36 zipWith = trans3 zipWith
39 instance Sym_List Eval where
40 list_empty = return []
42 list = Traversable.sequence
43 zipWith = eval3 List.zipWith
44 instance Sym_List View where
45 list_empty = View $ \_p _v -> "[]"
46 list_cons = viewInfix ":" (infixR 5)
47 list l = View $ \_po v ->
48 "[" <> Text.intercalate ", " ((\(View a) -> a op v) Functor.<$> l) <> "]"
49 where op = (infixN0, SideL)
50 zipWith = view3 "zipWith"
51 instance (Sym_List r1, Sym_List r2) => Sym_List (Dup r1 r2) where
52 list_empty = dup0 @Sym_List list_empty
53 list_cons = dup2 @Sym_List list_cons
56 foldr (\(x1 `Dup` x2) (xs1, xs2) ->
57 (x1:xs1, x2:xs2)) ([], []) l in
59 zipWith = dup3 @Sym_List zipWith
62 instance (Sym_List term, Sym_Lambda term) => Sym_List (BetaT term)
65 instance FixityOf [] where
66 instance ClassInstancesFor [] where
67 proveConstraintFor _ (TyApp _ (TyConst _ _ q) z)
68 | Just HRefl <- proj_ConstKiTy @_ @[] z
70 _ | Just Refl <- proj_Const @Applicative q -> Just Dict
71 | Just Refl <- proj_Const @Foldable q -> Just Dict
72 | Just Refl <- proj_Const @Functor q -> Just Dict
73 | Just Refl <- proj_Const @Monad q -> Just Dict
74 | Just Refl <- proj_Const @Traversable q -> Just Dict
76 proveConstraintFor _ (TyApp _ tq@(TyConst _ _ q) (TyApp _ z a))
77 | Just HRefl <- proj_ConstKiTy @_ @[] z
79 _ | Just Refl <- proj_Const @Eq q
80 , Just Dict <- proveConstraint (tq `tyApp` a) -> Just Dict
81 | Just Refl <- proj_Const @Monoid q -> Just Dict
82 | Just Refl <- proj_Const @Show q
83 , Just Dict <- proveConstraint (tq `tyApp` a) -> Just Dict
84 | Just Refl <- proj_Const @Ord q
85 , Just Dict <- proveConstraint (tq `tyApp` a) -> Just Dict
86 | Just Refl <- proj_Const @MT.MonoFoldable q -> Just Dict
87 | Just Refl <- proj_Const @MT.MonoFunctor q -> Just Dict
88 | Just Refl <- proj_Const @Seqs.IsSequence q -> Just Dict
89 | Just Refl <- proj_Const @Seqs.SemiSequence q -> Just Dict
91 proveConstraintFor _c _q = Nothing
92 instance TypeInstancesFor [] where
93 expandFamFor _c _len f ((TyApp _ z a) `TypesS` TypesZ)
94 | Just HRefl <- proj_ConstKi @_ @Element f
95 , Just HRefl <- proj_ConstKiTy @_ @[] z
97 expandFamFor _c _len _fam _as = Nothing
106 ) => Gram_Term_AtomsFor src ss g [] where
108 [ rule "teList_list" $
109 between (symbol "[") (symbol "]") listG
110 , rule "teList_empty" $
112 (\src -> BinTree0 $ Token_Term $ TermAVT teList_empty `setSource` src)
117 listG :: CF g (AST_Term src ss)
118 listG = rule "list" $
122 Just b -> BinTree2 (BinTree2 (BinTree0 $ Token_Term $ TermAVT $ (`setSource` src) $ teList_cons) a) b
125 (BinTree2 (BinTree0 $ Token_Term $ TermAVT $ (`setSource` src) $ teList_cons) a)
126 (BinTree0 $ Token_Term $ TermAVT $ (`setSource` src) $ teList_empty))
128 <*> option Nothing (Just <$ symbol "," <*> listG)
129 instance (Source src, Inj_Sym ss []) => ModuleFor src ss [] where
130 moduleFor = ["List"] `moduleWhere`
131 [ "[]" := teList_empty
132 , "zipWith" := teList_zipWith
133 , ":" `withInfixR` 5 := teList_cons
137 tyList :: Source src => Inj_Len vs => Type src vs a -> Type src vs [a]
138 tyList = (tyConst @(K []) @[] `tyApp`)
141 teList_empty :: Source src => Inj_Sym ss [] => Term src ss ts '[Proxy a] (() #> [a])
142 teList_empty = Term noConstraint (tyList a0) $ teSym @[] $ list_empty
144 teList_cons :: Source src => Inj_Sym ss [] => Term src ss ts '[Proxy a] (() #> (a -> [a] -> [a]))
145 teList_cons = Term noConstraint (a0 ~> tyList a0 ~> tyList a0) $ teSym @[] $ lam2 list_cons
147 teList_zipWith :: Source src => Inj_Sym ss [] => Term src ss ts '[Proxy a, Proxy b, Proxy c] (() #> ((a -> b -> c) -> [a] -> [b] -> [c]))
148 teList_zipWith = Term noConstraint ((a0 ~> b1 ~> c2) ~> tyList a0 ~> tyList b1 ~> tyList c2) $ teSym @[] $ lam3 zipWith