diff options
Diffstat (limited to 'src/LambdaCube/Compiler/Core.hs')
-rw-r--r-- | src/LambdaCube/Compiler/Core.hs | 60 |
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 | |||
216 | pattern TTyCon s t a <- TyCon (TyConName s _ t _ _) a | 216 | pattern TTyCon s t a <- TyCon (TyConName s _ t _ _) a |
217 | pattern TFun' a t b = DFun (FunName' a t) b | 217 | pattern TFun' a t b = DFun (FunName' a t) b |
218 | 218 | ||
219 | 219 | pattern TTyCon0 s <- TyCon (TyConName s _ _ _ _) [] | |
220 | pattern TTyCon0 s <- TyCon (TyConName s _ TType _ _) [] | ||
221 | 220 | ||
222 | tTyCon 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 | 221 | tTyCon 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 |
223 | tTyCon0 s cs = Closed $ TyCon (TyConName s 0 TType (map ((,) (error "tTyCon0")) cs) $ CaseFunName (error "TTyCon0-A") (error "TTyCon0-B") 0) [] | 222 | tTyCon0 s cs = Closed $ TyCon (TyConName s 0 TType (map ((,) (error "tTyCon0")) cs) $ CaseFunName (error "TTyCon0-A") (error "TTyCon0-B") 0) [] |
223 | |||
224 | pattern a :~> b = Pi Visible a b | 224 | pattern a :~> b = Pi Visible a b |
225 | 225 | ||
226 | pattern TConstraint <- TTyCon0 FConstraint where TConstraint = tTyCon0 FConstraint $ error "cs 1" | ||
226 | pattern Unit <- TTyCon0 FUnit where Unit = tTyCon0 FUnit [Unit] | 227 | pattern Unit <- TTyCon0 FUnit where Unit = tTyCon0 FUnit [Unit] |
227 | pattern TInt <- TTyCon0 FInt where TInt = tTyCon0 FInt $ error "cs 1" | 228 | pattern TInt <- TTyCon0 FInt where TInt = tTyCon0 FInt $ error "cs 1" |
228 | pattern TNat <- TTyCon0 FNat where TNat = tTyCon0 FNat $ error "cs 3" | 229 | pattern TNat <- TTyCon0 FNat where TNat = tTyCon0 FNat $ error "cs 3" |
@@ -233,8 +234,8 @@ pattern TChar <- TTyCon0 FChar where TChar = tTyCon0 FChar $ er | |||
233 | pattern TOrdering <- TTyCon0 FOrdering where TOrdering = tTyCon0 FOrdering $ error "cs 8" | 234 | pattern TOrdering <- TTyCon0 FOrdering where TOrdering = tTyCon0 FOrdering $ error "cs 8" |
234 | pattern TVec a b <- TyConN FVecS [b, a] | 235 | pattern TVec a b <- TyConN FVecS [b, a] |
235 | 236 | ||
236 | pattern Empty s <- TyCon (TyConName FEmpty _ _ _ _) [HString{-hnf?-} s] where | 237 | pattern 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 | ||
239 | pattern TT <- ConN' _ _ | 240 | pattern 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 _ | |||
243 | pattern Succ n <- ConN FSucc (n:_) | 244 | pattern 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 | ||
247 | pattern CUnit <- ConN FCUnit _ | ||
248 | where CUnit = tCon FCUnit 0 TConstraint [] | ||
249 | pattern CEmpty s <- ConN FCEmpty (HString s: _) | ||
250 | where CEmpty s = tCon FCEmpty 1 (TString :~> TConstraint) [HString s] | ||
251 | |||
246 | pattern CstrT t a b = Neut (CstrT' t a b) | 252 | pattern CstrT t a b = Neut (CstrT' t a b) |
247 | pattern CstrT' t a b = TFun' FEqCT (TType :~> Var 0 :~> Var 1 :~> TType) [t, a, b] | 253 | pattern CstrT' t a b = TFun' FEqCT (TType :~> Var 0 :~> Var 1 :~> TConstraint) [t, a, b] |
248 | pattern Coe a b w x = TFun Fcoe (TType :~> TType :~> CstrT TType (Var 1) (Var 0) :~> Var 2 :~> Var 2) [a,b,w,x] | 254 | pattern Coe a b w x = TFun Fcoe (TType :~> TType :~> CW (CstrT TType (Var 1) (Var 0)) :~> Var 2 :~> Var 2) [a,b,w,x] |
249 | pattern ParEval t a b = TFun FparEval (TType :~> Var 0 :~> Var 1 :~> Var 2) [t, a, b] | 255 | pattern ParEval t a b = TFun FparEval (TType :~> Var 0 :~> Var 1 :~> Var 2) [t, a, b] |
250 | pattern T2 a b = TFun FT2 (TType :~> TType :~> TType) [a, b] | 256 | pattern T2 a b = TFun FT2 (TConstraint :~> TConstraint :~> TConstraint) [a, b] |
257 | pattern CW a = TFun FCW (TConstraint :~> TType) [a] | ||
251 | pattern CSplit a b c <- UFunN FSplit [a, b, c] | 258 | pattern CSplit a b c <- UFunN FSplit [a, b, c] |
252 | 259 | ||
253 | pattern HLit a <- (hnf -> ELit a) | 260 | pattern 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 | |||
574 | getFunDef s f = case show s of | 581 | getFunDef 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 | |||
672 | evalCoe a b TT d = d | 680 | evalCoe a b TT d = d |
673 | evalCoe a b t d = Coe a b t d | 681 | evalCoe a b t d = Coe a b t d |
674 | 682 | ||
675 | 683 | cstr_ t a b = cw $ cstr t a b | |
676 | 684 | ||
677 | cstr = f [] | 685 | cstr = 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 | |||
710 | t2C (hnf -> TT) (hnf -> TT) = TT | 718 | t2C (hnf -> TT) (hnf -> TT) = TT |
711 | t2C a b = TFun Ft2C (Unit :~> Unit :~> Unit) [a, b] | 719 | t2C a b = TFun Ft2C (Unit :~> Unit :~> Unit) [a, b] |
712 | 720 | ||
713 | t2 (hnf -> Unit) a = a | 721 | cw (hnf -> CUnit) = Unit |
714 | t2 a (hnf -> Unit) = a | 722 | cw (hnf -> CEmpty a) = Empty a |
715 | t2 (hnf -> Empty a) (hnf -> Empty b) = Empty (a <> b) | 723 | cw a = CW a |
716 | t2 (hnf -> Empty s) _ = Empty s | 724 | |
717 | t2 _ (hnf -> Empty s) = Empty s | 725 | t2 (hnf -> CUnit) a = a |
726 | t2 a (hnf -> CUnit) = a | ||
727 | t2 (hnf -> CEmpty a) (hnf -> CEmpty b) = CEmpty (a <> b) | ||
728 | t2 (hnf -> CEmpty s) _ = CEmpty s | ||
729 | t2 _ (hnf -> CEmpty s) = CEmpty s | ||
718 | t2 a b = T2 a b | 730 | t2 a b = T2 a b |
719 | 731 | ||
720 | app_ :: Exp -> Exp -> Exp | 732 | app_ :: Exp -> Exp -> Exp |