Project

General

Profile

Haskell » History » Version 4

Grigoriy Volkov, 12/14/2018 04:41 PM

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