diff options
author | Andor Penzes <andor.penzes@gmail.com> | 2015-12-19 12:45:59 +0100 |
---|---|---|
committer | Andor Penzes <andor.penzes@gmail.com> | 2015-12-19 12:45:59 +0100 |
commit | 55e132dfc4283c98a31086face7c870beb527bcf (patch) | |
tree | 6450d82a1f92fef40adce4da1896bc36c11f3d87 /prototypes | |
parent | 288cd630c194a867408b6cea52256d14d0312952 (diff) |
Refactor: use fromNatE in patterns, introduce toNatE
Diffstat (limited to 'prototypes')
-rw-r--r-- | prototypes/Infer.hs | 52 |
1 files changed, 28 insertions, 24 deletions
diff --git a/prototypes/Infer.hs b/prototypes/Infer.hs index 8490761a..9ddec574 100644 --- a/prototypes/Infer.hs +++ b/prototypes/Infer.hs | |||
@@ -224,11 +224,11 @@ pattern Tuple0Type <- _ where Tuple0Type = TTyCon0 "'Tuple0" | |||
224 | pattern Tuple2Type :: Exp | 224 | pattern Tuple2Type :: Exp |
225 | pattern Tuple2Type <- _ where Tuple2Type = Pi Hidden TType $ Pi Hidden TType $ Var 1 :~> Var 1 :~> tTuple2 (Var 3) (Var 2) | 225 | pattern Tuple2Type <- _ where Tuple2Type = Pi Hidden TType $ Pi Hidden TType $ Var 1 :~> Var 1 :~> tTuple2 (Var 3) (Var 2) |
226 | pattern V2Type :: Exp | 226 | pattern V2Type :: Exp |
227 | pattern V2Type <- _ where V2Type = Pi Hidden TType $ Var 0 :~> Var 1 :~> TVec (Succ $ Succ Zero) (Var 2) | 227 | pattern V2Type <- _ where V2Type = Pi Hidden TType $ Var 0 :~> Var 1 :~> TVec (toNatE 2) (Var 2) |
228 | pattern V3Type :: Exp | 228 | pattern V3Type :: Exp |
229 | pattern V3Type <- _ where V3Type = Pi Hidden TType $ Var 0 :~> Var 1 :~> Var 2 :~> TVec (Succ $ Succ $ Succ Zero) (Var 3) | 229 | pattern V3Type <- _ where V3Type = Pi Hidden TType $ Var 0 :~> Var 1 :~> Var 2 :~> TVec (toNatE 3) (Var 3) |
230 | pattern V4Type :: Exp | 230 | pattern V4Type :: Exp |
231 | pattern V4Type <- _ where V4Type = Pi Hidden TType $ Var 0 :~> Var 1 :~> Var 2 :~> Var 3 :~> TVec (Succ $ Succ $ Succ $ Succ Zero) (Var 4) | 231 | pattern V4Type <- _ where V4Type = Pi Hidden TType $ Var 0 :~> Var 1 :~> Var 2 :~> Var 3 :~> TVec (toNatE 4) (Var 4) |
232 | pattern TRecordType :: Exp | 232 | pattern TRecordType :: Exp |
233 | pattern TRecordType <- _ where TRecordType = TList SListEType :~> TType | 233 | pattern TRecordType <- _ where TRecordType = TList SListEType :~> TType |
234 | pattern NilType :: Exp | 234 | pattern NilType :: Exp |
@@ -252,6 +252,10 @@ tTuple2 a b = TTyCon "'Tuple2" (TType :~> TType :~> TType) [a, b] | |||
252 | tTuple3 a b c = TTyCon "'Tuple3" (TType :~> TType :~> TType :~> TType) [a, b, c] | 252 | tTuple3 a b c = TTyCon "'Tuple3" (TType :~> TType :~> TType :~> TType) [a, b, c] |
253 | tMat a b c = TTyCon "'Mat" (TNat :~> TNat :~> TType :~> TType) [a, b, c] | 253 | tMat a b c = TTyCon "'Mat" (TNat :~> TNat :~> TType :~> TType) [a, b, c] |
254 | 254 | ||
255 | toNatE :: Int -> Exp | ||
256 | toNatE 0 = Zero | ||
257 | toNatE n | n > 0 = Succ (toNatE (n - 1)) | ||
258 | |||
255 | t2C TT TT = TT | 259 | t2C TT TT = TT |
256 | t2C a b = T2C a b | 260 | t2C a b = T2C a b |
257 | 261 | ||
@@ -526,15 +530,15 @@ eval te = \case | |||
526 | FunN "PrimSubS" [_, _, _, _, EInt x, EInt y] -> EInt (x - y) | 530 | FunN "PrimSubS" [_, _, _, _, EInt x, EInt y] -> EInt (x - y) |
527 | FunN "PrimAddS" [_, _, _, _, EFloat x, EFloat y] -> EFloat (x + y) | 531 | FunN "PrimAddS" [_, _, _, _, EFloat x, EFloat y] -> EFloat (x + y) |
528 | FunN "PrimMulS" [_, _, _, _, EFloat x, EFloat y] -> EFloat (x * y) | 532 | FunN "PrimMulS" [_, _, _, _, EFloat x, EFloat y] -> EFloat (x * y) |
529 | FunN "zeroComp" [TVec (Succ (Succ Zero)) t@TFloat, TT] -> VV2 t (EFloat 0) (EFloat 0) | 533 | FunN "zeroComp" [TVec (fromNatE -> Just 2) t@TFloat, TT] -> VV2 t (EFloat 0) (EFloat 0) |
530 | FunN "zeroComp" [TVec (Succ (Succ (Succ Zero))) t@TFloat, TT] -> VV3 t (EFloat 0) (EFloat 0) (EFloat 0) | 534 | FunN "zeroComp" [TVec (fromNatE -> Just 3) t@TFloat, TT] -> VV3 t (EFloat 0) (EFloat 0) (EFloat 0) |
531 | FunN "zeroComp" [TVec (Succ (Succ (Succ (Succ Zero)))) t@TFloat, TT] -> VV4 t (EFloat 0) (EFloat 0) (EFloat 0) (EFloat 0) | 535 | FunN "zeroComp" [TVec (fromNatE -> Just 4) t@TFloat, TT] -> VV4 t (EFloat 0) (EFloat 0) (EFloat 0) (EFloat 0) |
532 | FunN "oneComp" [TVec (Succ (Succ Zero)) t@TFloat, TT] -> VV2 t (EFloat 1) (EFloat 1) | 536 | FunN "oneComp" [TVec (fromNatE -> Just 2) t@TFloat, TT] -> VV2 t (EFloat 1) (EFloat 1) |
533 | FunN "oneComp" [TVec (Succ (Succ (Succ Zero))) t@TFloat, TT] -> VV3 t (EFloat 1) (EFloat 1) (EFloat 1) | 537 | FunN "oneComp" [TVec (fromNatE -> Just 3) t@TFloat, TT] -> VV3 t (EFloat 1) (EFloat 1) (EFloat 1) |
534 | FunN "oneComp" [TVec (Succ (Succ (Succ (Succ Zero)))) t@TFloat, TT] -> VV4 t (EFloat 1) (EFloat 1) (EFloat 1) (EFloat 1) | 538 | FunN "oneComp" [TVec (fromNatE -> Just 4) t@TFloat, TT] -> VV4 t (EFloat 1) (EFloat 1) (EFloat 1) (EFloat 1) |
535 | FunN "oneComp" [TVec (Succ (Succ Zero)) t@TBool, TT] -> VV2 t VTrue VTrue | 539 | FunN "oneComp" [TVec (fromNatE -> Just 2) t@TBool, TT] -> VV2 t VTrue VTrue |
536 | FunN "oneComp" [TVec (Succ (Succ (Succ Zero))) t@TBool, TT] -> VV3 t VTrue VTrue VTrue | 540 | FunN "oneComp" [TVec (fromNatE -> Just 3) t@TBool, TT] -> VV3 t VTrue VTrue VTrue |
537 | FunN "oneComp" [TVec (Succ (Succ (Succ (Succ Zero)))) t@TBool, TT] -> VV4 t VTrue VTrue VTrue VTrue | 541 | FunN "oneComp" [TVec (fromNatE -> Just 4) t@TBool, TT] -> VV4 t VTrue VTrue VTrue VTrue |
538 | 542 | ||
539 | -- todo: elim | 543 | -- todo: elim |
540 | Fun n@(FunName "natElim" _ _) [a, z, s, Succ x] -> let -- todo: replace let with better abstraction | 544 | Fun n@(FunName "natElim" _ _) [a, z, s, Succ x] -> let -- todo: replace let with better abstraction |
@@ -550,11 +554,11 @@ eval te = \case | |||
550 | FunN "matchList" [t, f, TyConN "List" [a]] -> t `app_` a | 554 | FunN "matchList" [t, f, TyConN "List" [a]] -> t `app_` a |
551 | FunN "matchList" [t, f, c@LCon] -> f `app_` c | 555 | FunN "matchList" [t, f, c@LCon] -> f `app_` c |
552 | 556 | ||
553 | FunN "Component" [TVec (Succ (Succ (Succ Zero))) TFloat] -> Unit | 557 | FunN "Component" [TVec (fromNatE -> Just 3) TFloat] -> Unit |
554 | FunN "Component" [TVec (Succ (Succ (Succ (Succ Zero)))) TBool] -> Unit | 558 | FunN "Component" [TVec (fromNatE -> Just 4) TBool] -> Unit |
555 | FunN "Component" [TVec (Succ (Succ (Succ (Succ Zero)))) TFloat] -> Unit | 559 | FunN "Component" [TVec (fromNatE -> Just 4) TFloat] -> Unit |
556 | FunN "Floating" [TVec (Succ (Succ Zero)) TFloat] -> Unit | 560 | FunN "Floating" [TVec (fromNatE -> Just 2) TFloat] -> Unit |
557 | FunN "Floating" [TVec (Succ (Succ (Succ (Succ Zero)))) TFloat] -> Unit | 561 | FunN "Floating" [TVec (fromNatE -> Just 4) TFloat] -> Unit |
558 | Fun n@(FunName "Eq_" _ _) [TyConN "List" [a]] -> eval te $ Fun n [a] | 562 | Fun n@(FunName "Eq_" _ _) [TyConN "List" [a]] -> eval te $ Fun n [a] |
559 | FunN "Eq_" [TInt] -> Unit | 563 | FunN "Eq_" [TInt] -> Unit |
560 | FunN "Eq_" [LCon] -> Empty | 564 | FunN "Eq_" [LCon] -> Empty |
@@ -570,9 +574,9 @@ eval te = \case | |||
570 | FunN "VecScalar" [Succ Zero, t] -> t | 574 | FunN "VecScalar" [Succ Zero, t] -> t |
571 | FunN "VecScalar" [n@(Succ (Succ _)), t] -> TVec n t | 575 | FunN "VecScalar" [n@(Succ (Succ _)), t] -> TVec n t |
572 | FunN "TFFrameBuffer" [TyConN "'Image" [n, t]] -> TFrameBuffer n t | 576 | FunN "TFFrameBuffer" [TyConN "'Image" [n, t]] -> TFrameBuffer n t |
573 | FunN "TFFrameBuffer" [TyConN "'Tuple2" [TyConN "'Image" [i@(fromNat -> Just n), t], TyConN "'Image" [fromNat -> Just n', t']]] | 577 | FunN "TFFrameBuffer" [TyConN "'Tuple2" [TyConN "'Image" [i@(fromNatE -> Just n), t], TyConN "'Image" [fromNatE -> Just n', t']]] |
574 | | n == n' -> TFrameBuffer i $ tTuple2 t t' -- todo | 578 | | n == n' -> TFrameBuffer i $ tTuple2 t t' -- todo |
575 | FunN "TFFrameBuffer" [TyConN "'Tuple3" [TyConN "'Image" [i@(fromNat -> Just n), t], TyConN "'Image" [fromNat -> Just n', t'], TyConN "'Image" [fromNat -> Just n'', t'']]] | 579 | FunN "TFFrameBuffer" [TyConN "'Tuple3" [TyConN "'Image" [i@(fromNatE -> Just n), t], TyConN "'Image" [fromNatE -> Just n', t'], TyConN "'Image" [fromNatE -> Just n'', t'']]] |
576 | | n == n' && n == n'' -> TFrameBuffer i $ tTuple3 t t' t'' -- todo | 580 | | n == n' && n == n'' -> TFrameBuffer i $ tTuple3 t t' t'' -- todo |
577 | FunN "FragOps" [TyConN "'FragmentOperation" [t]] -> t | 581 | FunN "FragOps" [TyConN "'FragmentOperation" [t]] -> t |
578 | FunN "FragOps" [TyConN "'Tuple2" [TyConN "'FragmentOperation" [t], TyConN "'FragmentOperation" [t']]] -> tTuple2 t t' | 582 | FunN "FragOps" [TyConN "'Tuple2" [TyConN "'FragmentOperation" [t], TyConN "'FragmentOperation" [t']]] -> tTuple2 t t' |
@@ -640,9 +644,9 @@ pattern NoTup <- (noTup -> True) | |||
640 | noTup (TyConN s _) = take 6 s /= "'Tuple" -- todo | 644 | noTup (TyConN s _) = take 6 s /= "'Tuple" -- todo |
641 | noTup _ = False | 645 | noTup _ = False |
642 | 646 | ||
643 | fromNat :: Exp -> Maybe Int | 647 | fromNatE :: Exp -> Maybe Int |
644 | fromNat Zero = Just 0 | 648 | fromNatE Zero = Just 0 |
645 | fromNat (Succ n) = (1 +) <$> fromNat n | 649 | fromNatE (Succ n) = (1 +) <$> fromNatE n |
646 | 650 | ||
647 | -- todo | 651 | -- todo |
648 | coe a b c d | a == b = d -- todo | 652 | coe a b c d | a == b = d -- todo |
@@ -677,8 +681,8 @@ cstr = cstr__ [] | |||
677 | -- cstr_ ns (Label f xs _) (Label f' xs' _) | f == f' = foldr1 T2 $ zipWith (cstr__ ns) xs xs' | 681 | -- cstr_ ns (Label f xs _) (Label f' xs' _) | f == f' = foldr1 T2 $ zipWith (cstr__ ns) xs xs' |
678 | cstr_ ns (FunN "VecScalar" [a, b]) (TVec a' b') = t2 (cstr__ ns a a') (cstr__ ns b b') | 682 | cstr_ ns (FunN "VecScalar" [a, b]) (TVec a' b') = t2 (cstr__ ns a a') (cstr__ ns b b') |
679 | cstr_ ns (FunN "VecScalar" [a, b]) (FunN "VecScalar" [a', b']) = t2 (cstr__ ns a a') (cstr__ ns b b') | 683 | cstr_ ns (FunN "VecScalar" [a, b]) (FunN "VecScalar" [a', b']) = t2 (cstr__ ns a a') (cstr__ ns b b') |
680 | cstr_ ns (FunN "VecScalar" [a, b]) t@(TTyCon0 n) | isElemTy n = t2 (cstr__ ns a (Succ Zero)) (cstr__ ns b t) | 684 | cstr_ ns (FunN "VecScalar" [a, b]) t@(TTyCon0 n) | isElemTy n = t2 (cstr__ ns a (toNatE 1)) (cstr__ ns b t) |
681 | cstr_ ns t@(TTyCon0 n) (FunN "VecScalar" [a, b]) | isElemTy n = t2 (cstr__ ns a (Succ Zero)) (cstr__ ns b t) | 685 | cstr_ ns t@(TTyCon0 n) (FunN "VecScalar" [a, b]) | isElemTy n = t2 (cstr__ ns a (toNatE 1)) (cstr__ ns b t) |
682 | cstr_ ns@[] (FunN "TFMat" [x, y]) (TyConN "'Mat" [i, j, a]) = t2 (cstr__ ns x (TVec i a)) (cstr__ ns y (TVec j a)) | 686 | cstr_ ns@[] (FunN "TFMat" [x, y]) (TyConN "'Mat" [i, j, a]) = t2 (cstr__ ns x (TVec i a)) (cstr__ ns y (TVec j a)) |
683 | cstr_ ns@[] (TyConN "'Tuple2" [x, y]) (FunN "JoinTupleType" [x'@NoTup, y']) = t2 (cstr__ ns x x') (cstr__ ns y y') | 687 | cstr_ ns@[] (TyConN "'Tuple2" [x, y]) (FunN "JoinTupleType" [x'@NoTup, y']) = t2 (cstr__ ns x x') (cstr__ ns y y') |
684 | cstr_ ns@[] (TyConN "'Color" [x]) (FunN "ColorRepr" [x']) = cstr__ ns x x' | 688 | cstr_ ns@[] (TyConN "'Color" [x]) (FunN "ColorRepr" [x']) = cstr__ ns x x' |