diff options
author | joe <joe@jerkface.net> | 2014-04-14 15:26:56 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2014-04-14 15:26:56 -0400 |
commit | ed9ecad17bf8d916eb8780b0e9a46597aeab7ae3 (patch) | |
tree | 6680efcfc7e03c981d5118c6ec8eb4984644635c /KeyRing.hs | |
parent | 7f1fb5ac9a1dfab0f44ab3b6327115d0eb4b4488 (diff) |
another alternative monad
Diffstat (limited to 'KeyRing.hs')
-rw-r--r-- | KeyRing.hs | 109 |
1 files changed, 108 insertions, 1 deletions
@@ -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 | {- | ||
385 | data Kiki a = | 387 | data 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 | ||
407 | eval :: KeyRingRuntime -> Kiki a -> KeyRingData -> a | 410 | eval :: KeyRingRuntime -> Kiki a -> KeyRingData -> a |
408 | eval rt (SinglePass f) kd = | 411 | eval 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) |
419 | eval' k@(MultiPass p kk) = MultiPass p kk' | 422 | eval' 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 | {- |
424 | fmapWithRT g (SinglePass d@(KeyRingData { kAction = KeyRingAction v})) | 431 | fmapWithRT 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 | ||
441 | data 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 | |||
452 | evalAction :: KeyRingRuntime -> KeyRingAction a -> a | ||
453 | evalAction rt (KeyRingAction v) = v | ||
454 | evalAction rt (RunTimeAction g) = g rt | ||
455 | |||
456 | instance 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 | |||
462 | instance Functor KeyRingAction where | ||
463 | fmap g (KeyRingAction v) = KeyRingAction $ g v | ||
464 | fmap g (RunTimeAction f) = RunTimeAction $ \rt -> g (f rt) | ||
465 | |||
466 | {- | ||
467 | argOut :: (KeyRingAction (a -> b)) -> a -> KeyRingAction b | ||
468 | argOut = todo | ||
469 | argIn :: (a -> KeyRingAction b) -> KeyRingAction (a->b) | ||
470 | -} | ||
471 | |||
472 | {- | ||
473 | fmapWithRT :: (a -> KeyRingAction b) -> Kiki a -> Kiki b | ||
474 | fmapWithRT g k@(SinglePass {}) = k { rtAction = action } | ||
475 | where | ||
476 | action = rtAction k >>= g | ||
477 | fmapWithRT 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 | |||
487 | fmapWithRT :: KeyRingAction (a -> b) -> Kiki a -> Kiki b | ||
488 | fmapWithRT g (SinglePass pass atn) = SinglePass pass atn' | ||
489 | where | ||
490 | atn' = g >>= flip fmap atn | ||
491 | fmapWithRT 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 | |||
496 | instance Functor Kiki where | ||
497 | fmap f k = fmapWithRT (return f) k | ||
498 | |||
499 | instance Monad Kiki where | ||
500 | return x = SinglePass todo (return x) | ||
501 | k >>= f = kjoin $ fmap f k | ||
502 | |||
503 | kikiAction :: Kiki a -> KeyRingAction a | ||
504 | kikiAction (SinglePass _ atn) = atn | ||
505 | kikiAction (MultiPass _ atn next) = do | ||
506 | x <- atn | ||
507 | g <- kikiAction next | ||
508 | return $ g x | ||
509 | |||
510 | kjoin :: Kiki (Kiki a) -> Kiki a | ||
511 | kjoin 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 | {- | ||
532 | instance 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 | {- |
435 | data Kiki a = SinglePass (KeyRingData a) | 542 | data Kiki a = SinglePass (KeyRingData a) |
436 | | forall b. MultiPass (KeyRingData b) (Kiki (b -> a)) | 543 | | forall b. MultiPass (KeyRingData b) (Kiki (b -> a)) |