on 16fe40d help sign in
Idris-dev/lib/Language/Reflection.idr
188 lines 5.4 kB history raw
1module Language.Reflection
3import Prelude
5%access public
7data TTName = UN String
8 -- ^ User-provided name
9 | NS TTName (List String)
10 -- ^ Root, namespaces
11 | MN Int String
12 -- ^ Machine chosen names
13 | NErased
14 -- ^ Name of somethng which is never used in scope
16implicit
17userSuppliedName : String -> TTName
18userSuppliedName = UN
20data TTUExp = UVar Int
21 -- ^ universe variable
22 | UVal Int
23 -- ^ explicit universe variable
25-- | Primitive constants
26data Const = I Int | BI Nat | Fl Float | Ch Char | Str String
27 | IType | BIType | FlType | ChType | StrType
28 | B8 Bits8 | B16 Bits16 | B32 Bits32 | B64 Bits64
29 | B8Type | B16Type | B32Type | B64Type
30 | PtrType | VoidType | Forgot
32abstract class ReflConst (a : Type) where
33 toConst : a -> Const
35instance ReflConst Int where
36 toConst x = I x
38instance ReflConst Nat where
39 toConst = BI
41instance ReflConst Float where
42 toConst = Fl
44instance ReflConst Char where
45 toConst = Ch
47instance ReflConst String where
48 toConst = Str
50instance ReflConst Bits8 where
51 toConst = B8
53instance ReflConst Bits16 where
54 toConst = B16
56instance ReflConst Bits32 where
57 toConst = B32
59instance ReflConst Bits64 where
60 toConst = B64
62implicit
63reflectConstant: (ReflConst a) => a -> Const
64reflectConstant = toConst
67-- | Types of named references
68data NameType = Bound
69 -- ^ reference which is just bound, e.g. by intro
70 | Ref
71 -- ^ reference to a variable
72 | DCon Int Int
73 -- ^ constructor with tag and number
74 | TCon Int Int
75 -- ^ type constructor with tag and number
77-- | Types annotations for bound variables in different
78-- binding contexts
79data Binder a = Lam a
80 | Pi a
81 | Let a a
82 | NLet a a
83 | Hole a
84 | GHole a
85 | Guess a a
86 | PVar a
87 | PVTy a
90instance Functor Binder where
91 map f (Lam x) = Lam (f x)
92 map f (Pi x) = Pi (f x)
93 map f (Let x y) = Let (f x) (f y)
94 map f (NLet x y) = NLet (f x) (f y)
95 map f (Hole x) = Hole (f x)
96 map f (GHole x) = GHole (f x)
97 map f (Guess x y) = Guess (f x) (f y)
98 map f (PVar x) = PVar (f x)
99 map f (PVTy x) = PVTy (f x)
101instance Foldable Binder where
102 foldr f z (Lam x) = f x z
103 foldr f z (Pi x) = f x z
104 foldr f z (Let x y) = f x (f y z)
105 foldr f z (NLet x y) = f x (f y z)
106 foldr f z (Hole x) = f x z
107 foldr f z (GHole x) = f x z
108 foldr f z (Guess x y) = f x (f y z)
109 foldr f z (PVar x) = f x z
110 foldr f z (PVTy x) = f x z
112instance Traversable Binder where
113 traverse f (Lam x) = [| Lam (f x) |]
114 traverse f (Pi x) = [| Pi (f x) |]
115 traverse f (Let x y) = [| Let (f x) (f y) |]
116 traverse f (NLet x y) = [| NLet (f x) (f y) |]
117 traverse f (Hole x) = [| Hole (f x) |]
118 traverse f (GHole x) = [| GHole (f x) |]
119 traverse f (Guess x y) = [| Guess (f x) (f y) |]
120 traverse f (PVar x) = [| PVar (f x) |]
121 traverse f (PVTy x) = [| PVTy (f x) |]
124-- | Reflection of the well typed core language
125data TT = P NameType TTName TT
126 -- ^ named binders
127 | V Int
128 -- ^ variables
129 | Bind TTName (Binder TT) TT
130 -- ^ type annotated named bindings
131 | App TT TT
132 -- ^ (named) application of a function to a value
133 | TConst Const
134 -- ^ constants
135 | Proj TT Int
136 -- ^ argument projection; runtime only
137 | Erased
138 -- ^ erased terms
139 | Impossible
140 -- ^ impossible terms
141 | TType TTUExp
142 -- ^ types
144-- | Raw terms without types
145data Raw = Var TTName
146 | RBind TTName (Binder Raw) Raw
147 | RApp Raw Raw
148 | RType
149 | RForce Raw
150 | RConstant Const
152data Tactic = Try Tactic Tactic
153 -- ^ try the first tactic and resort to the second one on failure
154 | GoalType String Tactic
155 -- ^ only run if the goal has the right type
156 | Refine TTName
157 -- ^ resolve function name, find matching arguments in the
158 -- context and compute the proof target
159 | Seq Tactic Tactic
160 -- ^ apply both tactics in sequence
161 | Trivial
162 -- ^ intelligently construct the proof target from the context
163 | Solve
164 -- ^ infer the proof target from the context
165 | Intros
166 -- ^ introduce all variables into the context
167 | Intro TTName
168 -- ^ introduce a named variable into the context, use the
169 -- first one if the given name is not found
170 | ApplyTactic TT
171 -- ^ invoke the reflected rep. of another tactic
172 | Reflect TT
173 -- ^ turn a value into its reflected representation
174 | Fill Raw
175 -- ^ turn a raw value back into a term
176 | Exact TT
177 -- ^ use the given value to conclude the proof
178 | Focus TTName
179 -- ^ focus a named hole
180 | Rewrite TT
181 -- ^ rewrite using the reflected rep. of a equality proof
182 | LetTac TTName TT
183 -- ^ name a reflected term
184 | LetTacTy TTName TT TT
185 -- ^ name a reflected term and type it
186 | Compute
187 -- ^ normalise the context