summaryrefslogtreecommitdiff
path: root/src/LambdaCube/Compiler/Core.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/LambdaCube/Compiler/Core.hs')
-rw-r--r--src/LambdaCube/Compiler/Core.hs60
1 files changed, 36 insertions, 24 deletions
diff --git a/src/LambdaCube/Compiler/Core.hs b/src/LambdaCube/Compiler/Core.hs
index 377cd164..9fc61888 100644
--- a/src/LambdaCube/Compiler/Core.hs
+++ b/src/LambdaCube/Compiler/Core.hs
@@ -216,13 +216,14 @@ pattern TyConN s a <- TyCon (TyConName s _ _ _ _) a
216pattern TTyCon s t a <- TyCon (TyConName s _ t _ _) a 216pattern TTyCon s t a <- TyCon (TyConName s _ t _ _) a
217pattern TFun' a t b = DFun (FunName' a t) b 217pattern TFun' a t b = DFun (FunName' a t) b
218 218
219 219pattern TTyCon0 s <- TyCon (TyConName s _ _ _ _) []
220pattern TTyCon0 s <- TyCon (TyConName s _ TType _ _) []
221 220
222tTyCon s t a cs = TyCon (TyConName s (error "todo: inum") t (map ((,) (error "tTyCon")) cs) $ CaseFunName (error "TTyCon-A") (error "TTyCon-B") $ length a) a 221tTyCon s t a cs = TyCon (TyConName s (error "todo: inum") t (map ((,) (error "tTyCon")) cs) $ CaseFunName (error "TTyCon-A") (error "TTyCon-B") $ length a) a
223tTyCon0 s cs = Closed $ TyCon (TyConName s 0 TType (map ((,) (error "tTyCon0")) cs) $ CaseFunName (error "TTyCon0-A") (error "TTyCon0-B") 0) [] 222tTyCon0 s cs = Closed $ TyCon (TyConName s 0 TType (map ((,) (error "tTyCon0")) cs) $ CaseFunName (error "TTyCon0-A") (error "TTyCon0-B") 0) []
223
224pattern a :~> b = Pi Visible a b 224pattern a :~> b = Pi Visible a b
225 225
226pattern TConstraint <- TTyCon0 FConstraint where TConstraint = tTyCon0 FConstraint $ error "cs 1"
226pattern Unit <- TTyCon0 FUnit where Unit = tTyCon0 FUnit [Unit] 227pattern Unit <- TTyCon0 FUnit where Unit = tTyCon0 FUnit [Unit]
227pattern TInt <- TTyCon0 FInt where TInt = tTyCon0 FInt $ error "cs 1" 228pattern TInt <- TTyCon0 FInt where TInt = tTyCon0 FInt $ error "cs 1"
228pattern TNat <- TTyCon0 FNat where TNat = tTyCon0 FNat $ error "cs 3" 229pattern TNat <- TTyCon0 FNat where TNat = tTyCon0 FNat $ error "cs 3"
@@ -233,8 +234,8 @@ pattern TChar <- TTyCon0 FChar where TChar = tTyCon0 FChar $ er
233pattern TOrdering <- TTyCon0 FOrdering where TOrdering = tTyCon0 FOrdering $ error "cs 8" 234pattern TOrdering <- TTyCon0 FOrdering where TOrdering = tTyCon0 FOrdering $ error "cs 8"
234pattern TVec a b <- TyConN FVecS [b, a] 235pattern TVec a b <- TyConN FVecS [b, a]
235 236
236pattern Empty s <- TyCon (TyConName FEmpty _ _ _ _) [HString{-hnf?-} s] where 237pattern Empty s <- TyCon (TyConName FEmpty _ _ _ _) [HString{-hnf?-} s]
237 Empty s = TyCon (TyConName FEmpty (error "todo: inum2_") (TString :~> TType) (error "todo: tcn cons 3_") $ error "Empty") [HString s] 238 where Empty s = TyCon (TyConName FEmpty (error "todo: inum2_") (TString :~> TType) (error "todo: tcn cons 3_") $ error "Empty") [HString s]
238 239
239pattern TT <- ConN' _ _ 240pattern TT <- ConN' _ _
240 where TT = Closed (tCon FTT 0 Unit []) 241 where TT = Closed (tCon FTT 0 Unit [])
@@ -243,11 +244,17 @@ pattern Zero <- ConN FZero _
243pattern Succ n <- ConN FSucc (n:_) 244pattern Succ n <- ConN FSucc (n:_)
244 where Succ n = tCon FSucc 1 (TNat :~> TNat) [n] 245 where Succ n = tCon FSucc 1 (TNat :~> TNat) [n]
245 246
247pattern CUnit <- ConN FCUnit _
248 where CUnit = tCon FCUnit 0 TConstraint []
249pattern CEmpty s <- ConN FCEmpty (HString s: _)
250 where CEmpty s = tCon FCEmpty 1 (TString :~> TConstraint) [HString s]
251
246pattern CstrT t a b = Neut (CstrT' t a b) 252pattern CstrT t a b = Neut (CstrT' t a b)
247pattern CstrT' t a b = TFun' FEqCT (TType :~> Var 0 :~> Var 1 :~> TType) [t, a, b] 253pattern CstrT' t a b = TFun' FEqCT (TType :~> Var 0 :~> Var 1 :~> TConstraint) [t, a, b]
248pattern Coe a b w x = TFun Fcoe (TType :~> TType :~> CstrT TType (Var 1) (Var 0) :~> Var 2 :~> Var 2) [a,b,w,x] 254pattern Coe a b w x = TFun Fcoe (TType :~> TType :~> CW (CstrT TType (Var 1) (Var 0)) :~> Var 2 :~> Var 2) [a,b,w,x]
249pattern ParEval t a b = TFun FparEval (TType :~> Var 0 :~> Var 1 :~> Var 2) [t, a, b] 255pattern ParEval t a b = TFun FparEval (TType :~> Var 0 :~> Var 1 :~> Var 2) [t, a, b]
250pattern T2 a b = TFun FT2 (TType :~> TType :~> TType) [a, b] 256pattern T2 a b = TFun FT2 (TConstraint :~> TConstraint :~> TConstraint) [a, b]
257pattern CW a = TFun FCW (TConstraint :~> TType) [a]
251pattern CSplit a b c <- UFunN FSplit [a, b, c] 258pattern CSplit a b c <- UFunN FSplit [a, b, c]
252 259
253pattern HLit a <- (hnf -> ELit a) 260pattern HLit a <- (hnf -> ELit a)
@@ -422,7 +429,7 @@ instance Up Exp where
422 Pi _ a b -> foldVar f i a <> foldVar f (i+1) b 429 Pi _ a b -> foldVar f i a <> foldVar f (i+1) b
423 Con _ _ as -> foldMap (foldVar f i) as 430 Con _ _ as -> foldMap (foldVar f i) as
424 TyCon _ as -> foldMap (foldVar f i) as 431 TyCon _ as -> foldMap (foldVar f i) as
425 TType -> mempty 432 TType_ _ -> mempty
426 ELit{} -> mempty 433 ELit{} -> mempty
427 Neut x -> foldVar f i x 434 Neut x -> foldVar f i x
428 Delta -> mempty 435 Delta -> mempty
@@ -451,7 +458,7 @@ instance HasMaxDB Exp where
451 Con_ c _ _ _ -> c 458 Con_ c _ _ _ -> c
452 TyCon_ c _ _ -> c 459 TyCon_ c _ _ -> c
453 460
454 TType -> mempty 461 TType_ _ -> mempty
455 ELit{} -> mempty 462 ELit{} -> mempty
456 Neut x -> maxDB_ x 463 Neut x -> maxDB_ x
457 Delta -> mempty 464 Delta -> mempty
@@ -484,7 +491,7 @@ instance ClosedExp Exp where
484 Pi_ _ a b c -> Pi_ mempty a (closedExp b) c 491 Pi_ _ a b c -> Pi_ mempty a (closedExp b) c
485 Con_ _ a b c -> Con_ mempty a b (closedExp <$> c) 492 Con_ _ a b c -> Con_ mempty a b (closedExp <$> c)
486 TyCon_ _ a b -> TyCon_ mempty a (closedExp <$> b) 493 TyCon_ _ a b -> TyCon_ mempty a (closedExp <$> b)
487 e@TType{} -> e 494 e@TType_{} -> e
488 e@ELit{} -> e 495 e@ELit{} -> e
489 Neut a -> Neut $ closedExp a 496 Neut a -> Neut $ closedExp a
490 Delta -> Delta 497 Delta -> Delta
@@ -574,6 +581,7 @@ pattern FunName' a t <- FunName a _ t
574getFunDef s f = case show s of 581getFunDef s f = case show s of
575 "'EqCT" -> \case (t: a: b: _) -> cstr t a b 582 "'EqCT" -> \case (t: a: b: _) -> cstr t a b
576 "'T2" -> \case (a: b: _) -> t2 a b 583 "'T2" -> \case (a: b: _) -> t2 a b
584 "'CW" -> \case (a: _) -> cw a
577 "t2C" -> \case (a: b: _) -> t2C a b 585 "t2C" -> \case (a: b: _) -> t2C a b
578 "coe" -> \case (a: b: t: d: _) -> evalCoe a b t d 586 "coe" -> \case (a: b: t: d: _) -> evalCoe a b t d
579 "parEval" -> \case (t: a: b: _) -> parEval t a b 587 "parEval" -> \case (t: a: b: _) -> parEval t a b
@@ -672,13 +680,13 @@ evalCoe a b (Reduced x) d = evalCoe a b x d
672evalCoe a b TT d = d 680evalCoe a b TT d = d
673evalCoe a b t d = Coe a b t d 681evalCoe a b t d = Coe a b t d
674 682
675 683cstr_ t a b = cw $ cstr t a b
676 684
677cstr = f [] 685cstr = f []
678 where 686 where
679 f z ty a a' = f_ z (hnf ty) (hnf a) (hnf a') 687 f z ty a a' = f_ z (hnf ty) (hnf a) (hnf a')
680 688
681 f_ _ _ a a' | a == a' = Unit 689 f_ _ _ a a' | a == a' = CUnit
682 f_ ns typ (RHS a) (RHS a') = f ns typ a a' 690 f_ ns typ (RHS a) (RHS a') = f ns typ a a'
683 f_ ns typ (Con a n xs) (Con a' n' xs') | a == a' && n == n' && length xs == length xs' = 691 f_ ns typ (Con a n xs) (Con a' n' xs') | a == a' && n == n' && length xs == length xs' =
684 ff ns (foldl appTy (conType typ a) $ mkConPars n typ) $ zip xs xs' 692 ff ns (foldl appTy (conType typ a) $ mkConPars n typ) $ zip xs xs'
@@ -687,17 +695,17 @@ cstr = f []
687 f_ (_: ns) typ{-down?-} (down 0 -> Just a) (down 0 -> Just a') = f ns typ a a' 695 f_ (_: ns) typ{-down?-} (down 0 -> Just a) (down 0 -> Just a') = f ns typ a a'
688 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') 696 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')
689 697
690 f_ [] TType (hnf -> UFunN FVecScalar [a, b]) (hnf -> UFunN FVecScalar [a', b']) = t2 (f [] TNat a a') (f [] TType b b') 698 f_ [] TType (UFunN FVecScalar [a, b]) (UFunN FVecScalar [a', b']) = t2 (f [] TNat a a') (f [] TType b b')
691 f_ [] TType (hnf -> UFunN FVecScalar [a, b]) (TVec a' b') = t2 (f [] TNat a a') (f [] TType b b') 699 f_ [] TType (UFunN FVecScalar [a, b]) (TVec a' b') = t2 (f [] TNat a a') (f [] TType b b')
692 f_ [] TType (hnf -> UFunN FVecScalar [a, b]) t@NonNeut = t2 (f [] TNat a (ENat 1)) (f [] TType b t) 700 f_ [] TType (UFunN FVecScalar [a, b]) t@NonNeut = t2 (f [] TNat a (ENat 1)) (f [] TType b t)
693 f_ [] TType (TVec a' b') (hnf -> UFunN FVecScalar [a, b]) = t2 (f [] TNat a' a) (f [] TType b' b) 701 f_ [] TType (TVec a' b') (UFunN FVecScalar [a, b]) = t2 (f [] TNat a' a) (f [] TType b' b)
694 f_ [] TType t@NonNeut (hnf -> UFunN FVecScalar [a, b]) = t2 (f [] TNat a (ENat 1)) (f [] TType b t) 702 f_ [] TType t@NonNeut (UFunN FVecScalar [a, b]) = t2 (f [] TNat a (ENat 1)) (f [] TType b t)
695 703
696 f_ [] typ a@Neut{} a' = CstrT typ a a' 704 f_ [] typ a@Neut{} a' = CstrT typ a a'
697 f_ [] typ a a'@Neut{} = CstrT typ a a' 705 f_ [] typ a a'@Neut{} = CstrT typ a a'
698 f_ ns typ a a' = Empty $ unlines [ "can not unify", ppShow a, "with", ppShow a' ] 706 f_ ns typ a a' = CEmpty $ unlines [ "can not unify", ppShow a, "with", ppShow a' ]
699 707
700 ff _ _ [] = Unit 708 ff _ _ [] = CUnit
701 ff ns tt@(Pi v t _) ((t1, t2'): ts) = t2 (f ns t t1 t2') $ ff ns (appTy tt t1) ts 709 ff ns tt@(Pi v t _) ((t1, t2'): ts) = t2 (f ns t t1 t2') $ ff ns (appTy tt t1) ts
702 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 710 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
703 711
@@ -710,11 +718,15 @@ nonNeut _ = True
710t2C (hnf -> TT) (hnf -> TT) = TT 718t2C (hnf -> TT) (hnf -> TT) = TT
711t2C a b = TFun Ft2C (Unit :~> Unit :~> Unit) [a, b] 719t2C a b = TFun Ft2C (Unit :~> Unit :~> Unit) [a, b]
712 720
713t2 (hnf -> Unit) a = a 721cw (hnf -> CUnit) = Unit
714t2 a (hnf -> Unit) = a 722cw (hnf -> CEmpty a) = Empty a
715t2 (hnf -> Empty a) (hnf -> Empty b) = Empty (a <> b) 723cw a = CW a
716t2 (hnf -> Empty s) _ = Empty s 724
717t2 _ (hnf -> Empty s) = Empty s 725t2 (hnf -> CUnit) a = a
726t2 a (hnf -> CUnit) = a
727t2 (hnf -> CEmpty a) (hnf -> CEmpty b) = CEmpty (a <> b)
728t2 (hnf -> CEmpty s) _ = CEmpty s
729t2 _ (hnf -> CEmpty s) = CEmpty s
718t2 a b = T2 a b 730t2 a b = T2 a b
719 731
720app_ :: Exp -> Exp -> Exp 732app_ :: Exp -> Exp -> Exp