summaryrefslogtreecommitdiff
path: root/src/CC/Name.idr
blob: d1df2535dabba2cd881c1a711e0388767bffa403 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
module CC.Name

import public Control.Relation

import Data.List
import Data.Singleton
import Data.Stream

import Decidable.Equality

import Text.Bounded
import Text.PrettyPrint.Prettyprinter

infix 4 ~~

-- Definitions -----------------------------------------------------------------

||| Names of variables.
export
Name : Type
Name = WithBounds String

%name Name n, m

||| Equivalence relation on names.
export
(~~) : Rel Name
m ~~ n = m.val = n.val

%name (~~) prf

-- Interfaces ------------------------------------------------------------------

export
Interpolation Name where
  interpolate n = n.val

export
Pretty Name where
  pretty n = pretty n.val

export
Reflexive Name (~~) where
  reflexive = Refl

export
Symmetric Name (~~) where
  symmetric prf = sym prf

export
Transitive Name (~~) where
  transitive prf1 prf2 = trans prf1 prf2

export Equivalence Name (~~) where

-- Operations ------------------------------------------------------------------

||| Constructs a new name from a bounded String.
export
MkName : WithBounds String -> Name
MkName = id

||| Decides if two names are equivalent
export
decEquiv : (m, n : Name) -> Dec (m ~~ n)
decEquiv m n = decEq m.val n.val

||| Returns the bounds of `name`, if any.
export
bounds : (name : Name) -> Maybe Bounds
bounds name = if name.isIrrelevant then Nothing else Just name.bounds

||| Returns a new name not in the set.
export
fresh : Foldable f => f Name -> Name
fresh xs =
  irrelevantBounds $
  select (unfoldr (\n => ("_\{show n}", S n)) 0) $
  sort $
  foldMap (singleton . val) xs
  where
  select : Ord a => Stream a -> List a -> a
  select (x :: xs) [] = x
  select (x :: xs) (y :: ys) =
    case compare x y of
      LT => x
      EQ => select xs ys
      GT => select (x :: xs) ys

export
fresh' : Foldable f => Singleton {a = f Name} xs -> Name
fresh' (Val xs) = fresh xs