singletonsã®åã¬ãã«é¢æ°ã使ã£ã¦ãã¾ãã¨ç¤ºããªãæ§è³ªã®ä¾
âã®è©±
å®ésingletonsãä¹±èãããã ãã©ï¼singletonsããæä¾ããã¦ãåã¬ãã«é¢æ°ã®ä¸ã«ã¯ä½¿ãã¨ããã以ä¸è¨¼æãé²ã¾ãªããªãããã¤ãçµæ§ãªå²åã§å«ã¾ãã¦ã¦ï¼é¢ç½å³ã®ãããªã以åã«æããå è¡ãã¦ãã¾ãï¼ããã¯åã¬ãã«è¨ç®ã®ããã®ã©ã¤ãã©ãªã§ãããªãã¦è¨¼æã®ããã®ã©ã¤ãã©ãªã«ã¯ãªãã¦ãªãï¼ https://t.co/ckCcUpWQLK
— Noriyuki OHKAWA (@notogawa) January 3, 2019
ãµã³ãã«ã³ã¼ãã¯ここ
ãµã³ãã«ã³ã¼ãã§ãããã¨ãã¦ãããã¨ã¯ããªã¹ãã®å転ã¯ãªã¹ãå è¦ç´ ã®ç½®æã®ä¸ç¨®ã§ãããã¨ã示ããã¨ãããã®
ã¾ãï¼2ã¤ã®åã¬ãã«ãªã¹ããç½®æã®é¢ä¿ã«ãããã¨ãå®ç¾©ããï¼
data Permutation :: [k] -> [k] -> Type where PermutationNil :: Permutation '[] '[] PermutationSkip :: Sing a -> Permutation xs ys -> Permutation (a : xs) (a : ys) PermutationSwap :: Sing a -> Sing b -> Permutation (a : b : xs) (b : a : xs) PermutationTrans :: Permutation xs ys -> Permutation ys zs -> Permutation xs zs
å°ããã®å®ç¾©ã«ã¤ãã¦ç¢ºèªãã¦ããï¼å®éã«ã²ã¨ã¤ã®ä¾ã«ã¤ãã¦ç½®æé¢ä¿ã«ãããã¨ç¢ºèªãã¦ã¿ãï¼
testPermutation :: Permutation [1,2,3,4] [2,4,3,1] testPermutation = PermutationTrans (PermutationSwap s1 s2) $ PermutationTrans (PermutationSkip s2 $ PermutationSwap s1 s3) $ PermutationTrans (PermutationSkip s2 $ PermutationSkip s3 $ PermutationSwap s1 s4) $ PermutationSkip s2 $ PermutationSwap s3 s4
ç½®æã¯åå¤é¢ä¿ã®ã²ã¨ã¤ãªã®ã§ï¼åå°å¾ï¼
refl :: Sing xs -> Permutation xs xs refl SNil = PermutationNil refl (SCons x xs) = PermutationSkip x $ refl xs
対称å¾ï¼
sym :: Permutation xs ys -> Permutation ys xs sym PermutationNil = PermutationNil sym (PermutationSkip s p) = PermutationSkip s $ sym p sym (PermutationSwap a b) = PermutationSwap b a sym (PermutationTrans p1 p2) = PermutationTrans (sym p2) (sym p1)
æ¨ç§»å¾ã示ããï¼
trans :: Permutation xs ys -> Permutation ys zs -> Permutation xs zs trans = PermutationTrans
å é ã¸ã®è¦ç´ ã®è¿½å ã¨ï¼éä¸ã¸ã®è¦ç´ ã®è¿½å ã¯ï¼ç½®æé¢ä¿çã«ã¯åããã¨ãæå³ããï¼ã¨ãã£ãæ§è³ªã示ããï¼
here :: Sing a -> Sing xs -> Sing ys -> Permutation (a : xs :++ ys) (xs :++ (a : ys)) here a SNil ys = PermutationSkip a $ refl ys here a (SCons x xs) ys = PermutationTrans (PermutationSwap a x) (PermutationSkip x $ here a xs ys)
ãã¦ï¼å転ãç½®æã®ä¸ç¨®ã§ããã㨠reverseIsPermutation ã¯ï¼
reverseIsPermutation :: Sing xs -> Permutation xs (L.Reverse xs)
ã®ãããªåããã¦ããï¼ãããï¼å®éã«ããã®å®è£ (=証æ)ãä¸ãããã¨ããã¨ï¼
reverseIsPermutation SNil = PermutationNil reverseIsPermutation (SCons x xs) = _
ã¾ã§ããé²ããããªãï¼ãã®typed holeã確èªããã¨ï¼
[-Wtyped-holes] ⢠Found hole: _ :: Permutation (n0 : n1) (Data.Singletons.Prelude.List.Let6989586621679793554Rev (n0 : n1) n1 '[n0])
ã¨ãªã£ã¦ããï¼Let6989586621679793554Revã£ã¦ä½ã¨ï¼singletonsã©ã¤ãã©ãªã®reverseã®å®ç¾©ã確èªããã¨ï¼
$(singletonsOnly [d| (snip.) reverse :: [a] -> [a] reverse l = rev l [] where rev :: [a] -> [a] -> [a] rev [] a = a rev (x:xs) a = rev xs (x:a) (snip.) |])
ã§ããï¼ãã®reverseã®å®ç¾©ããTHã§çæãããReverseã¯exportããã¦ãããï¼reverseå®ç¾©ä¸ã®whereç¯ã«ããrevã¯exportããã¦ããªãã®ã§ï¼ãããæå¾ ããåãç¾ãã¦ãã¾ãã¨è¨¼æãé²ããããã«æ¢ã¾ã£ã¦ãã¾ãï¼
ããã¯ããã©ããããããªãã®ã§ï¼ãããªãèªåã§reverseãå®ç¾©ãã¦è£å©é¢æ°ãåç §ã§ããããã«ããªããã°ãªããªãï¼
$(singletonsOnly [d| myReverse :: [a] -> [a] myReverse l = myReverseAcc l [] myReverseAcc :: [a] -> [a] -> [a] myReverseAcc [] ys = ys myReverseAcc (x:xs) ys = myReverseAcc xs (x:ys) |])
ãã®myReverseã®å®ç¾©ããçæããã MyReverse ã MyReverseAcc ã«ããï¼åæ§ã«å転ãç½®æã§ãããã¨ã示ãã¨
myReverseIsPermutation :: Sing xs -> Permutation xs (MyReverse xs) myReverseIsPermutation xs' = gcastWith (nilIsRightIdentityOfAppend xs') (go xs' SNil) where go :: Sing xs -> Sing ys -> Permutation (xs :++ ys) (MyReverseAcc xs ys) go SNil ys = refl ys go (SCons x xs) ys = PermutationTrans (here x xs ys) (go xs (SCons x ys))
ã®ããã«ç¤ºããã¨ãã§ããï¼nilIsRightIdentityOfAppend 㯠[] ããªã¹ãçµåæ¼ç®ã«å¯¾ããå³åä½å ã§ãããã¨ã示ãã¦ããï¼
nilIsRightIdentityOfAppend :: Sing xs -> (xs :++ '[]) :~: xs nilIsRightIdentityOfAppend SNil = Refl nilIsRightIdentityOfAppend (SCons _ xs) = gcastWith (nilIsRightIdentityOfAppend xs) Refl
ã¨ãã£ãããã«ï¼singletonsã®Singã®å®ç¾©ã¯å¥æ®µåé¡ç¡ããï¼Data.Singletons.Prelude 以ä¸ã®ã¢ã¸ã¥ã¼ã«ã§å®ç¾©ããã¦ããåã¬ãã«é¢æ°ã使ã£ãã¤ã³ã¿ã¼ãã§ã¼ã¹ã¯ï¼ããã ãã§è¨¼æãã§ããªããªã£ã¦ãã¾ããããªãã®ãå¤æ°åå¨ãã¦ããï¼whereãletãªã©ã®è£å©é¢æ°ã«ããå®ç¾©ããã¦ãããã®ã¯æããå ¨æ» ãããªãã ãããï¼ãªã®ã§ï¼ãããã£ããã®ãã©ã¤ãã©ãªå¤ã«exportããå ´åã¯ï¼ãã®ã¾ã¾singletonsã®ãã®ã使ãã®ã§ã¯ãªãé©å®hideããä¸ã§ï¼èªåã§å®ç¾©ãã証æå¯è½ãªãã®ã§ä»£æ¿ãã¦exportããã»ãã親åãããªãããªã¨ï¼