summaryrefslogtreecommitdiff
path: root/KeyRing.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2014-04-14 15:26:56 -0400
committerjoe <joe@jerkface.net>2014-04-14 15:26:56 -0400
commited9ecad17bf8d916eb8780b0e9a46597aeab7ae3 (patch)
tree6680efcfc7e03c981d5118c6ec8eb4984644635c /KeyRing.hs
parent7f1fb5ac9a1dfab0f44ab3b6327115d0eb4b4488 (diff)
another alternative monad
Diffstat (limited to 'KeyRing.hs')
-rw-r--r--KeyRing.hs109
1 files changed, 108 insertions, 1 deletions
diff --git a/KeyRing.hs b/KeyRing.hs
index 71d4bd1..c05e9e7 100644
--- a/KeyRing.hs
+++ b/KeyRing.hs
@@ -382,6 +382,8 @@ merge_ db filename qs = foldl mergeit db (zip [0..] qs)
382 mergeSubSig n sig Nothing = error $ 382 mergeSubSig n sig Nothing = error $
383 "Unable to merge subkey signature: "++(words (show sig) >>= take 1) 383 "Unable to merge subkey signature: "++(words (show sig) >>= take 1)
384 384
385
386{-
385data Kiki a = 387data Kiki a =
386 SinglePass (KeyRingData -> KeyRingAction a) 388 SinglePass (KeyRingData -> KeyRingAction a)
387 | forall b. MultiPass (KeyRingData -> KeyRingAction b) 389 | forall b. MultiPass (KeyRingData -> KeyRingAction b)
@@ -403,6 +405,7 @@ instance Monad Kiki where
403 return x = SinglePass (const $ KeyRingAction x) 405 return x = SinglePass (const $ KeyRingAction x)
404 406
405 k >>= f = eval' $ fmapWithRT (\rt x -> eval rt (f x)) k 407 k >>= f = eval' $ fmapWithRT (\rt x -> eval rt (f x)) k
408 where (.:) = (.) . (.)
406 409
407eval :: KeyRingRuntime -> Kiki a -> KeyRingData -> a 410eval :: KeyRingRuntime -> Kiki a -> KeyRingData -> a
408eval rt (SinglePass f) kd = 411eval rt (SinglePass f) kd =
@@ -418,7 +421,11 @@ eval' k@(SinglePass pass) = SinglePass pass'
418 RunTimeAction g -> RunTimeAction (\rt -> g rt kd) 421 RunTimeAction g -> RunTimeAction (\rt -> g rt kd)
419eval' k@(MultiPass p kk) = MultiPass p kk' 422eval' k@(MultiPass p kk) = MultiPass p kk'
420 where 423 where
421 kk' = eval' $ fmap flip kk 424 kk' = fmap flip kk
425
426-}
427
428
422 429
423{- 430{-
424fmapWithRT g (SinglePass d@(KeyRingData { kAction = KeyRingAction v})) 431fmapWithRT g (SinglePass d@(KeyRingData { kAction = KeyRingAction v}))
@@ -431,6 +438,106 @@ fmapWithRT g (MultiPass p kk) = MultiPass p (fmapWithRT g' kk)
431-} 438-}
432 439
433 440
441data Kiki a =
442 SinglePass { passInfo :: KeyRingData
443 , rtAction :: KeyRingAction a }
444 | forall b.
445 MultiPass { passInfo :: KeyRingData
446 , passAction :: KeyRingAction b
447 , nextPass :: Kiki (b -> a)
448 }
449
450
451
452evalAction :: KeyRingRuntime -> KeyRingAction a -> a
453evalAction rt (KeyRingAction v) = v
454evalAction rt (RunTimeAction g) = g rt
455
456instance Monad KeyRingAction where
457 return x = KeyRingAction x
458 m >>= g = case m of
459 KeyRingAction v -> g v
460 RunTimeAction f -> RunTimeAction $ \rt -> evalAction rt (g $ f rt)
461
462instance Functor KeyRingAction where
463 fmap g (KeyRingAction v) = KeyRingAction $ g v
464 fmap g (RunTimeAction f) = RunTimeAction $ \rt -> g (f rt)
465
466{-
467argOut :: (KeyRingAction (a -> b)) -> a -> KeyRingAction b
468argOut = todo
469argIn :: (a -> KeyRingAction b) -> KeyRingAction (a->b)
470-}
471
472{-
473fmapWithRT :: (a -> KeyRingAction b) -> Kiki a -> Kiki b
474fmapWithRT g k@(SinglePass {}) = k { rtAction = action }
475 where
476 action = rtAction k >>= g
477fmapWithRT g (MultiPass p atn next) = MultiPass p atn next'
478 where
479 next' = fmapWithRT g' next {- next :: Kiki (x -> a) -}
480 -- g' :: ( (x->a) -> KeyRingAction b)
481 g' h = RunTimeAction $
482 \rt x -> case g (h x) of
483 KeyRingAction v -> v
484 RunTimeAction f -> f rt
485-}
486
487fmapWithRT :: KeyRingAction (a -> b) -> Kiki a -> Kiki b
488fmapWithRT g (SinglePass pass atn) = SinglePass pass atn'
489 where
490 atn' = g >>= flip fmap atn
491fmapWithRT g (MultiPass p atn next) = MultiPass p atn next'
492 where
493 next' = fmapWithRT g' next
494 g' = fmap (\gf h -> gf . h) g
495
496instance Functor Kiki where
497 fmap f k = fmapWithRT (return f) k
498
499instance Monad Kiki where
500 return x = SinglePass todo (return x)
501 k >>= f = kjoin $ fmap f k
502
503kikiAction :: Kiki a -> KeyRingAction a
504kikiAction (SinglePass _ atn) = atn
505kikiAction (MultiPass _ atn next) = do
506 x <- atn
507 g <- kikiAction next
508 return $ g x
509
510kjoin :: Kiki (Kiki a) -> Kiki a
511kjoin k = fmapWithRT eval' k
512 where
513 eval' :: KeyRingAction (Kiki a -> a)
514 eval' = RunTimeAction (\rt -> evalAction rt . kikiAction )
515
516 {-
517 kjoin :: Kiki (Kiki a) -> Kiki a
518 kjoin k = kjoin' (fmap kikiAction k)
519 where
520 ev rt (KeyRingAction v) = v
521 ev rt (RunTimeAction g) = g rt
522
523 kjoin' :: Kiki (KeyRingAction a) -> Kiki a
524 kjoin' (SinglePass pass atn) = SinglePass pass $ join atn
525 kjoin' (MultiPass pass atn next) = MultiPass pass atn next'
526 where
527 next' = todo
528 -}
529
530
531{-
532instance Functor Kiki where
533 fmap f (SinglePass pass atn)
534 = SinglePass pass (fmap f atn)
535 fmap f (MultiPass pass atn next)
536 = MultiPass pass atn (next >>= g)
537 where
538 g = todo
539-}
540
434{- 541{-
435data Kiki a = SinglePass (KeyRingData a) 542data Kiki a = SinglePass (KeyRingData a)
436 | forall b. MultiPass (KeyRingData b) (Kiki (b -> a)) 543 | forall b. MultiPass (KeyRingData b) (Kiki (b -> a))