summaryrefslogtreecommitdiff
path: root/src/LambdaCube
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-02-19 15:26:24 +0100
committerPéter Diviánszky <divipp@gmail.com>2016-02-19 15:26:24 +0100
commita287d5a79406ac9042c5ca9bbd6b52b192ca1eee (patch)
treee731090ef4f95ef7dbab282599b0b719fdcdd169 /src/LambdaCube
parenta4b6b7bae563d8d2cfdbb5afc3bde594dad7f77b (diff)
names have identifiers
Diffstat (limited to 'src/LambdaCube')
-rw-r--r--src/LambdaCube/Compiler/CoreToIR.hs2
-rw-r--r--src/LambdaCube/Compiler/Infer.hs308
2 files changed, 205 insertions, 105 deletions
diff --git a/src/LambdaCube/Compiler/CoreToIR.hs b/src/LambdaCube/Compiler/CoreToIR.hs
index 8c759ea4..29a2ab39 100644
--- a/src/LambdaCube/Compiler/CoreToIR.hs
+++ b/src/LambdaCube/Compiler/CoreToIR.hs
@@ -857,7 +857,7 @@ mkApp (ExpTV (Neut (I.App_ a b)) et vs) = Just (ExpTV (Neut a) t vs, head $ chai
857 where t = neutType' (mkEnv vs) a 857 where t = neutType' (mkEnv vs) a
858mkApp _ = Nothing 858mkApp _ = Nothing
859 859
860mkFunc r@(ExpTV (I.Func n def nt xs) ty vs) | all (supType . tyOf) (r: xs') && n `notElem` ["typeAnn"] && all validChar n 860mkFunc r@(ExpTV (I.Func (show -> n) def nt xs) ty vs) | all (supType . tyOf) (r: xs') && n `notElem` ["typeAnn"] && all validChar n
861 = Just (untick n, toExp (def, nt), tyOf r, xs') 861 = Just (untick n, toExp (def, nt), tyOf r, xs')
862 where 862 where
863 xs' = chain vs nt $ reverse xs 863 xs' = chain vs nt $ reverse xs
diff --git a/src/LambdaCube/Compiler/Infer.hs b/src/LambdaCube/Compiler/Infer.hs
index 13e18411..c593f607 100644
--- a/src/LambdaCube/Compiler/Infer.hs
+++ b/src/LambdaCube/Compiler/Infer.hs
@@ -28,9 +28,11 @@ module LambdaCube.Compiler.Infer
28 , nType, conType, neutType, neutType', appTy, mkConPars, makeCaseFunPars, makeCaseFunPars' 28 , nType, conType, neutType, neutType', appTy, mkConPars, makeCaseFunPars, makeCaseFunPars'
29 , MaxDB(..), unfixlabel 29 , MaxDB(..), unfixlabel
30 , ErrorMsg, showError 30 , ErrorMsg, showError
31 , FName (..)
31 ) where 32 ) where
32 33
33import Data.Monoid 34import Data.Monoid
35import Data.Char
34import Data.Maybe 36import Data.Maybe
35import Data.List 37import Data.List
36import qualified Data.Set as Set 38import qualified Data.Set as Set
@@ -72,31 +74,125 @@ data Neutral
72 | Delta (SData ([Exp] -> Exp)) -- not neut? 74 | Delta (SData ([Exp] -> Exp)) -- not neut?
73 deriving (Show) 75 deriving (Show)
74 76
75data ConName = ConName SName Int{-ordinal number, e.g. Zero:0, Succ:1-} Type 77data ConName = ConName FName Int{-ordinal number, e.g. Zero:0, Succ:1-} Type
76 78
77data TyConName = TyConName SName Int{-num of indices-} Type [(ConName, Type)]{-constructors-} CaseFunName 79data TyConName = TyConName FName Int{-num of indices-} Type [(ConName, Type)]{-constructors-} CaseFunName
78 80
79data FunName = FunName SName (Maybe Exp) Type 81data FunName = FunName FName (Maybe Exp) Type
80 82
81data CaseFunName = CaseFunName SName Type Int{-num of parameters-} 83data CaseFunName = CaseFunName FName Type Int{-num of parameters-}
82 84
83data TyCaseFunName = TyCaseFunName SName Type 85data TyCaseFunName = TyCaseFunName FName Type
84 86
85type Type = Exp 87type Type = Exp
86type ExpType = (Exp, Type) 88type ExpType = (Exp, Type)
87type SExp2 = SExp' ExpType 89type SExp2 = SExp' ExpType
88 90
89instance Show ConName where show (ConName n _ _) = n 91instance Show ConName where show (ConName n _ _) = show n
90instance Eq ConName where ConName _ n _ == ConName _ n' _ = n == n' 92instance Eq ConName where ConName _ n _ == ConName _ n' _ = n == n'
91instance Show TyConName where show (TyConName n _ _ _ _) = n 93instance Show TyConName where show (TyConName n _ _ _ _) = show n
92instance Eq TyConName where TyConName n _ _ _ _ == TyConName n' _ _ _ _ = n == n' 94instance Eq TyConName where TyConName n _ _ _ _ == TyConName n' _ _ _ _ = n == n'
93instance Show FunName where show (FunName n _ _) = n 95instance Show FunName where show (FunName n _ _) = show n
94instance Eq FunName where FunName n _ _ == FunName n' _ _ = n == n' 96instance Eq FunName where FunName n _ _ == FunName n' _ _ = n == n'
95instance Show CaseFunName where show (CaseFunName n _ _) = caseName n 97instance Show CaseFunName where show (CaseFunName n _ _) = CaseName $ show n
96instance Eq CaseFunName where CaseFunName n _ _ == CaseFunName n' _ _ = n == n' 98instance Eq CaseFunName where CaseFunName n _ _ == CaseFunName n' _ _ = n == n'
97instance Show TyCaseFunName where show (TyCaseFunName n _) = MatchName n 99instance Show TyCaseFunName where show (TyCaseFunName n _) = MatchName $ show n
98instance Eq TyCaseFunName where TyCaseFunName n _ == TyCaseFunName n' _ = n == n' 100instance Eq TyCaseFunName where TyCaseFunName n _ == TyCaseFunName n' _ = n == n'
99 101
102data FName
103 = CFName !Int (SData String)
104 | FVecScalar
105 | FEqCT | FT2 | Fcoe | FparEval | Ft2C | FprimFix
106 | FUnit
107 | FInt
108 | FWord
109 | FNat
110 | FBool
111 | FFloat
112 | FString
113 | FChar
114 | FOrdering
115 | FVecS
116 | FEmpty
117 | FHList
118 | FEq
119 | FOrd
120 | FNum
121 | FSigned
122 | FComponent
123 | FIntegral
124 | FFloating
125 | FOutput
126 | FType
127 | FHCons
128 | FHNil
129 | FZero
130 | FSucc
131 | FFalse
132 | FTrue
133 | FLT
134 | FGT
135 | FEQ
136 | FTT
137 | FNil
138 | FCons
139 | FSplit
140 deriving (Eq, Ord)
141
142-- todo: use module indentifier instead of hash
143cFName mod i (RangeSI (Range l _), s) = fromMaybe (CFName n $ SData s) $ lookup s fntable
144 where
145 n = hash (sourceName l) * 2^32 + sourceLine l * 2^16 + sourceColumn l * 2^3 -- + i
146 hash = foldr (\c x -> ord c + x*2) 0
147
148fntable =
149 [ (,) "'VecScalar" FVecScalar
150 , (,) "'EqCT" FEqCT
151 , (,) "'T2" FT2
152 , (,) "coe" Fcoe
153 , (,) "parEval" FparEval
154 , (,) "t2C" Ft2C
155 , (,) "primFix" FprimFix
156 , (,) "'Unit" FUnit
157 , (,) "'Int" FInt
158 , (,) "'Word" FWord
159 , (,) "'Nat" FNat
160 , (,) "'Bool" FBool
161 , (,) "'Float" FFloat
162 , (,) "'String" FString
163 , (,) "'Char" FChar
164 , (,) "'Ordering" FOrdering
165 , (,) "'VecS" FVecS
166 , (,) "'Empty" FEmpty
167 , (,) "'HList" FHList
168 , (,) "'Eq" FEq
169 , (,) "'Ord" FOrd
170 , (,) "'Num" FNum
171 , (,) "'Signed" FSigned
172 , (,) "'Component" FComponent
173 , (,) "'Integral" FIntegral
174 , (,) "'Floating" FFloating
175 , (,) "'Output" FOutput
176 , (,) "'Type" FType
177 , (,) "HCons" FHCons
178 , (,) "HNil" FHNil
179 , (,) "Zero" FZero
180 , (,) "Succ" FSucc
181 , (,) "False" FFalse
182 , (,) "True" FTrue
183 , (,) "LT" FLT
184 , (,) "GT" FGT
185 , (,) "EQ" FEQ
186 , (,) "TT" FTT
187 , (,) "Nil" FNil
188 , (,) "Cons" FCons
189 , (,) "'Split" FSplit
190 ]
191
192instance Show FName where
193 show (CFName _ (SData s)) = s
194 show s = fromMaybe (error "show") $ lookup s $ map (\(a, b) -> (b, a)) fntable
195
100-------------------------------------------------------------------------------- auxiliary functions and patterns 196-------------------------------------------------------------------------------- auxiliary functions and patterns
101 197
102infixl 2 `App`, `app_` 198infixl 2 `App`, `app_`
@@ -110,7 +206,6 @@ isNoLabelEnd _ = True
110mconcat1 [] = mempty 206mconcat1 [] = mempty
111mconcat1 [x] = x 207mconcat1 [x] = x
112mconcat1 xs = notClosed --foldl1 (<>) xs 208mconcat1 xs = notClosed --foldl1 (<>) xs
113mconcat1' = mconcat
114 209
115pattern Fun' f vs i xs n <- Fun_ _ f vs i xs n where Fun' f vs i xs n = Fun_ (mconcat1 $ map maxDB_ vs ++ map maxDB_ xs {- <> iterateN i lowerDB (maxDB_ n)-}) f vs i xs n 210pattern Fun' f vs i xs n <- Fun_ _ f vs i xs n where Fun' f vs i xs n = Fun_ (mconcat1 $ map maxDB_ vs ++ map maxDB_ xs {- <> iterateN i lowerDB (maxDB_ n)-}) f vs i xs n
116pattern Fun f i xs n = Fun' f [] i xs n 211pattern Fun f i xs n = Fun' f [] i xs n
@@ -161,29 +256,29 @@ pattern TTyCon0 s <- (unfixlabel -> TyCon (TyConName s _ TType _ _) [])
161tTyCon0 s cs = Closed $ TyCon (TyConName s 0 TType (map ((,) (error "tTyCon0")) cs) $ CaseFunName (error "TTyCon0-A") (error "TTyCon0-B") 0) [] 256tTyCon0 s cs = Closed $ TyCon (TyConName s 0 TType (map ((,) (error "tTyCon0")) cs) $ CaseFunName (error "TTyCon0-A") (error "TTyCon0-B") 0) []
162pattern a :~> b = Pi Visible a b 257pattern a :~> b = Pi Visible a b
163 258
164pattern Unit <- TTyCon0 "'Unit" where Unit = tTyCon0 "'Unit" [Unit] 259pattern Unit <- TTyCon0 FUnit where Unit = tTyCon0 FUnit [Unit]
165pattern TInt <- TTyCon0 "'Int" where TInt = tTyCon0 "'Int" $ error "cs 1" 260pattern TInt <- TTyCon0 FInt where TInt = tTyCon0 FInt $ error "cs 1"
166pattern TNat <- TTyCon0 "'Nat" where TNat = tTyCon0 "'Nat" $ error "cs 3" 261pattern TNat <- TTyCon0 FNat where TNat = tTyCon0 FNat $ error "cs 3"
167pattern TBool <- TTyCon0 "'Bool" where TBool = tTyCon0 "'Bool" $ error "cs 4" 262pattern TBool <- TTyCon0 FBool where TBool = tTyCon0 FBool $ error "cs 4"
168pattern TFloat <- TTyCon0 "'Float" where TFloat = tTyCon0 "'Float" $ error "cs 5" 263pattern TFloat <- TTyCon0 FFloat where TFloat = tTyCon0 FFloat $ error "cs 5"
169pattern TString <- TTyCon0 "'String" where TString = tTyCon0 "'String" $ error "cs 6" 264pattern TString <- TTyCon0 FString where TString = tTyCon0 FString $ error "cs 6"
170pattern TChar <- TTyCon0 "'Char" where TChar = tTyCon0 "'Char" $ error "cs 7" 265pattern TChar <- TTyCon0 FChar where TChar = tTyCon0 FChar $ error "cs 7"
171pattern TOrdering <- TTyCon0 "'Ordering" where TOrdering = tTyCon0 "'Ordering" $ error "cs 8" 266pattern TOrdering <- TTyCon0 FOrdering where TOrdering = tTyCon0 FOrdering $ error "cs 8"
172pattern TVec a b <- TyConN "'VecS" {-(TType :~> TNat :~> TType)-} [b, a] 267pattern TVec a b <- TyConN FVecS {-(TType :~> TNat :~> TType)-} [b, a]
173 268
174pattern Empty s <- TyCon (TyConName "'Empty" _ _ _ _) [EString s] where 269pattern Empty s <- TyCon (TyConName FEmpty _ _ _ _) [EString s] where
175 Empty s = TyCon (TyConName "'Empty" (error "todo: inum2_") (TString :~> TType) (error "todo: tcn cons 3_") $ error "Empty") [EString s] 270 Empty s = TyCon (TyConName FEmpty (error "todo: inum2_") (TString :~> TType) (error "todo: tcn cons 3_") $ error "Empty") [EString s]
176 271
177pattern TT <- ConN' _ _ where TT = Closed (tCon "TT" 0 Unit []) 272pattern TT <- ConN' _ _ where TT = Closed (tCon FTT 0 Unit [])
178pattern Zero <- ConN "Zero" _ where Zero = Closed (tCon "Zero" 0 TNat []) 273pattern Zero <- ConN FZero _ where Zero = Closed (tCon FZero 0 TNat [])
179pattern Succ n <- ConN "Succ" (n:_) where Succ n = tCon "Succ" 1 (TNat :~> TNat) [n] 274pattern Succ n <- ConN FSucc (n:_) where Succ n = tCon FSucc 1 (TNat :~> TNat) [n]
180 275
181pattern CstrT t a b = Neut (CstrT' t a b) 276pattern CstrT t a b = Neut (CstrT' t a b)
182pattern CstrT' t a b = TFun' "'EqCT" (TType :~> Var 0 :~> Var 1 :~> TType) [t, a, b] 277pattern CstrT' t a b = TFun' FEqCT (TType :~> Var 0 :~> Var 1 :~> TType) [t, a, b]
183pattern Coe a b w x = TFun "coe" (TType :~> TType :~> CstrT TType (Var 1) (Var 0) :~> Var 2 :~> Var 2) [a,b,w,x] 278pattern Coe a b w x = TFun Fcoe (TType :~> TType :~> CstrT TType (Var 1) (Var 0) :~> Var 2 :~> Var 2) [a,b,w,x]
184pattern ParEval t a b = TFun "parEval" (TType :~> Var 0 :~> Var 1 :~> Var 2) [t, a, b] 279pattern ParEval t a b = TFun FparEval (TType :~> Var 0 :~> Var 1 :~> Var 2) [t, a, b]
185pattern T2 a b = TFun "'T2" (TType :~> TType :~> TType) [a, b] 280pattern T2 a b = TFun FT2 (TType :~> TType :~> TType) [a, b]
186pattern CSplit a b c <- UFunN "'Split" [a, b, c] 281pattern CSplit a b c <- UFunN FSplit [a, b, c]
187 282
188pattern EInt a = ELit (LInt a) 283pattern EInt a = ELit (LInt a)
189pattern EFloat a = ELit (LFloat a) 284pattern EFloat a = ELit (LFloat a)
@@ -207,22 +302,22 @@ fromNatE' (unfixlabel -> Zero) = Just 0
207fromNatE' (unfixlabel -> Succ n) = (1 +) <$> fromNatE' n 302fromNatE' (unfixlabel -> Succ n) = (1 +) <$> fromNatE' n
208fromNatE' _ = Nothing 303fromNatE' _ = Nothing
209 304
210mkBool False = Closed $ tCon "False" 0 TBool [] 305mkBool False = Closed $ tCon FFalse 0 TBool []
211mkBool True = Closed $ tCon "True" 1 TBool [] 306mkBool True = Closed $ tCon FTrue 1 TBool []
212 307
213getEBool (unfixlabel -> ConN' 0 _) = Just False 308getEBool (unfixlabel -> ConN' 0 _) = Just False
214getEBool (unfixlabel -> ConN' 1 _) = Just True 309getEBool (unfixlabel -> ConN' 1 _) = Just True
215getEBool _ = Nothing 310getEBool _ = Nothing
216 311
217mkOrdering x = Closed $ case x of 312mkOrdering x = Closed $ case x of
218 LT -> tCon "LT" 0 TOrdering [] 313 LT -> tCon FLT 0 TOrdering []
219 EQ -> tCon "EQ" 1 TOrdering [] 314 EQ -> tCon FEQ 1 TOrdering []
220 GT -> tCon "GT" 2 TOrdering [] 315 GT -> tCon FGT 2 TOrdering []
221 316
222conTypeName :: ConName -> TyConName 317conTypeName :: ConName -> TyConName
223conTypeName (ConName _ _ t) = case snd $ getParams t of TyCon n _ -> n 318conTypeName (ConName _ _ t) = case snd $ getParams t of TyCon n _ -> n
224 319
225outputType = tTyCon0 "'Output" $ error "cs 9" 320outputType = tTyCon0 FOutput $ error "cs 9"
226boolType = TBool 321boolType = TBool
227trueExp = EBool True 322trueExp = EBool True
228 323
@@ -437,10 +532,10 @@ evalCaseFun a b x = error $ "evalCaseFun: " ++ show (a, x)
437 532
438evalTyCaseFun a b (FL c) = evalTyCaseFun a b c 533evalTyCaseFun a b (FL c) = evalTyCaseFun a b c
439evalTyCaseFun a b (Neut c) = TyCaseFun a b c 534evalTyCaseFun a b (Neut c) = TyCaseFun a b c
440evalTyCaseFun (TyCaseFunName "match'Type" ty) [_, t, f] TType = t 535evalTyCaseFun (TyCaseFunName FType ty) (_: t: f: _) TType = t
441evalTyCaseFun (TyCaseFunName n ty) [_, t, f] (TyCon (TyConName n' _ _ _ _) vs) | n == n' = foldl app_ t vs 536evalTyCaseFun (TyCaseFunName n ty) (_: t: f: _) (TyCon (TyConName n' _ _ _ _) vs) | n == n' = foldl app_ t vs
442--evalTyCaseFun (TyCaseFunName n ty) [_, t, f] (DFun (FunName n' _) vs) | n == n' = foldl app_ t vs -- hack 537--evalTyCaseFun (TyCaseFunName n ty) [_, t, f] (DFun (FunName n' _) vs) | n == n' = foldl app_ t vs -- hack
443evalTyCaseFun (TyCaseFunName n ty) [_, t, f] _ = f 538evalTyCaseFun (TyCaseFunName n ty) (_: t: f: _) _ = f
444 539
445evalCoe a b (FL x) d = evalCoe a b x d 540evalCoe a b (FL x) d = evalCoe a b x d
446evalCoe a b TT d = d 541evalCoe a b TT d = d
@@ -457,61 +552,64 @@ evalCoe a b t d = Coe a b t d
457-} 552-}
458 553
459getFunDef s f = case s of 554getFunDef s f = case s of
460 "unsafeCoerce" -> \case xs@[_, _, x] -> case x of x@FL{} -> x; Neut{} -> f xs; _ -> x 555 FEqCT -> \case (t: a: b: _) -> cstr t a b
461 "'EqCT" -> \case [t, a, b] -> cstr t a b 556 FT2 -> \case (a: b: _) -> t2 a b
462 "reflCstr" -> \case [a] -> TT 557 Ft2C -> \case (a: b: _) -> t2C a b
463 "coe" -> \case [a, b, t, d] -> evalCoe a b t d 558 Fcoe -> \case (a: b: t: d: _) -> evalCoe a b t d
464 "'T2" -> \case [a, b] -> t2 a b 559 FparEval -> \case (t: a: b: _) -> parEval t a b
465 "t2C" -> \case [a, b] -> t2C a b
466 "parEval" -> \case [t, a, b] -> parEval t a b
467 where 560 where
468 parEval _ (LabelEnd x) _ = LabelEnd x 561 parEval _ (LabelEnd x) _ = LabelEnd x
469 parEval _ _ (LabelEnd x) = LabelEnd x 562 parEval _ _ (LabelEnd x) = LabelEnd x
470 parEval t a b = ParEval t a b 563 parEval t a b = ParEval t a b
564 CFName _ (SData s) -> case s of
565 "unsafeCoerce" -> \case xs@(_: _: x: _) -> case x of x@FL{} -> x; Neut{} -> f xs; _ -> x
566 "reflCstr" -> \case (a: _) -> TT
471 567
472 "hlistNilCase" -> \case [_, x, unfixlabel -> Con n@(ConName _ 0 _) _ []] -> x; xs -> f xs 568 "hlistNilCase" -> \case (_: x: (unfixlabel -> Con n@(ConName _ 0 _) _ _): _) -> x; xs -> f xs
473 "hlistConsCase" -> \case [_, _, _, x, unfixlabel -> Con n@(ConName _ 1 _) _ [_, _, a, b]] -> x `app_` a `app_` b; xs -> f xs 569 "hlistConsCase" -> \case (_: _: _: x: (unfixlabel -> Con n@(ConName _ 1 _) _ (_: _: a: b: _)): _) -> x `app_` a `app_` b; xs -> f xs
474 570
475 -- general compiler primitives 571 -- general compiler primitives
476 "primAddInt" -> \case [EInt i, EInt j] -> EInt (i + j); xs -> f xs 572 "primAddInt" -> \case (EInt i: EInt j: _) -> EInt (i + j); xs -> f xs
477 "primSubInt" -> \case [EInt i, EInt j] -> EInt (i - j); xs -> f xs 573 "primSubInt" -> \case (EInt i: EInt j: _) -> EInt (i - j); xs -> f xs
478 "primModInt" -> \case [EInt i, EInt j] -> EInt (i `mod` j); xs -> f xs 574 "primModInt" -> \case (EInt i: EInt j: _) -> EInt (i `mod` j); xs -> f xs
479 "primSqrtFloat" -> \case [EFloat i] -> EFloat $ sqrt i; xs -> f xs 575 "primSqrtFloat" -> \case (EFloat i: _) -> EFloat $ sqrt i; xs -> f xs
480 "primRound" -> \case [EFloat i] -> EInt $ round i; xs -> f xs 576 "primRound" -> \case (EFloat i: _) -> EInt $ round i; xs -> f xs
481 "primIntToFloat" -> \case [EInt i] -> EFloat $ fromIntegral i; xs -> f xs 577 "primIntToFloat" -> \case (EInt i: _) -> EFloat $ fromIntegral i; xs -> f xs
482 "primIntToNat" -> \case [EInt i] -> ENat $ fromIntegral i; xs -> f xs 578 "primIntToNat" -> \case (EInt i: _) -> ENat $ fromIntegral i; xs -> f xs
483 "primCompareInt" -> \case [EInt x, EInt y] -> mkOrdering $ x `compare` y; xs -> f xs 579 "primCompareInt" -> \case (EInt x: EInt y: _) -> mkOrdering $ x `compare` y; xs -> f xs
484 "primCompareFloat" -> \case [EFloat x, EFloat y] -> mkOrdering $ x `compare` y; xs -> f xs 580 "primCompareFloat" -> \case (EFloat x: EFloat y: _) -> mkOrdering $ x `compare` y; xs -> f xs
485 "primCompareChar" -> \case [EChar x, EChar y] -> mkOrdering $ x `compare` y; xs -> f xs 581 "primCompareChar" -> \case (EChar x: EChar y: _) -> mkOrdering $ x `compare` y; xs -> f xs
486 "primCompareString" -> \case [EString x, EString y] -> mkOrdering $ x `compare` y; xs -> f xs 582 "primCompareString" -> \case (EString x: EString y: _) -> mkOrdering $ x `compare` y; xs -> f xs
487 583
488 -- LambdaCube 3D specific primitives 584 -- LambdaCube 3D specific primitives
489 "PrimGreaterThan" -> \case [t, _, _, _, _, _, _, x, y] | Just r <- twoOpBool (>) t x y -> r; xs -> f xs 585 "PrimGreaterThan" -> \case (t: _: _: _: _: _: _: x: y: _) | Just r <- twoOpBool (>) t x y -> r; xs -> f xs
490 "PrimGreaterThanEqual" -> \case [t, _, _, _, _, _, _, x, y] | Just r <- twoOpBool (>=) t x y -> r; xs -> f xs 586 "PrimGreaterThanEqual" -> \case (t: _: _: _: _: _: _: x: y: _) | Just r <- twoOpBool (>=) t x y -> r; xs -> f xs
491 "PrimLessThan" -> \case [t, _, _, _, _, _, _, x, y] | Just r <- twoOpBool (<) t x y -> r; xs -> f xs 587 "PrimLessThan" -> \case (t: _: _: _: _: _: _: x: y: _) | Just r <- twoOpBool (<) t x y -> r; xs -> f xs
492 "PrimLessThanEqual" -> \case [t, _, _, _, _, _, _, x, y] | Just r <- twoOpBool (<=) t x y -> r; xs -> f xs 588 "PrimLessThanEqual" -> \case (t: _: _: _: _: _: _: x: y: _) | Just r <- twoOpBool (<=) t x y -> r; xs -> f xs
493 "PrimEqualV" -> \case [t, _, _, _, _, _, _, x, y] | Just r <- twoOpBool (==) t x y -> r; xs -> f xs 589 "PrimEqualV" -> \case (t: _: _: _: _: _: _: x: y: _) | Just r <- twoOpBool (==) t x y -> r; xs -> f xs
494 "PrimNotEqualV" -> \case [t, _, _, _, _, _, _, x, y] | Just r <- twoOpBool (/=) t x y -> r; xs -> f xs 590 "PrimNotEqualV" -> \case (t: _: _: _: _: _: _: x: y: _) | Just r <- twoOpBool (/=) t x y -> r; xs -> f xs
495 "PrimEqual" -> \case [t, _, _, x, y] | Just r <- twoOpBool (==) t x y -> r; xs -> f xs 591 "PrimEqual" -> \case (t: _: _: x: y: _) | Just r <- twoOpBool (==) t x y -> r; xs -> f xs
496 "PrimNotEqual" -> \case [t, _, _, x, y] | Just r <- twoOpBool (/=) t x y -> r; xs -> f xs 592 "PrimNotEqual" -> \case (t: _: _: x: y: _) | Just r <- twoOpBool (/=) t x y -> r; xs -> f xs
497 "PrimSubS" -> \case [_, _, _, _, x, y] | Just r <- twoOp (-) x y -> r; xs -> f xs 593 "PrimSubS" -> \case (_: _: _: _: x: y: _) | Just r <- twoOp (-) x y -> r; xs -> f xs
498 "PrimSub" -> \case [_, _, x, y] | Just r <- twoOp (-) x y -> r; xs -> f xs 594 "PrimSub" -> \case (_: _: x: y: _) | Just r <- twoOp (-) x y -> r; xs -> f xs
499 "PrimAddS" -> \case [_, _, _, _, x, y] | Just r <- twoOp (+) x y -> r; xs -> f xs 595 "PrimAddS" -> \case (_: _: _: _: x: y: _) | Just r <- twoOp (+) x y -> r; xs -> f xs
500 "PrimAdd" -> \case [_, _, x, y] | Just r <- twoOp (+) x y -> r; xs -> f xs 596 "PrimAdd" -> \case (_: _: x: y: _) | Just r <- twoOp (+) x y -> r; xs -> f xs
501 "PrimMulS" -> \case [_, _, _, _, x, y] | Just r <- twoOp (*) x y -> r; xs -> f xs 597 "PrimMulS" -> \case (_: _: _: _: x: y: _) | Just r <- twoOp (*) x y -> r; xs -> f xs
502 "PrimMul" -> \case [_, _, x, y] | Just r <- twoOp (*) x y -> r; xs -> f xs 598 "PrimMul" -> \case (_: _: x: y: _) | Just r <- twoOp (*) x y -> r; xs -> f xs
503 "PrimDivS" -> \case [_, _, _, _, _, x, y] | Just r <- twoOp_ (/) div x y -> r; xs -> f xs 599 "PrimDivS" -> \case (_: _: _: _: _: x: y: _) | Just r <- twoOp_ (/) div x y -> r; xs -> f xs
504 "PrimDiv" -> \case [_, _, _, _, _, x, y] | Just r <- twoOp_ (/) div x y -> r; xs -> f xs 600 "PrimDiv" -> \case (_: _: _: _: _: x: y: _) | Just r <- twoOp_ (/) div x y -> r; xs -> f xs
505 "PrimModS" -> \case [_, _, _, _, _, x, y] | Just r <- twoOp_ modF mod x y -> r; xs -> f xs 601 "PrimModS" -> \case (_: _: _: _: _: x: y: _) | Just r <- twoOp_ modF mod x y -> r; xs -> f xs
506 "PrimMod" -> \case [_, _, _, _, _, x, y] | Just r <- twoOp_ modF mod x y -> r; xs -> f xs 602 "PrimMod" -> \case (_: _: _: _: _: x: y: _) | Just r <- twoOp_ modF mod x y -> r; xs -> f xs
507 "PrimNeg" -> \case [_, x] | Just r <- oneOp negate x -> r; xs -> f xs 603 "PrimNeg" -> \case (_: x: _) | Just r <- oneOp negate x -> r; xs -> f xs
508 "PrimAnd" -> \case [EBool x, EBool y] -> EBool (x && y); xs -> f xs 604 "PrimAnd" -> \case (EBool x: EBool y: _) -> EBool (x && y); xs -> f xs
509 "PrimOr" -> \case [EBool x, EBool y] -> EBool (x || y); xs -> f xs 605 "PrimOr" -> \case (EBool x: EBool y: _) -> EBool (x || y); xs -> f xs
510 "PrimXor" -> \case [EBool x, EBool y] -> EBool (x /= y); xs -> f xs 606 "PrimXor" -> \case (EBool x: EBool y: _) -> EBool (x /= y); xs -> f xs
511 "PrimNot" -> \case [TNat, _, _, EBool x] -> EBool $ not x; xs -> f xs 607 "PrimNot" -> \case (TNat: _: _: EBool x: _) -> EBool $ not x; xs -> f xs
512 608
513 _ -> f 609 _ -> f
514 610
611 _ -> f
612
515cstr = f [] 613cstr = f []
516 where 614 where
517 f z ty a a' = f_ z (unfixlabel ty) (unfixlabel a) (unfixlabel a') 615 f z ty a a' = f_ z (unfixlabel ty) (unfixlabel a) (unfixlabel a')
@@ -525,11 +623,11 @@ cstr = f []
525 f_ (_: ns) typ{-down?-} (down 0 -> Just a) (down 0 -> Just a') = f ns typ a a' 623 f_ (_: ns) typ{-down?-} (down 0 -> Just a) (down 0 -> Just a') = f ns typ a a'
526 f_ ns TType (Pi h a b) (Pi h' a' b') | h == h' = t2 (f ns TType a a') (f ((a, a'): ns) TType b b') 624 f_ ns TType (Pi h a b) (Pi h' a' b') | h == h' = t2 (f ns TType a a') (f ((a, a'): ns) TType b b')
527 625
528 f_ [] TType (UFunN "'VecScalar" [a, b]) (UFunN "'VecScalar" [a', b']) = t2 (f [] TNat a a') (f [] TType b b') 626 f_ [] TType (UFunN FVecScalar [a, b]) (UFunN FVecScalar [a', b']) = t2 (f [] TNat a a') (f [] TType b b')
529 f_ [] TType (UFunN "'VecScalar" [a, b]) (TVec a' b') = t2 (f [] TNat a a') (f [] TType b b') 627 f_ [] TType (UFunN FVecScalar [a, b]) (TVec a' b') = t2 (f [] TNat a a') (f [] TType b b')
530 f_ [] TType (UFunN "'VecScalar" [a, b]) t@(TTyCon0 n) | isElemTy n = t2 (f [] TNat a (ENat 1)) (f [] TType b t) 628 f_ [] TType (UFunN FVecScalar [a, b]) t@(TTyCon0 n) | isElemTy n = t2 (f [] TNat a (ENat 1)) (f [] TType b t)
531 f_ [] TType (TVec a' b') (UFunN "'VecScalar" [a, b]) = t2 (f [] TNat a' a) (f [] TType b' b) 629 f_ [] TType (TVec a' b') (UFunN FVecScalar [a, b]) = t2 (f [] TNat a' a) (f [] TType b' b)
532 f_ [] TType t@(TTyCon0 n) (UFunN "'VecScalar" [a, b]) | isElemTy n = t2 (f [] TNat a (ENat 1)) (f [] TType b t) 630 f_ [] TType t@(TTyCon0 n) (UFunN FVecScalar [a, b]) | isElemTy n = t2 (f [] TNat a (ENat 1)) (f [] TType b t)
533 631
534 f_ [] typ a@Neut{} a' = CstrT typ a a' 632 f_ [] typ a@Neut{} a' = CstrT typ a a'
535 f_ [] typ a a'@Neut{} = CstrT typ a a' 633 f_ [] typ a a'@Neut{} = CstrT typ a a'
@@ -544,10 +642,10 @@ cstr = f []
544 ff ns tt@(Pi v t _) ((t1, t2'): ts) = t2 (f ns t t1 t2') $ ff ns (appTy tt t1) ts 642 ff ns tt@(Pi v t _) ((t1, t2'): ts) = t2 (f ns t t1 t2') $ ff ns (appTy tt t1) ts
545 ff ns t zs = error $ "ff: " -- ++ show (a, n, length xs', length $ mkConPars n typ) ++ "\n" ++ ppShow (nType a) ++ "\n" ++ ppShow (foldl appTy (nType a) $ mkConPars n typ) ++ "\n" ++ ppShow (zip xs xs') ++ "\n" ++ ppShow zs ++ "\n" ++ ppShow t 643 ff ns t zs = error $ "ff: " -- ++ show (a, n, length xs', length $ mkConPars n typ) ++ "\n" ++ ppShow (nType a) ++ "\n" ++ ppShow (foldl appTy (nType a) $ mkConPars n typ) ++ "\n" ++ ppShow (zip xs xs') ++ "\n" ++ ppShow zs ++ "\n" ++ ppShow t
546 644
547 isElemTy n = n `elem` ["'Bool", "'Float", "'Int"] 645 isElemTy n = n `elem` [FBool, FFloat, FInt]
548 646
549t2C (unfixlabel -> TT) (unfixlabel -> TT) = TT 647t2C (unfixlabel -> TT) (unfixlabel -> TT) = TT
550t2C a b = TFun "t2C" (Unit :~> Unit :~> Unit) [a, b] 648t2C a b = TFun Ft2C (Unit :~> Unit :~> Unit) [a, b]
551 649
552t2 (unfixlabel -> Unit) a = a 650t2 (unfixlabel -> Unit) a = a
553t2 a (unfixlabel -> Unit) = a 651t2 a (unfixlabel -> Unit) = a
@@ -790,7 +888,7 @@ inferN_ tellTrace = infer where
790 = infer te $ x `SAppV` SLamV (STyped mempty (substTo (v+1) (Var 0) $ up 1 t, TType)) `SAppV` a `SAppV` b `SAppV` SVar siv v 888 = infer te $ x `SAppV` SLamV (STyped mempty (substTo (v+1) (Var 0) $ up 1 t, TType)) `SAppV` a `SAppV` b `SAppV` SVar siv v
791 -- temporal hack 889 -- temporal hack
792 | x@(SGlobal (si, CaseName "'VecS")) `SAppV` SLamV (SLamV (Wildcard _)) `SAppV` a `SAppV` b `SAppV` c `SAppV` SVar siv v <- e 890 | x@(SGlobal (si, CaseName "'VecS")) `SAppV` SLamV (SLamV (Wildcard _)) `SAppV` a `SAppV` b `SAppV` c `SAppV` SVar siv v <- e
793 , TyConN "'VecS" [_, Var n'] <- snd $ varType "xx" v te 891 , TyConN FVecS [_, Var n'] <- snd $ varType "xx" v te
794 = infer te $ x `SAppV` SLamV (SLamV (STyped mempty (substTo (n'+2) (Var 1) $ up 2 t, TType))) `SAppV` a `SAppV` b `SAppV` c `SAppV` SVar siv v 892 = infer te $ x `SAppV` SLamV (SLamV (STyped mempty (substTo (n'+2) (Var 1) $ up 2 t, TType))) `SAppV` a `SAppV` b `SAppV` c `SAppV` SVar siv v
795 893
796{- 894{-
@@ -961,7 +1059,7 @@ replaceMetas bind = \case
961 1059
962 1060
963isCstr CstrT{} = True 1061isCstr CstrT{} = True
964isCstr (UFunN s [_]) = s `elem` ["'Eq", "'Ord", "'Num", "'CNum", "'Signed", "'Component", "'Integral", "'NumComponent", "'Floating"] -- todo: use Constraint type to decide this 1062isCstr (UFunN s [_]) = s `elem` [FEq, FOrd, FNum, FSigned, FComponent, FIntegral, FFloating] -- todo: use Constraint type to decide this
965isCstr _ = {- trace_ (ppShow c ++ show c) $ -} False 1063isCstr _ = {- trace_ (ppShow c ++ show c) $ -} False
966 1064
967-------------------------------------------------------------------------------- re-checking 1065-------------------------------------------------------------------------------- re-checking
@@ -1117,12 +1215,14 @@ listTypeInfos m = map (second Set.toList) $ Map.toList $ Map.unionsWith (<>) [Ma
1117 1215
1118-------------------------------------------------------------------------------- inference for statements 1216-------------------------------------------------------------------------------- inference for statements
1119 1217
1218modn = 0
1219
1120handleStmt :: MonadFix m => [Stmt] -> Stmt -> IM m GlobalEnv 1220handleStmt :: MonadFix m => [Stmt] -> Stmt -> IM m GlobalEnv
1121handleStmt defs = \case 1221handleStmt defs = \case
1122 Primitive n (trSExp' -> t_) -> do 1222 Primitive n (trSExp' -> t_) -> do
1123 t <- inferType =<< ($ t_) <$> addF 1223 t <- inferType =<< ($ t_) <$> addF
1124 tellType (fst n) t 1224 tellType (fst n) t
1125 addToEnv n $ flip (,) t $ lamify t $ Neut . DFun_ (FunName (snd n) Nothing t) 1225 addToEnv n $ flip (,) t $ lamify t $ Neut . DFun_ (FunName (cFName modn 0 n) Nothing t)
1126 Let n mt t_ -> do 1226 Let n mt t_ -> do
1127 af <- addF 1227 af <- addF
1128 let t__ = maybe id (flip SAnn . af) mt t_ 1228 let t__ = maybe id (flip SAnn . af) mt t_
@@ -1156,7 +1256,7 @@ handleStmt defs = \case
1156 let pars = zipWith (\x -> second $ STyped (debugSI "mkConstr1") . flip (,) TType . up_ (1+j) x) [0..] $ drop (length ps) $ fst $ getParams cty 1256 let pars = zipWith (\x -> second $ STyped (debugSI "mkConstr1") . flip (,) TType . up_ (1+j) x) [0..] $ drop (length ps) $ fst $ getParams cty
1157 act = length . fst . getParams $ cty 1257 act = length . fst . getParams $ cty
1158 acts = map fst . fst . getParams $ cty 1258 acts = map fst . fst . getParams $ cty
1159 conn = ConName (snd cn) j cty 1259 conn = ConName (cFName modn 1 cn) j cty
1160 e <- addToEnv cn (Con conn 0 [], cty) 1260 e <- addToEnv cn (Con conn 0 [], cty)
1161 return (e, ((conn, cty) 1261 return (e, ((conn, cty)
1162 , addParamsS pars 1262 , addParamsS pars
@@ -1170,8 +1270,8 @@ handleStmt defs = \case
1170 SPi Visible (apps' (SGlobal s) $ zip (map fst ps) (downToS "a6" inum $ length ps) ++ zip (map fst $ fst $ getParamsS t_) (downToS "a7" 0 inum)) SType 1270 SPi Visible (apps' (SGlobal s) $ zip (map fst ps) (downToS "a6" inum $ length ps) ++ zip (map fst $ fst $ getParamsS t_) (downToS "a7" 0 inum)) SType
1171 1271
1172 (e1, es, tcn, cfn@(CaseFunName _ ct _), _, _) <- mfix $ \ ~(_, _, _, _, ct', cons') -> do 1272 (e1, es, tcn, cfn@(CaseFunName _ ct _), _, _) <- mfix $ \ ~(_, _, _, _, ct', cons') -> do
1173 let cfn = CaseFunName (snd s) ct' $ length ps 1273 let cfn = CaseFunName (cFName modn 2 s) ct' $ length ps
1174 let tcn = TyConName (snd s) inum vty (map fst cons') cfn 1274 let tcn = TyConName (cFName modn 3 s) inum vty (map fst cons') cfn
1175 e1 <- addToEnv s (TyCon tcn [], vty) 1275 e1 <- addToEnv s (TyCon tcn [], vty)
1176 (unzip -> (mconcat -> es, cons)) <- withEnv e1 $ zipWithM mkConstr [0..] cs 1276 (unzip -> (mconcat -> es, cons)) <- withEnv e1 $ zipWithM mkConstr [0..] cs
1177 ct <- withEnv (e1 <> es) $ inferType 1277 ct <- withEnv (e1 <> es) $ inferType
@@ -1193,7 +1293,7 @@ handleStmt defs = \case
1193 :~> TType 1293 :~> TType
1194 :~> Var 2 `app_` Var 0 1294 :~> Var 2 `app_` Var 0
1195 :~> Var 3 `app_` Var 1 1295 :~> Var 3 `app_` Var 1
1196 e3 <- addToEnv (fst s, MatchName (snd s)) (lamify t $ \[m, tr, n, f] -> evalTyCaseFun (TyCaseFunName (snd s) t) [m, tr, f] n, t) 1296 e3 <- addToEnv (fst s, MatchName (snd s)) (lamify t $ \[m, tr, n, f] -> evalTyCaseFun (TyCaseFunName (cFName modn 4 s) t) [m, tr, f] n, t)
1197 return (e1 <> e2 <> e3 <> es) 1297 return (e1 <> e2 <> e3 <> es)
1198 1298
1199 stmt -> error $ "handleStmt: " ++ show stmt 1299 stmt -> error $ "handleStmt: " ++ show stmt
@@ -1203,12 +1303,12 @@ withEnv e = local $ second (<> e)
1203mkELet n x xt = {-(if null vs then id else trace_ $ "mkELet " ++ show (length vs) ++ " " ++ show n)-} term 1303mkELet n x xt = {-(if null vs then id else trace_ $ "mkELet " ++ show (length vs) ++ " " ++ show n)-} term
1204 where 1304 where
1205 vs = [Var i | i <- Set.toList $ free x <> free xt] 1305 vs = [Var i | i <- Set.toList $ free x <> free xt]
1206 fn = FunName (snd n) (Just x) xt 1306 fn = FunName (cFName modn 5 n) (Just x) xt
1207 1307
1208 term = pmLabel fn vs 0 [] $ getFix x 0 1308 term = pmLabel fn vs 0 [] $ getFix x 0
1209 1309
1210 getFix (Lam z) i = Lam $ getFix z (i+1) 1310 getFix (Lam z) i = Lam $ getFix z (i+1)
1211 getFix (TFun "primFix" _ [t, Lam f]) i = (if null vs then id else trace_ "!local rec") $ subst 0 (foldl app_ term (downTo 0 i)) f 1311 getFix (TFun FprimFix _ [t, Lam f]) i = (if null vs then id else trace_ "!local rec") $ subst 0 (foldl app_ term (downTo 0 i)) f
1212 getFix x _ = x 1312 getFix x _ = x
1213 1313
1214 1314
@@ -1373,7 +1473,7 @@ instance MkDoc Exp where
1373 (getTTup -> Just xs) -> shTuple <$> mapM f xs 1473 (getTTup -> Just xs) -> shTuple <$> mapM f xs
1374 (getTup -> Just xs) -> shTuple <$> mapM f xs 1474 (getTup -> Just xs) -> shTuple <$> mapM f xs
1375 Con s _ xs -> foldl (shApp Visible) (shAtom_ $ show s) <$> mapM f xs 1475 Con s _ xs -> foldl (shApp Visible) (shAtom_ $ show s) <$> mapM f xs
1376 TyConN s xs -> foldl (shApp Visible) (shAtom_ s) <$> mapM f xs 1476 TyConN s xs -> foldl (shApp Visible) (shAtom_ $ show s) <$> mapM f xs
1377 TType -> pure $ shAtom "Type" 1477 TType -> pure $ shAtom "Type"
1378 ELit l -> pure $ shAtom $ show l 1478 ELit l -> pure $ shAtom $ show l
1379 Neut x -> mkDoc ts x 1479 Neut x -> mkDoc ts x
@@ -1406,15 +1506,15 @@ instance MkDoc (CEnv Exp) where
1406 Meta a b -> join $ shLam True BMeta <$> mkDoc ts a <*> pure (f b) 1506 Meta a b -> join $ shLam True BMeta <$> mkDoc ts a <*> pure (f b)
1407 Assign i (x, _) e -> shLet i (mkDoc ts x) (f e) 1507 Assign i (x, _) e -> shLet i (mkDoc ts x) (f e)
1408 1508
1409getTup (unfixlabel -> ConN "HCons" [_, _, x, xs]) = (x:) <$> getTup xs 1509getTup (unfixlabel -> ConN FHCons [_, _, x, xs]) = (x:) <$> getTup xs
1410getTup (unfixlabel -> ConN "HNil" []) = Just [] 1510getTup (unfixlabel -> ConN FHNil []) = Just []
1411getTup _ = Nothing 1511getTup _ = Nothing
1412 1512
1413getTTup (unfixlabel -> TyConN "'HList" [xs]) = getList xs 1513getTTup (unfixlabel -> TyConN FHList [xs]) = getList xs
1414getTTup _ = Nothing 1514getTTup _ = Nothing
1415 1515
1416getList (unfixlabel -> ConN "Cons" [x, xs]) = (x:) <$> getList xs 1516getList (unfixlabel -> ConN FCons [x, xs]) = (x:) <$> getList xs
1417getList (unfixlabel -> ConN "Nil" []) = Just [] 1517getList (unfixlabel -> ConN FNil []) = Just []
1418getList _ = Nothing 1518getList _ = Nothing
1419 1519
1420 1520