Haskell » History » Version 3
Grigoriy Volkov, 12/11/2018 03:34 PM
1 | 1 | Grigoriy Volkov | h1. Haskell |
---|---|---|---|
2 | |||
3 | h2. libs |
||
4 | |||
5 | * "concat":https://github.com/conal/concat ("z3cat":https://github.com/jwiegley/z3cat/blob/master/test/Main.hs || "smt example":https://github.com/conal/concat/blob/master/examples/src/ConCat/SMT.hs) — *"Compiling to categories":http://conal.net/papers/compiling-to-categories/* — compiler plugin for translating normal Haskell functions (on standard types) to SMT, etc. |
||
6 | * "SBV: SMT Based Verification":https://leventerkok.github.io/sbv/ — translates Haskell functions on custom symbolic types |
||
7 | * "refined":http://nikita-volkov.github.io/refined/ — refinement types <pre>type ProperFraction = Refined (And (Not (LessThan 0)) (Not (GreaterThan 1))) Double</pre> |
||
8 | * "hedgehog":https://github.com/hedgehogqa/haskell-hedgehog — random test gen (QuickCheck) |
||
9 | 2 | Grigoriy Volkov | * "GHC.Stack":https://hackage.haskell.org/package/base-4.10.0.0/docs/GHC-Stack.html — can get code position for DSL |
10 | * "Data.Sequence":http://hackage.haskell.org/package/containers-0.6.0.1/docs/Data-Sequence.html — list with fast append on both sides |
||
11 | |||
12 | h2. exts |
||
13 | |||
14 | * "RebindableSyntax":https://ocharles.org.uk/guest-posts/2014-12-06-rebindable-syntax.html — overloading built-in operators / do notation |
||
15 | * "NullaryTypeClasses":https://ocharles.org.uk/posts/2014-12-10-nullary-type-classes.html — global implementation of one thing |
||
16 | 3 | Grigoriy Volkov | |
17 | h2. snippets |
||
18 | |||
19 | h3. Using Maybe Monad for checks |
||
20 | |||
21 | <pre> |
||
22 | {-# LANGUAGE LambdaCase, RecordWildCards #-} |
||
23 | |||
24 | import Data.Maybe (isJust, mapMaybe) |
||
25 | import Data.List (nub) |
||
26 | |||
27 | data Group = X25519 | Secp384 |
||
28 | deriving (Eq) |
||
29 | |||
30 | data KeyShareData = KeyShareClientHello [Group] |
||
31 | | KeyShareHelloRetryRequest Group |
||
32 | |||
33 | data Ext = SupportedGroups [Group] |
||
34 | | KeyShares KeyShareData |
||
35 | |||
36 | check :: [Ext] -> [Ext] -> Bool |
||
37 | check eCH eHRR = isJust $ do |
||
38 | sgCH <- exactlyOne $ mapMaybe (\case SupportedGroups gs -> Just gs; _ -> Nothing) eCH |
||
39 | continueIf $ nub sgCH == sgCH -- no duplicates allowed |
||
40 | ksCH <- (\case KeyShareClientHello gs -> Just gs; _ -> Nothing) =<< |
||
41 | exactlyOne (mapMaybe (\case KeyShares ks -> Just ks; _ -> Nothing) eCH) |
||
42 | ksHRR <- (\case KeyShareHelloRetryRequest g -> Just g; _ -> Nothing) =<< |
||
43 | exactlyOne (mapMaybe (\case KeyShares ks -> Just ks; _ -> Nothing) eHRR) |
||
44 | continueIf $ nub ksCH == ksCH -- no duplicates allowed |
||
45 | continueIf $ ksHRR `elem` sgCH -- group must be supported |
||
46 | continueIf $ ksHRR `notElem` ksCH -- key share must not have been already sent |
||
47 | |||
48 | exactlyOne :: [a] -> Maybe a |
||
49 | exactlyOne [x] = Just x |
||
50 | exactlyOne _ = Nothing |
||
51 | |||
52 | continueIf :: Bool -> Maybe () |
||
53 | continueIf True = Just () |
||
54 | continueIf False = Nothing |
||
55 | </pre> |