-
Notifications
You must be signed in to change notification settings - Fork 0
/
Relation.hs
71 lines (61 loc) · 2.74 KB
/
Relation.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
-- This file is part of FairCheck
--
-- FairCheck is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published
-- by the Free Software Foundation, either version 3 of the License,
-- or (at your option) any later version.
--
-- FairCheck is distributed in the hope that it will be useful, but
-- WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-- General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with FairCheck. If not, see <http://www.gnu.org/licenses/>.
--
-- Copyright 2021 Luca Padovani
-- |Implementation of session type equality, unfair subtyping and fair subtyping
-- decision algorithms.
module Relation (equality, unfairSubtype, fairSubtype) where
import Data.Set (Set)
import qualified Data.Set as Set
import qualified Data.Map as Map
import Control.Monad (foldM)
import Tree (Tree)
import qualified Tree
import qualified Node
import qualified Predicate
-- | Shortcut for a binary relation over session types.
type Relation u = Tree u -> Tree u -> Bool
-- | Generic coinductive relation over session types. The provided
-- 'Tree.Comparator' is used to compute the list of pairs of subtrees that must
-- be compared in turn.
relation :: Ord u => Tree.Comparator u -> Relation u
relation cmp = aux Set.empty
where
aux vset f g | Set.member (f, g) vset = True
aux vset f g | Just (es, rs) <- cmp f g =
all (uncurry equality) es &&
all (uncurry (aux (Set.insert (f, g) vset))) rs
aux _ _ _ = False
-- | Equality relation over session types.
equality :: Ord u => Relation u
equality = relation Tree.equalityCmp
-- | Unfair subtyping relation over session types.
unfairSubtype :: Ord u => Bool -> Relation u
unfairSubtype weak = relation (if weak then Tree.weakSubCmp else Tree.strongSubCmp)
-- | Convergence relation over session types (see 'fairSubtype' below for
-- references to the papers in which the convergence relation is defined).
converge :: Ord u => Relation u
converge f g = not (Predicate.viable (Tree.difference f g))
-- | Fair subtyping relation over session types (Table 1). This implementation
-- of fair subtyping is based on the characterization of fair subtyping
-- described in the papers /Fair Subtyping for Multi-Party Session Types/
-- <http://dx.doi.org/10.1017/S096012951400022X> and /Fair Subtyping for Open/
-- /Session Types/ <http://dx.doi.org/10.1007/978-3-642-39212-2_34>.
fairSubtype :: Ord u => Bool -> Relation u
fairSubtype weak = relation cmp
where
cmp f g | converge f g = rel f g
| otherwise = Nothing
rel = if weak then Tree.weakSubCmp else Tree.strongSubCmp