Skip to content
This repository
tag: v1346
Fetching contributors…

Cannot retrieve contributors at this time

file 455 lines (378 sloc) 12.815 kb
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA 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 Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)

(**
This module is for various utility functions on QML AST.
See also QmlAst, QmlAstWalk and QmlAstCons for more basic operations.

@author Louis Gesbert
@author Rudy Sicard
@author Esther Baruk
@author Mathieu Barbin
@author Valentin Gatien-Baron
@author Quentin Bourgerie
*)

(** {6 Design Note (TODO)} *)

(**
TODO:introduce in the ast definition a notion of structural ignored
directives for common utils. By default, an util must traverse all
these directives.

{[
type structural_ignored_directives = [ `tracker | `coerce, etc..]
let util ... =
let rec aux ... = function
| Directive (#structural_ignored_directive, e, ...) -> aux e
| ...
]}

TODO:refactor with Lang design.
Currently a lot of utils are considering some assemptions about the
expr that are applied to, and so have [assert false] or [invalid_arg]
in their implementation.
Instead of defining them to the type [expr], they should take arguments
of the corresponding constructor.

TODO:the utils should define a type 'a utils working an arguments of the
constructor.

{[
(* in Lang.Ast *)
type expr =
| A of int
| B of expr * expr
| Directive of (variant, ....)

module QmlUtils =
struct
module B =
struct
let utils_1 e e' = <impl>
instead of
let old_utils_1 e =
match e with
| B (e, e') -> <impl>
| _ -> assert false
end
end
]}
Typically, user of utils are doing things like :
{[
match e with
| ....
| B (a, b) ->
(* oups, I need an utils on B *)
B.utils a b
(* instead of *)
B.old_utils e
]}
*)

val map_exprident : QmlAst.code -> ( Ident.t -> Ident.t ) -> QmlAst.code

(**
take the deeper expression, go through all letin, lambda ...,
except paramater can be used to stop on a particular expression
*)
val get_deeper_expr : ?except:(QmlAst.expr-> bool) -> QmlAst.expr -> QmlAst.expr
(**
substitute old_expr new_expr global_expr
=>replace in global_expr every occurrence of old_expr (based on the annotation number) with new_expr
*)
val substitute : QmlAst.expr -> QmlAst.expr -> QmlAst.expr -> QmlAst.expr
(**
collect sub_expr global_expr
=>collect all occurrence of sub_expr (based on the annotation number)
*)
val collect : QmlAst.expr -> QmlAst.expr -> QmlAst.expr list
(**
collect_annot sub_expr_annot global_expr
=>same as collect, but you give only the annotation number
*)
val collect_annot : Annot.t -> QmlAst.expr -> QmlAst.expr list

(**
checks whether a Qml expression is expensive or not
*)
val is_expansive : QmlAst.expr -> bool
val is_expansive_strict : QmlAst.expr -> bool
val is_expansive_with_options : [`disabled|`normal|`strict] -> (QmlAst.expr -> bool)

module App : sig
  (**
The type of utils for an [Apply] Node
*)
  type 'a util = QmlAst.expr -> QmlAst.expr list -> 'a

  (**
Gives the number of arguments with which an expression is applied
Example:
{[
(((f x) y) z)
]}
The [nary_args_number] is [1].

{[
((f x) y z)
]}
The [nary_args_number] is [2].

Not Implemented because QmlAst is not ready yet for nary applications.
Currently the implementation is [assert false]

{[
| Apply (f, args) -> nary_args_number f args
]}
The argument [f] is not used, but we follow the interface of App.
*)
  val nary_args_number : int util

  (**
Gives the number of arguments with which an expression is applied
Example:
{[
(((f x) y) z t)
]}
The [curryfied_args_number] is [4].
@see "nary_args_number" for nary support
*)
  val curryfied_args_number : int util

 (** {6 Old Util: TODO use util type} *)

  val to_list : ?strict:bool -> QmlAst.expr -> QmlAst.expr list
  (** transform an apply() to a list of function :: args
@param strict if [true], means there must be at list one apply node
for this function to succeed (so the output list has at least length 2)
if [false], this function never fails
Default is [true]
*)

  val from_list : QmlAst.expr list -> QmlAst.expr
  (**
inverse of to_list, regardless of the [strict] flag that was used
@raise Invalid_argument if the list is empty
*)

end

module ExprIdent :
sig
  (**
get the uniq ident string from an ident expression
*)
  val string : QmlAst.expr -> string

  (**
change the content of an ident keeping the same annotation
*)
  val change_ident : QmlAst.ident -> QmlAst.expr -> QmlAst.expr

  (**
substitute all occurrences of an ident by another expression
dont care about annotmap and annot unicity, you are warned
can embbed side effet in the ident substitution map,
to count substitution for instance
*)
  val substitute : (unit -> QmlAst.expr) IdentMap.t -> QmlAst.expr -> QmlAst.expr
end

module Lambda :
sig
  (**
The type of utils for a [Lambda] Node

The functions must take the two arguments of the constructor :
the ident and the expression

Example :
{[val toto e = match e with
| Lambda (params, expr) -> QmlAstUtils.curryfied_arity params expr
| _ -> 0]}

gives the curryfied_arity of the expression [e], assuming that it is a lambda
or 0 instead.
*)
  type 'a util = QmlAst.ident list -> QmlAst.expr -> 'a

  (**
Returns the number of arguments of [lambda] taking in consideration the nary informations.
Examples :
{[
fun x -> fun y, z -> x + y
]}
The [nary_arity] is [1], where the [curryfied_arity] is [3]
*)
  val nary_arity : int util

  (**
Returns the number of arguments of a lambda without distinction between a function
which returns a function and its curryfied version.

{[
fun x -> fun y, z -> x + y
]}
The [curryfied_arity] is [3], where the [nary_arity] is [1]
*)
  val curryfied_arity : int util

  (** {6 Old Utils: TODO use lambda_utils type} *)

  (**
The function that count successive lambda node, traversing coercion node only
Examples :
count {[
fun x -> fun y, z -> x + y
]}

return 3

@deprecated use [curryfied_arity] instead
*)
  val count : QmlAst.expr -> int

  (** eta-expands an expression by int argument *)
  val eta_expand_ast : int -> QmlAst.expr -> QmlAst.expr

end

module Const : sig
  (**
Compare at compile time 2 constants.
Assume that the two constant are of the same type,
assert false otherwise.
*)
  val compare : QmlAst.const_expr -> QmlAst.const_expr -> int

  (**
Checks if compare returns 0
*)
  val equal : QmlAst.const_expr -> QmlAst.const_expr -> bool
end

module Coerce : sig
  (** remove all nested coerces at the root of the expression, and keep information to recoerce
as a list of annotation and type *)
  val uncoerce : QmlAst.expr -> QmlAst.expr * (Annot.label * QmlAst.ty) list

  (** inverse of uncoerce
warning: the annotations are restored as they were (no consistency with an annotmap in case of type change) *)
  val recoerce : QmlAst.expr -> (Annot.label * QmlAst.ty) list -> QmlAst.expr

  (** non reversible coerce removing *)
  val rm_coerces : QmlAst.expr -> QmlAst.expr
end

(** Returns an IdentSet.t of the free vars in an expression *)
module FreeVars :
sig
  val pat : QmlAst.pat -> IdentSet.t
  val expr : QmlAst.expr -> IdentSet.t

  val pat_fold : ('a -> Annot.t -> QmlAst.ident -> 'a) -> QmlAst.pat -> 'a -> 'a
  val expr_fold : ('a -> Annot.t -> QmlAst.ident -> 'a) -> QmlAst.expr -> 'a -> 'a
end

(**
Utils on Record node.
*)
module Record :
sig
  type 'a util = (string * QmlAst.expr) list -> 'a

  (**
uncons a tuple.
If the record is a standard tuple, ["f1", "f2", .. "fn"], will return
an option of the list of data of length [n]. if not, returns [None]
*)
  val uncons_tuple : QmlAst.expr list option util

  (**
special case for deprecated qml couple.
@deprecated Opa tuple are now the standard tuples.
*)
  val uncons_qml_tuple : QmlAst.expr list option util

  (**
Uncons a record returning the list of its fields and the list of its expressions
*)
  val uncons : (string list * QmlAst.expr list) util

  (**
Construct a record given the list of its fields and the list of expressions corresponding to the fields
*)
  val cons : string list -> QmlAst.expr list -> QmlAst.expr

end

(**
Utils on tuples (Decons).

In the opa compiler, a tuple is a standard record where fields are nammed ["f1", "f2", "f3", etc...]

In qml side, it used to have only couple ["fst", "snd"].
Qml couple are deprecated, but still used in existing code.
Please, do not use them in new code, use only standard tuples.

Some utils are related to Types. QmlAstUtils and QmlTypesUtils will be merged
into QmlUtils, taking part of qmllang.

For constructing expression or type, cf module [QmlAstCons]
*)
module Tuple :
sig
  (**
Will call internally [Record.uncons_tuple].
If the expression is not a record, will return [None]
*)
  val uncons : QmlAst.expr -> QmlAst.expr list option

  (**
Inspect a typeident and see if it is a tuple type. If the type is a tuple, returns its arity.
if not, returns [None]
*)
  val uncons_typeident : QmlAst.TypeIdent.t -> int option

  (**
Will call internally [Record.uncons_qml_couple].
If the expression is not a record, will return [None].
@deprecated Opa tuple are now the standard tuples.
*)
  val uncons_qml_tuple : QmlAst.expr -> QmlAst.expr list option
end

(**
Utils on patterns
*)
module Pat :
sig
  type 'a util = QmlAst.pat -> 'a

  (** Tell if the pat is [true] or [false], traversing patcoercion of [TypeName "bool"] or structural patcoerce *)
  val is_bool : bool option util
end

(**
Utils on Match node
*)
module Match :
sig
  type 'a util = QmlAst.expr -> (QmlAst.pat * QmlAst.expr) list -> 'a

  (** Uncons a match which was built with QmlAstCons.ifthenelse *)
  val uncons_ifthenelse : (QmlAst.expr * QmlAst.expr * QmlAst.expr) option util

  (**
Uncons a match, returning a triplet of
- the expression matched,
- the list of patterns,
- the list of resulting expressions
(elements in the last two lists have corresponding orders)
*)
  val uncons : (QmlAst.expr * QmlAst.pat list * QmlAst.expr list) util

  (**
Construct a match given
- its expression matched,
- its list of patterns,
- its list of resulting expressions
(elements in the last two lists have corresponding orders)
*)
  val cons : QmlAst.expr -> QmlAst.pat list -> QmlAst.expr list -> QmlAst.expr
end

(**
Utils on LetIn node
*)
module LetIn :
sig
  type 'a util = (QmlAst.ident * QmlAst.expr) list -> QmlAst.expr -> 'a

  (** Uncons a LetIn node, returning the pair of the list of declarations list and the last expression *)
  val uncons : ((QmlAst.ident * QmlAst.expr) list list * QmlAst.expr) util

  (** Construct a LetIn node given the list of declaration list and the last expression *)
  val cons : (QmlAst.ident * QmlAst.expr) list list -> QmlAst.expr -> QmlAst.expr
end

(**
Utils on LetRecIn node
*)
module LetRecIn :
sig
  type 'a util = (QmlAst.ident * QmlAst.expr) list -> QmlAst.expr -> 'a

  (** Uncons a LetRecIn node, returning the pair of the list of declarations list and the last expression *)
  val uncons : ((QmlAst.ident * QmlAst.expr) list list * QmlAst.expr) util

  (** Construct a LetRecIn node given the list of declaration list and the last expression *)
  val cons : (QmlAst.ident * QmlAst.expr) list list -> QmlAst.expr -> QmlAst.expr
end

(**
Utils on full code
*)
module Code :
sig

  (**
Insertion of a portion of code with dependencies.
The code is inserted just after the first dependencies starting from the end of the code.
Example:
{[
insert (["a"; "b"; "c"], "val h = a+b+c", ...)

g = 7
a = 6
b = 7
c = 7
h = a+b+c
...
]}
*)
  val insert :
    deps:IdentSet.t ->
    insert:QmlAst.code ->
    QmlAst.code ->
    QmlAst.code
end
Something went wrong with that request. Please try again.