on 9792d64 help sign in
Idris-dev/lib/Language/Reflection.idr
176 lines 5.1 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 fmap f (Lam x) = Lam (f x)
90 fmap f (Pi x) = Pi (f x)
91 fmap f (Let x y) = Let (f x) (f y)
92 fmap f (NLet x y) = NLet (f x) (f y)
93 fmap f (Hole x) = Hole (f x)
94 fmap f (GHole x) = GHole (f x)
95 fmap f (Guess x y) = Guess (f x) (f y)
96 fmap f (PVar x) = PVar (f x)
97 fmap f (PVTy x) = PVTy (f x)
99total
100traverse : (Applicative f) => (a -> f b) -> Binder a -> f (Binder b)
101traverse f (Lam x) = [| Lam (f x) |]
102traverse f (Pi x) = [| Pi (f x) |]
103traverse f (Let x y) = [| Let (f x) (f y) |]
104traverse f (NLet x y) = [| NLet (f x) (f y) |]
105traverse f (Hole x) = [| Hole (f x) |]
106traverse f (GHole x) = [| GHole (f x) |]
107traverse f (Guess x y) = [| Guess (f x) (f y) |]
108traverse f (PVar x) = [| PVar (f x) |]
109traverse f (PVTy x) = [| PVTy (f x) |]
112-- | Reflection of the well typed core language
113data TT = P NameType TTName TT
114 -- ^ named binders
115 | V Int
116 -- ^ variables
117 | Bind TTName (Binder TT) TT
118 -- ^ type annotated named bindings
119 | App TT TT
120 -- ^ (named) application of a function to a value
121 | TConst Const
122 -- ^ constants
123 | Proj TT Int
124 -- ^ argument projection; runtime only
125 | Erased
126 -- ^ erased terms
127 | Impossible
128 -- ^ impossible terms
129 | TType TTUExp
130 -- ^ types
132-- | Raw terms without types
133data Raw = Var TTName
134 | RBind TTName (Binder Raw) Raw
135 | RApp Raw Raw
136 | RType
137 | RForce Raw
138 | RConstant Const
140data Tactic = Try Tactic Tactic
141 -- ^ try the first tactic and resort to the second one on failure
142 | GoalType String Tactic
143 -- ^ only run if the goal has the right type
144 | Refine TTName
145 -- ^ resolve function name, find matching arguments in the
146 -- context and compute the proof target
147 | Seq Tactic Tactic
148 -- ^ apply both tactics in sequence
149 | Trivial
150 -- ^ intelligently construct the proof target from the context
151 | Solve
152 -- ^ infer the proof target from the context
153 | Intros
154 -- ^ introduce all variables into the context
155 | Intro TTName
156 -- ^ introduce a named variable into the context, use the
157 -- first one if the given name is not found
158 | ApplyTactic TT
159 -- ^ invoke the reflected rep. of another tactic
160 | Reflect TT
161 -- ^ turn a value into its reflected representation
162 | Fill Raw
163 -- ^ turn a raw value back into a term
164 | Exact TT
165 -- ^ use the given value to conclude the proof
166 | Focus TTName
167 -- ^ focus a named hole
168 | Rewrite TT
169 -- ^ rewrite using the reflected rep. of a equality proof
170 | LetTac TTName TT
171 -- ^ name a reflected term
172 | LetTacTy TTName TT TT
173 -- ^ name a reflected term and type it
174 | Compute
175 -- ^ normalise the context