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