diff options
-rw-r--r-- | src/LambdaCube/Compiler/Infer.hs | 129 |
1 files changed, 65 insertions, 64 deletions
diff --git a/src/LambdaCube/Compiler/Infer.hs b/src/LambdaCube/Compiler/Infer.hs index c93f1134..cffaf7a1 100644 --- a/src/LambdaCube/Compiler/Infer.hs +++ b/src/LambdaCube/Compiler/Infer.hs | |||
@@ -122,8 +122,8 @@ pattern Closed :: () => Up a => a -> a | |||
122 | pattern Closed a <- a where Closed a = closedExp a | 122 | pattern Closed a <- a where Closed a = closedExp a |
123 | 123 | ||
124 | pattern Con x n y <- Con_ _ x n y where Con x n y = Con_ (foldMap maxDB_ y) x n y | 124 | pattern Con x n y <- Con_ _ x n y where Con x n y = Con_ (foldMap maxDB_ y) x n y |
125 | pattern ConN s a <- Con (ConName s _ _ _ _) _ a | 125 | pattern ConN s a <- Con (ConName s _ _ _ _) _ a |
126 | pattern TCon s i t a <- Con (ConName s _ i _ t) _ a where TCon s i t a = Con (conName s Nothing i t) 0 a -- todo: don't match on type | 126 | tCon s i t a = Con (conName s Nothing i t) 0 a |
127 | pattern TyCon x y <- TyCon_ _ x y where TyCon x y = TyCon_ (foldMap maxDB_ y) x y | 127 | pattern TyCon x y <- TyCon_ _ x y where TyCon x y = TyCon_ (foldMap maxDB_ y) x y |
128 | pattern Lam y <- Lam_ _ y where Lam y = Lam_ (lowerDB (maxDB_ y)) y | 128 | pattern Lam y <- Lam_ _ y where Lam y = Lam_ (lowerDB (maxDB_ y)) y |
129 | pattern Pi v x y <- Pi_ _ v x y where Pi v x y = Pi_ (maxDB_ x <> lowerDB (maxDB_ y)) v x y | 129 | pattern Pi v x y <- Pi_ _ v x y where Pi v x y = Pi_ (maxDB_ x <> lowerDB (maxDB_ y)) v x y |
@@ -148,9 +148,9 @@ pattern TVec a b = TTyCon "'VecS" (TType :~> TNat :~> TType) [b, a] | |||
148 | pattern Empty s <- TyCon (TyConName "'Empty" _ _ _ _ _) [EString s] where | 148 | pattern Empty s <- TyCon (TyConName "'Empty" _ _ _ _ _) [EString s] where |
149 | Empty s = TyCon (TyConName "'Empty" Nothing (error "todo: inum2_") (TString :~> TType) (error "todo: tcn cons 3_") $ error "Empty") [EString s] | 149 | Empty s = TyCon (TyConName "'Empty" Nothing (error "todo: inum2_") (TString :~> TType) (error "todo: tcn cons 3_") $ error "Empty") [EString s] |
150 | 150 | ||
151 | pattern TT = Closed (TCon "TT" 0 Unit []) | 151 | pattern TT <- ConN "TT" _ where TT = Closed (tCon "TT" 0 Unit []) |
152 | pattern Zero = Closed (TCon "Zero" 0 TNat []) | 152 | pattern Zero <- ConN "Zero" _ where Zero = Closed (tCon "Zero" 0 TNat []) |
153 | pattern Succ n = TCon "Succ" 1 (TNat :~> TNat) [n] | 153 | pattern Succ n <- ConN "Succ" (n:_) where Succ n = tCon "Succ" 1 (TNat :~> TNat) [n] |
154 | 154 | ||
155 | pattern CstrT t a b = TFun "'EqCT" (TType :~> Var 0 :~> Var 1 :~> TType) [t, a, b] | 155 | pattern CstrT t a b = TFun "'EqCT" (TType :~> Var 0 :~> Var 1 :~> TType) [t, a, b] |
156 | pattern CstrT' t a b = TFun' "'EqCT" (TType :~> Var 0 :~> Var 1 :~> TType) [t, a, b] | 156 | pattern CstrT' t a b = TFun' "'EqCT" (TType :~> Var 0 :~> Var 1 :~> TType) [t, a, b] |
@@ -175,8 +175,8 @@ pattern NoTup <- (noTup -> True) | |||
175 | 175 | ||
176 | --pattern Sigma a b <- TyConN "Sigma" [a, Lam b] where Sigma a b = TTyCon "Sigma" (error "sigmatype") [a, Lam Visible a{-todo: don't duplicate-} b] | 176 | --pattern Sigma a b <- TyConN "Sigma" [a, Lam b] where Sigma a b = TTyCon "Sigma" (error "sigmatype") [a, Lam Visible a{-todo: don't duplicate-} b] |
177 | --pattern TVec a b = TTyCon "'Vec" (TNat :~> TType :~> TType) [a, b] | 177 | --pattern TVec a b = TTyCon "'Vec" (TNat :~> TType :~> TType) [a, b] |
178 | --pattern Tuple2 a b c d = TCon "Tuple2" 0 Tuple2Type [a, b, c, d] | 178 | --pattern Tuple2 a b c d = tCon "Tuple2" 0 Tuple2Type [a, b, c, d] |
179 | --pattern Tuple0 = TCon "Tuple0" 0 TTuple0 [] | 179 | --pattern Tuple0 = tCon "Tuple0" 0 TTuple0 [] |
180 | --pattern TTuple0 :: Exp | 180 | --pattern TTuple0 :: Exp |
181 | --pattern TTuple0 <- _ where TTuple0 = TTyCon0 "'Tuple0" | 181 | --pattern TTuple0 <- _ where TTuple0 = TTyCon0 "'Tuple0" |
182 | --pattern Tuple2Type :: Exp | 182 | --pattern Tuple2Type :: Exp |
@@ -192,11 +192,11 @@ fromNatE Zero = Just 0 | |||
192 | fromNatE (Succ n) = (1 +) <$> fromNatE n | 192 | fromNatE (Succ n) = (1 +) <$> fromNatE n |
193 | fromNatE _ = Nothing | 193 | fromNatE _ = Nothing |
194 | 194 | ||
195 | mkBool False = Closed $ TCon "False" 0 TBool [] | 195 | mkBool False = Closed $ tCon "False" 0 TBool [] |
196 | mkBool True = Closed $ TCon "True" 1 TBool [] | 196 | mkBool True = Closed $ tCon "True" 1 TBool [] |
197 | 197 | ||
198 | getEBool (ConN "False" []) = Just False | 198 | getEBool (ConN "False" _) = Just False |
199 | getEBool (ConN "True" []) = Just True | 199 | getEBool (ConN "True" _) = Just True |
200 | getEBool _ = Nothing | 200 | getEBool _ = Nothing |
201 | 201 | ||
202 | isCaseFun Fun{} = True | 202 | isCaseFun Fun{} = True |
@@ -212,9 +212,9 @@ isCon = \case | |||
212 | _ -> False | 212 | _ -> False |
213 | 213 | ||
214 | mkOrdering x = Closed $ case x of | 214 | mkOrdering x = Closed $ case x of |
215 | LT -> TCon "LT" 0 TOrdering [] | 215 | LT -> tCon "LT" 0 TOrdering [] |
216 | EQ -> TCon "EQ" 1 TOrdering [] | 216 | EQ -> tCon "EQ" 1 TOrdering [] |
217 | GT -> TCon "GT" 2 TOrdering [] | 217 | GT -> tCon "GT" 2 TOrdering [] |
218 | 218 | ||
219 | noTup (TyConN s _) = take 6 s /= "'Tuple" -- todo | 219 | noTup (TyConN s _) = take 6 s /= "'Tuple" -- todo |
220 | noTup _ = False | 220 | noTup _ = False |
@@ -390,6 +390,7 @@ instance Subst Exp Exp where | |||
390 | 390 | ||
391 | instance Up Neutral where | 391 | instance Up Neutral where |
392 | 392 | ||
393 | up_ 0 = \_ e -> e | ||
393 | up_ n = f where | 394 | up_ n = f where |
394 | f i e | isClosed e = e | 395 | f i e | isClosed e = e |
395 | f i e = case e of | 396 | f i e = case e of |
@@ -464,59 +465,59 @@ evalCoe a b t d = Coe a b t d | |||
464 | MT "finElim" [m, z, s, n, ConN "FZero" [i]] -> z `app_` i | 465 | MT "finElim" [m, z, s, n, ConN "FZero" [i]] -> z `app_` i |
465 | -} | 466 | -} |
466 | 467 | ||
467 | pattern MT a b = (a, b) | 468 | evalFun s = case show s of |
468 | 469 | "unsafeCoerce" -> \case [_, _, x@LCon] -> x; xs -> f xs | |
469 | evalFun s xs = case MT (show s) xs of | 470 | "'EqCT" -> \case [t, a, b] -> cstrT'' t a b |
470 | MT "unsafeCoerce" [_, _, x@LCon] -> x | 471 | "reflCstr" -> \case [a] -> reflCstr a |
471 | MT "'EqCT" [t, a, b] -> cstrT'' t a b | 472 | "coe" -> \case [a, b, t, d] -> evalCoe a b t d |
472 | MT "reflCstr" [a] -> reflCstr a | 473 | "'T2" -> \case [a, b] -> t2 a b |
473 | MT "coe" [a, b, t, d] -> evalCoe a b t d | 474 | "t2C" -> \case [a, b] -> t2C a b |
474 | MT "'T2" [a, b] -> t2 a b | 475 | "parEval" -> \case [t, a, b] -> parEval t a b |
475 | MT "t2C" [a, b] -> t2C a b | ||
476 | MT "parEval" [t, a, b] -> parEval a b | ||
477 | where | 476 | where |
478 | parEval (LabelEnd x) _ = LabelEnd x | 477 | parEval _ (LabelEnd x) _ = LabelEnd x |
479 | parEval _ (LabelEnd x) = LabelEnd x | 478 | parEval _ _ (LabelEnd x) = LabelEnd x |
480 | parEval a b = ParEval t a b | 479 | parEval t a b = ParEval t a b |
481 | 480 | ||
482 | -- general compiler primitives | 481 | -- general compiler primitives |
483 | MT "primAddInt" [EInt i, EInt j] -> EInt (i + j) | 482 | "primAddInt" -> \case [EInt i, EInt j] -> EInt (i + j); xs -> f xs |
484 | MT "primSubInt" [EInt i, EInt j] -> EInt (i - j) | 483 | "primSubInt" -> \case [EInt i, EInt j] -> EInt (i - j); xs -> f xs |
485 | MT "primModInt" [EInt i, EInt j] -> EInt (i `mod` j) | 484 | "primModInt" -> \case [EInt i, EInt j] -> EInt (i `mod` j); xs -> f xs |
486 | MT "primSqrtFloat" [EFloat i] -> EFloat $ sqrt i | 485 | "primSqrtFloat" -> \case [EFloat i] -> EFloat $ sqrt i; xs -> f xs |
487 | MT "primRound" [EFloat i] -> EInt $ round i | 486 | "primRound" -> \case [EFloat i] -> EInt $ round i; xs -> f xs |
488 | MT "primIntToFloat" [EInt i] -> EFloat $ fromIntegral i | 487 | "primIntToFloat" -> \case [EInt i] -> EFloat $ fromIntegral i; xs -> f xs |
489 | MT "primCompareInt" [EInt x, EInt y] -> mkOrdering $ x `compare` y | 488 | "primCompareInt" -> \case [EInt x, EInt y] -> mkOrdering $ x `compare` y; xs -> f xs |
490 | MT "primCompareFloat" [EFloat x, EFloat y] -> mkOrdering $ x `compare` y | 489 | "primCompareFloat" -> \case [EFloat x, EFloat y] -> mkOrdering $ x `compare` y; xs -> f xs |
491 | MT "primCompareChar" [EChar x, EChar y] -> mkOrdering $ x `compare` y | 490 | "primCompareChar" -> \case [EChar x, EChar y] -> mkOrdering $ x `compare` y; xs -> f xs |
492 | MT "primCompareString" [EString x, EString y] -> mkOrdering $ x `compare` y | 491 | "primCompareString" -> \case [EString x, EString y] -> mkOrdering $ x `compare` y; xs -> f xs |
493 | 492 | ||
494 | -- LambdaCube 3D specific primitives | 493 | -- LambdaCube 3D specific primitives |
495 | MT "PrimGreaterThan" [_, _, _, _, _, _, _, x, y] | Just r <- twoOpBool (>) x y -> r | 494 | "PrimGreaterThan" -> \case [_, _, _, _, _, _, _, x, y] | Just r <- twoOpBool (>) x y -> r; xs -> f xs |
496 | MT "PrimGreaterThanEqual" [_, _, _, _, _, _, _, x, y] | Just r <- twoOpBool (>=) x y -> r | 495 | "PrimGreaterThanEqual" -> \case [_, _, _, _, _, _, _, x, y] | Just r <- twoOpBool (>=) x y -> r; xs -> f xs |
497 | MT "PrimLessThan" [_, _, _, _, _, _, _, x, y] | Just r <- twoOpBool (<) x y -> r | 496 | "PrimLessThan" -> \case [_, _, _, _, _, _, _, x, y] | Just r <- twoOpBool (<) x y -> r; xs -> f xs |
498 | MT "PrimLessThanEqual" [_, _, _, _, _, _, _, x, y] | Just r <- twoOpBool (<=) x y -> r | 497 | "PrimLessThanEqual" -> \case [_, _, _, _, _, _, _, x, y] | Just r <- twoOpBool (<=) x y -> r; xs -> f xs |
499 | MT "PrimEqualV" [_, _, _, _, _, _, _, x, y] | Just r <- twoOpBool (==) x y -> r | 498 | "PrimEqualV" -> \case [_, _, _, _, _, _, _, x, y] | Just r <- twoOpBool (==) x y -> r; xs -> f xs |
500 | MT "PrimNotEqualV" [_, _, _, _, _, _, _, x, y] | Just r <- twoOpBool (/=) x y -> r | 499 | "PrimNotEqualV" -> \case [_, _, _, _, _, _, _, x, y] | Just r <- twoOpBool (/=) x y -> r; xs -> f xs |
501 | MT "PrimEqual" [_, _, _, x, y] | Just r <- twoOpBool (==) x y -> r | 500 | "PrimEqual" -> \case [_, _, _, x, y] | Just r <- twoOpBool (==) x y -> r; xs -> f xs |
502 | MT "PrimNotEqual" [_, _, _, x, y] | Just r <- twoOpBool (/=) x y -> r | 501 | "PrimNotEqual" -> \case [_, _, _, x, y] | Just r <- twoOpBool (/=) x y -> r; xs -> f xs |
503 | MT "PrimSubS" [_, _, _, _, x, y] | Just r <- twoOp (-) x y -> r | 502 | "PrimSubS" -> \case [_, _, _, _, x, y] | Just r <- twoOp (-) x y -> r; xs -> f xs |
504 | MT "PrimSub" [_, _, x, y] | Just r <- twoOp (-) x y -> r | 503 | "PrimSub" -> \case [_, _, x, y] | Just r <- twoOp (-) x y -> r; xs -> f xs |
505 | MT "PrimAddS" [_, _, _, _, x, y] | Just r <- twoOp (+) x y -> r | 504 | "PrimAddS" -> \case [_, _, _, _, x, y] | Just r <- twoOp (+) x y -> r; xs -> f xs |
506 | MT "PrimAdd" [_, _, x, y] | Just r <- twoOp (+) x y -> r | 505 | "PrimAdd" -> \case [_, _, x, y] | Just r <- twoOp (+) x y -> r; xs -> f xs |
507 | MT "PrimMulS" [_, _, _, _, x, y] | Just r <- twoOp (*) x y -> r | 506 | "PrimMulS" -> \case [_, _, _, _, x, y] | Just r <- twoOp (*) x y -> r; xs -> f xs |
508 | MT "PrimMul" [_, _, x, y] | Just r <- twoOp (*) x y -> r | 507 | "PrimMul" -> \case [_, _, x, y] | Just r <- twoOp (*) x y -> r; xs -> f xs |
509 | MT "PrimDivS" [_, _, _, _, _, x, y] | Just r <- twoOp_ (/) div x y -> r | 508 | "PrimDivS" -> \case [_, _, _, _, _, x, y] | Just r <- twoOp_ (/) div x y -> r; xs -> f xs |
510 | MT "PrimDiv" [_, _, _, _, _, x, y] | Just r <- twoOp_ (/) div x y -> r | 509 | "PrimDiv" -> \case [_, _, _, _, _, x, y] | Just r <- twoOp_ (/) div x y -> r; xs -> f xs |
511 | MT "PrimModS" [_, _, _, _, _, x, y] | Just r <- twoOp_ modF mod x y -> r | 510 | "PrimModS" -> \case [_, _, _, _, _, x, y] | Just r <- twoOp_ modF mod x y -> r; xs -> f xs |
512 | MT "PrimMod" [_, _, _, _, _, x, y] | Just r <- twoOp_ modF mod x y -> r | 511 | "PrimMod" -> \case [_, _, _, _, _, x, y] | Just r <- twoOp_ modF mod x y -> r; xs -> f xs |
513 | MT "PrimNeg" [_, x] | Just r <- oneOp negate x -> r | 512 | "PrimNeg" -> \case [_, x] | Just r <- oneOp negate x -> r; xs -> f xs |
514 | MT "PrimAnd" [EBool x, EBool y] -> EBool (x && y) | 513 | "PrimAnd" -> \case [EBool x, EBool y] -> EBool (x && y); xs -> f xs |
515 | MT "PrimOr" [EBool x, EBool y] -> EBool (x || y) | 514 | "PrimOr" -> \case [EBool x, EBool y] -> EBool (x || y); xs -> f xs |
516 | MT "PrimXor" [EBool x, EBool y] -> EBool (x /= y) | 515 | "PrimXor" -> \case [EBool x, EBool y] -> EBool (x /= y); xs -> f xs |
517 | MT "PrimNot" [_, _, _, EBool x] -> EBool $ not x | 516 | "PrimNot" -> \case [_, _, _, EBool x] -> EBool $ not x; xs -> f xs |
518 | 517 | ||
519 | MT a b -> Fun s b | 518 | _ -> f |
519 | where | ||
520 | f = Fun s | ||
520 | 521 | ||
521 | cstrT'' TType = cstrT_ TType | 522 | cstrT'' TType = cstrT_ TType |
522 | cstrT'' t = cstrT t | 523 | cstrT'' t = cstrT t |
@@ -525,7 +526,7 @@ cstr = cstrT_ TType | |||
525 | 526 | ||
526 | 527 | ||
527 | cstrT t (UL a) (UL a') | a == a' = Unit | 528 | cstrT t (UL a) (UL a') | a == a' = Unit |
528 | cstrT t (ConN "Succ" [a]) (ConN "Succ" [a']) = cstrT TNat a a' | 529 | cstrT TNat (ConN "Succ" [a]) (ConN "Succ" [a']) = cstrT TNat a a' |
529 | cstrT t (FixLabel _ a) a' = cstrT t a a' | 530 | cstrT t (FixLabel _ a) a' = cstrT t a a' |
530 | cstrT t a (FixLabel _ a') = cstrT t a a' | 531 | cstrT t a (FixLabel _ a') = cstrT t a a' |
531 | cstrT t a a' = CstrT t a a' | 532 | cstrT t a a' = CstrT t a a' |
@@ -1380,7 +1381,7 @@ instance MkDoc Exp where | |||
1380 | Lam b -> join $ shLam True (BLam Visible) <$> f TType{-todo-} <*> pure (f b) | 1381 | Lam b -> join $ shLam True (BLam Visible) <$> f TType{-todo-} <*> pure (f b) |
1381 | Pi h a b -> join $ shLam (used 0 b) (BPi h) <$> f a <*> pure (f b) | 1382 | Pi h a b -> join $ shLam (used 0 b) (BPi h) <$> f a <*> pure (f b) |
1382 | ENat n -> pure $ shAtom $ show n | 1383 | ENat n -> pure $ shAtom $ show n |
1383 | ConN s xs -> foldl (shApp Visible) (shAtom_ s) <$> mapM f xs | 1384 | Con s _ xs -> foldl (shApp Visible) (shAtom_ $ show s) <$> mapM f xs |
1384 | TyConN s xs -> foldl (shApp Visible) (shAtom_ s) <$> mapM f xs | 1385 | TyConN s xs -> foldl (shApp Visible) (shAtom_ s) <$> mapM f xs |
1385 | TType -> pure $ shAtom "Type" | 1386 | TType -> pure $ shAtom "Type" |
1386 | ELit l -> pure $ shAtom $ show l | 1387 | ELit l -> pure $ shAtom $ show l |