pattern.ml 5.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
(********************************************************************)
(*                                                                  *)
(*  The Why3 Verification Platform   /   The Why3 Development Team  *)
(*  Copyright 2010-2012   --   INRIA - CNRS - Paris-Sud University  *)
(*                                                                  *)
(*  This software is distributed under the terms of the GNU Lesser  *)
(*  General Public License version 2.1, with the special exception  *)
(*  on linking described in file LICENSE.                           *)
(*                                                                  *)
(********************************************************************)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
11 12 13 14 15

open Ident
open Ty
open Term

16 17
module type Action = sig
  type action
18
  type branch
19
  val mk_let : vsymbol -> term -> action -> action
20 21
  val mk_branch : pattern -> action -> branch
  val mk_case : term -> branch list -> action
22
end
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
23

24
exception ConstructorExpected of lsymbol
25
exception NonExhaustive of pattern list
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
26

27 28
module Compile (X : Action) = struct
  open X
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
29

30
  let rec compile constructors tl rl = match tl,rl with
31
    | _, [] -> (* no actions *)
32
        let pl = List.map (fun t -> pat_wild (t_type t)) tl in
33
        raise (NonExhaustive pl)
34 35 36
    | [], (_,a) :: _ -> (* no terms, at least one action *)
        a
    | t :: tl, _ -> (* process the leftmost column *)
37
        let ty = t_type t in
Andrei Paskevich's avatar
Andrei Paskevich committed
38
        (* extract the set of constructors *)
39 40
        let css = match ty.ty_node with
          | Tyapp (ts,_) ->
Andrei Paskevich's avatar
Andrei Paskevich committed
41 42 43 44
              let csl = constructors ts in
              List.fold_left (fun s cs -> Sls.add cs s) Sls.empty csl
          | Tyvar _ ->
              Sls.empty
45
        in
Andrei Paskevich's avatar
Andrei Paskevich committed
46 47 48 49 50 51 52 53 54 55 56 57
        (* map every constructor occurring at the head
         * of the pattern list to the list of its args *)
        let types =
          let rec populate acc p = match p.pat_node with
            | Pwild | Pvar _ -> acc
            | Pas (p,_) -> populate acc p
            | Por (p,q) -> populate (populate acc p) q
            | Papp (fs,pl) when Sls.mem fs css -> Mls.add fs pl acc
            | Papp (fs,_) -> raise (ConstructorExpected fs)
          in
          let populate acc (pl,_) = populate acc (List.hd pl) in
          List.fold_left populate Mls.empty rl
58
        in
Andrei Paskevich's avatar
Andrei Paskevich committed
59 60
        (* dispatch every case to a primitive constructor/wild case *)
        let cases,wilds =
61
          let add_case fs pl a cases =
62
            Mls.change (function
63
              | None -> Some [pl,a]
64
              | Some rl -> Some ((pl,a)::rl)) fs cases
65
          in
66
          let union_cases pl a types cases =
67 68 69 70 71
            let add pl q = pat_wild q.pat_ty :: pl in
            let wild ql = [List.fold_left add pl ql, a] in
            let join _ wl rl = Some (List.append wl rl) in
            Mls.union join (Mls.map wild types) cases
          in
Andrei Paskevich's avatar
Andrei Paskevich committed
72 73 74 75
          let rec dispatch (pl,a) (cases,wilds) =
            let p = List.hd pl in let pl = List.tl pl in
            match p.pat_node with
              | Papp (fs,pl') ->
76
                  add_case fs (List.rev_append pl' pl) a cases, wilds
Andrei Paskevich's avatar
Andrei Paskevich committed
77 78 79 80 81 82
              | Por (p,q) ->
                  dispatch (p::pl, a) (dispatch (q::pl, a) (cases,wilds))
              | Pas (p,x) ->
                  dispatch (p::pl, mk_let x t a) (cases,wilds)
              | Pvar x ->
                  let a = mk_let x t a in
83
                  union_cases pl a types cases, (pl,a)::wilds
Andrei Paskevich's avatar
Andrei Paskevich committed
84
              | Pwild ->
85
                  union_cases pl a types cases, (pl,a)::wilds
Andrei Paskevich's avatar
Andrei Paskevich committed
86 87
          in
          List.fold_right dispatch rl (Mls.empty,[])
88
        in
Andrei Paskevich's avatar
Andrei Paskevich committed
89 90 91
        (* how to proceed if [t] is [Tapp(cs,al)] and [cs] is in [cases] *)
        let comp_cases cs al =
          try compile constructors (List.rev_append al tl) (Mls.find cs cases)
92
          with NonExhaustive pl ->
Andrei Paskevich's avatar
Andrei Paskevich committed
93 94 95 96 97 98
            let rec cont acc vl pl = match vl,pl with
              | (_::vl), (p::pl) -> cont (p::acc) vl pl
              | [], pl -> pat_app cs acc ty :: pl
              | _, _ -> assert false
            in
            raise (NonExhaustive (cont [] cs.ls_args pl))
99
        in
Andrei Paskevich's avatar
Andrei Paskevich committed
100 101 102 103 104 105
        (* how to proceed if [t] is not covered by [cases] *)
        let comp_wilds () =
          try compile constructors tl wilds
          with NonExhaustive pl ->
            let find_cs cs =
              if Mls.mem cs types then () else
106
              let tm = ty_match Mtv.empty (Opt.get cs.ls_value) ty in
Andrei Paskevich's avatar
Andrei Paskevich committed
107 108 109 110 111 112
              let wild ty = pat_wild (ty_inst tm ty) in
              let pw = pat_app cs (List.map wild cs.ls_args) ty in
              raise (NonExhaustive (pw :: pl))
            in
            Sls.iter find_cs css;
            raise (NonExhaustive (pat_wild ty :: pl))
113
        in
Andrei Paskevich's avatar
Andrei Paskevich committed
114 115 116 117 118 119 120 121
        (* assemble the primitive case statement *)
        match t.t_node with
        | _ when Mls.is_empty types ->
            comp_wilds ()
        | Tapp (cs,al) when Sls.mem cs css ->
            if Mls.mem cs types then comp_cases cs al else comp_wilds ()
        | _ ->
            let base =
122
              if Mls.set_submap css types then []
123
              else [mk_branch (pat_wild ty) (comp_wilds ())]
Andrei Paskevich's avatar
Andrei Paskevich committed
124 125 126 127 128 129
            in
            let add cs ql acc =
              let get_vs q = create_vsymbol (id_fresh "x") q.pat_ty in
              let vl = List.rev_map get_vs ql in
              let pl = List.rev_map pat_var vl in
              let al = List.rev_map t_var vl in
130
              mk_branch (pat_app cs pl ty) (comp_cases cs al) :: acc
Andrei Paskevich's avatar
Andrei Paskevich committed
131 132
            in
            mk_case t (Mls.fold add types base)
Jean-Christophe Filliâtre's avatar
Jean-Christophe Filliâtre committed
133 134 135

end

136
module CompileTerm  = Compile (struct
137
  type action = term
138
  type branch = term_branch
139
  let mk_let v s t  = t_let_close_simp v s t
140 141
  let mk_branch p t = t_close_branch p t
  let mk_case t bl  = t_case t bl
142 143
end)