1 {-# LANGUAGE UndecidableInstances #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3 {-# OPTIONS_GHC -fconstraint-solver-iterations=5 #-}
4 -- | Symantic for @if@.
5 module Language.Symantic.Lib.If where
8 import qualified Data.Text as Text
9 import Data.Type.Equality ((:~:)(Refl))
11 import Language.Symantic.Parsing
12 import Language.Symantic.Parsing.Grammar
13 import Language.Symantic.Typing
14 import Language.Symantic.Compiling
15 import Language.Symantic.Interpreting
16 import Language.Symantic.Transforming.Trans
17 import Language.Symantic.Lib.Lambda
20 class Sym_If term where
21 if_ :: term Bool -> term a -> term a -> term a
22 default if_ :: Trans t term => t term Bool -> t term a -> t term a -> t term a
27 type instance Sym_of_Iface (Proxy If) = Sym_If
28 type instance Consts_of_Iface (Proxy If) = Consts_imported_by If
29 type instance Consts_imported_by If = '[ Proxy Bool ]
31 instance Sym_If HostI where
32 if_ (HostI b) ok ko = if b then ok else ko
33 instance Sym_If TextI where
34 if_ (TextI cond) (TextI ok) (TextI ko) =
38 [ "if ", cond (op, L) v
39 , " then ", ok (op, L) v
40 , " else ", ko (op, L) v ]
42 instance (Sym_If r1, Sym_If r2) => Sym_If (DupI r1 r2) where
43 if_ = dupI3 (Proxy @Sym_If) if_
46 ( Read_TypeNameR Type_Name cs rs
47 ) => Read_TypeNameR Type_Name cs (Proxy If ': rs) where
48 read_typenameR _rs = read_typenameR (Proxy @rs)
49 instance Show_Const cs => Show_Const (Proxy If ': cs) where
50 show_const ConstZ{} = "If"
51 show_const (ConstS c) = show_const c
53 instance Proj_ConC cs (Proxy If)
54 data instance TokenT meta (ts::[*]) (Proxy If)
55 = Token_Term_If_if (EToken meta ts) (EToken meta ts)
56 deriving instance Eq_Token meta ts => Eq (TokenT meta ts (Proxy If))
57 deriving instance Show_Token meta ts => Show (TokenT meta ts (Proxy If))
59 ( Inj_Const (Consts_of_Ifaces is) Bool
60 , Inj_Const (Consts_of_Ifaces is) (->)
62 ) => CompileI is (Proxy If) where
65 Token_Term_If_if tok_cond tok_ok ->
66 -- if :: Bool -> a -> a -> a
67 compileO tok_cond ctx $ \ty_cond (TermO cond) ->
68 compileO tok_ok ctx $ \ty_ok (TermO ok) ->
70 (At Nothing (ty @Bool))
71 (At (Just tok_cond) ty_cond) $ \Refl ->
72 k (ty_ok ~> ty_ok) $ TermO $
73 \c -> lam $ if_ (cond c) (ok c)
75 -- Inj_Token meta ts If =>
76 TokenizeT meta ts (Proxy If)
77 instance Gram_Term_AtomsT meta ts (Proxy If) g -- TODO