adamc@16: (* Copyright (c) 2008, Adam Chlipala adamc@16: * All rights reserved. adamc@16: * adamc@16: * Redistribution and use in source and binary forms, with or without adamc@16: * modification, are permitted provided that the following conditions are met: adamc@16: * adamc@16: * - Redistributions of source code must retain the above copyright notice, adamc@16: * this list of conditions and the following disclaimer. adamc@16: * - Redistributions in binary form must reproduce the above copyright notice, adamc@16: * this list of conditions and the following disclaimer in the documentation adamc@16: * and/or other materials provided with the distribution. adamc@16: * - The names of contributors may not be used to endorse or promote products adamc@16: * derived from this software without specific prior written permission. adamc@16: * adamc@16: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" adamc@16: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE adamc@16: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE adamc@16: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE adamc@16: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR adamc@16: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF adamc@16: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS adamc@16: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN adamc@16: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) adamc@16: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE adamc@16: * POSSIBILITY OF SUCH DAMAGE. adamc@16: *) adamc@16: adamc@16: structure CoreEnv :> CORE_ENV = struct adamc@16: adamc@16: open Core adamc@16: adamc@16: structure U = CoreUtil adamc@16: adamc@16: structure IM = IntBinaryMap adamc@16: adamc@16: adamc@16: (* AST utility functions *) adamc@16: adamc@626: val liftKindInKind = adamc@626: U.Kind.mapB {kind = fn bound => fn k => adamc@626: case k of adamc@626: KRel xn => adamc@626: if xn < bound then adamc@626: k adamc@626: else adamc@626: KRel (xn + 1) adamc@626: | _ => k, adamc@626: bind = fn (bound, _) => bound + 1} adamc@626: adamc@626: val liftKindInCon = adamc@626: U.Con.mapB {kind = fn bound => fn k => adamc@626: case k of adamc@626: KRel xn => adamc@626: if xn < bound then adamc@626: k adamc@626: else adamc@626: KRel (xn + 1) adamc@626: | _ => k, adamc@626: con = fn _ => fn c => c, adamc@626: bind = fn (bound, U.Con.RelK _) => bound + 1 adamc@626: | (bound, _) => bound} adamc@626: adamc@626: val liftKindInExp = adamc@626: U.Exp.mapB {kind = fn bound => fn k => adamc@626: case k of adamc@626: KRel xn => adamc@626: if xn < bound then adamc@626: k adamc@626: else adamc@626: KRel (xn + 1) adamc@626: | _ => k, adamc@626: con = fn _ => fn c => c, adamc@626: exp = fn _ => fn e => e, adamc@626: bind = fn (bound, U.Exp.RelK _) => bound + 1 adamc@626: | (bound, _) => bound} adamc@626: adamc@16: val liftConInCon = adamc@626: U.Con.mapB {kind = fn _ => fn k => k, adamc@16: con = fn bound => fn c => adamc@16: case c of adamc@16: CRel xn => adamc@16: if xn < bound then adamc@16: c adamc@16: else adamc@16: CRel (xn + 1) adamc@16: | _ => c, adamc@626: bind = fn (bound, U.Con.RelC _) => bound + 1 adamc@16: | (bound, _) => bound} adamc@16: adamc@16: val lift = liftConInCon 0 adamc@16: adamc@193: val subConInCon = adamc@626: U.Con.mapB {kind = fn _ => fn k => k, adamc@193: con = fn (xn, rep) => fn c => adamc@193: case c of adamc@193: CRel xn' => adamc@193: (case Int.compare (xn', xn) of adamc@193: EQUAL => #1 rep adamc@193: | GREATER => CRel (xn' - 1) adamc@193: | LESS => c) adamc@193: | _ => c, adamc@626: bind = fn ((xn, rep), U.Con.RelC _) => (xn+1, liftConInCon 0 rep) adamc@193: | (ctx, _) => ctx} adamc@193: adamc@16: adamc@315: val liftConInExp = adamc@626: U.Exp.mapB {kind = fn _ => fn k => k, adamc@315: con = fn bound => fn c => adamc@315: case c of adamc@315: CRel xn => adamc@315: if xn < bound then adamc@315: c adamc@315: else adamc@315: CRel (xn + 1) adamc@315: | _ => c, adamc@315: exp = fn _ => fn e => e, adamc@315: bind = fn (bound, U.Exp.RelC _) => bound + 1 adamc@315: | (bound, _) => bound} adamc@315: adamc@315: val subConInExp = adamc@626: U.Exp.mapB {kind = fn _ => fn k => k, adamc@315: con = fn (xn, rep) => fn c => adamc@417: case c of adamc@417: CRel xn' => adamc@417: (case Int.compare (xn', xn) of adamc@417: EQUAL => #1 rep adamc@417: | GREATER => CRel (xn' - 1) adamc@417: | LESS => c) adamc@417: | _ => c, adamc@315: exp = fn _ => fn e => e, adamc@315: bind = fn ((xn, rep), U.Exp.RelC _) => (xn+1, liftConInCon 0 rep) adamc@315: | (ctx, _) => ctx} adamc@315: adamc@443: val liftExpInExp = adamc@626: U.Exp.mapB {kind = fn _ => fn k => k, adamc@443: con = fn _ => fn c => c, adamc@443: exp = fn bound => fn e => adamc@443: case e of adamc@443: ERel xn => adamc@443: if xn < bound then adamc@443: e adamc@443: else adamc@443: ERel (xn + 1) adamc@443: | _ => e, adamc@443: bind = fn (bound, U.Exp.RelE _) => bound + 1 adamc@443: | (bound, _) => bound} adamc@443: adamc@443: val subExpInExp = adamc@626: U.Exp.mapB {kind = fn _ => fn k => k, adamc@443: con = fn _ => fn c => c, adamc@443: exp = fn (xn, rep) => fn e => adamc@443: case e of adamc@443: ERel xn' => adamc@443: (case Int.compare (xn', xn) of adamc@443: EQUAL => #1 rep adamc@443: | GREATER=> ERel (xn' - 1) adamc@443: | LESS => e) adamc@443: | _ => e, adamc@443: bind = fn ((xn, rep), U.Exp.RelE _) => (xn+1, liftExpInExp 0 rep) adamc@443: | ((xn, rep), U.Exp.RelC _) => (xn, liftConInExp 0 rep) adamc@443: | (ctx, _) => ctx} adamc@443: adamc@16: (* Back to environments *) adamc@16: adamc@16: exception UnboundRel of int adamc@16: exception UnboundNamed of int adamc@16: adamc@16: type env = { adamc@626: relK : string list, adamc@626: adamc@16: relC : (string * kind) list, adamc@16: namedC : (string * kind * con option) IM.map, adamc@16: adamc@192: datatypes : (string * string list * (string * int * con option) list) IM.map, adamc@192: constructors : (string * string list * con option * int) IM.map, adamc@168: adamc@16: relE : (string * con) list, adamc@109: namedE : (string * con * exp option * string) IM.map adamc@16: } adamc@16: adamc@16: val empty = { adamc@626: relK = [], adamc@626: adamc@16: relC = [], adamc@16: namedC = IM.empty, adamc@16: adamc@168: datatypes = IM.empty, adamc@177: constructors = IM.empty, adamc@168: adamc@16: relE = [], adamc@16: namedE = IM.empty adamc@16: } adamc@16: adamc@626: fun pushKRel (env : env) x = adamc@626: {relK = x :: #relK env, adamc@626: adamc@626: relC = map (fn (x, k) => (x, liftKindInKind 0 k)) (#relC env), adamc@626: namedC = #namedC env, adamc@626: adamc@626: relE = map (fn (x, c) => (x, liftKindInCon 0 c)) (#relE env), adamc@626: namedE = #namedE env, adamc@626: adamc@626: datatypes = #datatypes env, adamc@626: constructors = #constructors env adamc@626: } adamc@626: adamc@626: fun lookupKRel (env : env) n = adamc@626: (List.nth (#relK env, n)) adamc@626: handle Subscript => raise UnboundRel n adamc@626: adamc@16: fun pushCRel (env : env) x k = adamc@626: {relK = #relK env, adamc@626: adamc@626: relC = (x, k) :: #relC env, adamc@16: namedC = IM.map (fn (x, k, co) => (x, k, Option.map lift co)) (#namedC env), adamc@16: adamc@168: datatypes = #datatypes env, adamc@177: constructors = #constructors env, adamc@168: adamc@16: relE = map (fn (x, c) => (x, lift c)) (#relE env), adamc@109: namedE = IM.map (fn (x, c, eo, s) => (x, lift c, eo, s)) (#namedE env)} adamc@16: adamc@16: fun lookupCRel (env : env) n = adamc@16: (List.nth (#relC env, n)) adamc@16: handle Subscript => raise UnboundRel n adamc@16: adamc@16: fun pushCNamed (env : env) x n k co = adamc@626: {relK = #relK env, adamc@626: adamc@626: relC = #relC env, adamc@16: namedC = IM.insert (#namedC env, n, (x, k, co)), adamc@16: adamc@168: datatypes = #datatypes env, adamc@177: constructors = #constructors env, adamc@168: adamc@16: relE = #relE env, adamc@16: namedE = #namedE env} adamc@16: adamc@16: fun lookupCNamed (env : env) n = adamc@16: case IM.find (#namedC env, n) of adamc@16: NONE => raise UnboundNamed n adamc@16: | SOME x => x adamc@16: adamc@192: fun pushDatatype (env : env) x n xs xncs = adamc@626: {relK = #relK env, adamc@626: adamc@626: relC = #relC env, adamc@168: namedC = #namedC env, adamc@168: adamc@192: datatypes = IM.insert (#datatypes env, n, (x, xs, xncs)), adamc@182: constructors = foldl (fn ((x, n', to), constructors) => adamc@192: IM.insert (constructors, n', (x, xs, to, n))) adamc@177: (#constructors env) xncs, adamc@168: adamc@168: relE = #relE env, adamc@168: namedE = #namedE env} adamc@168: adamc@168: fun lookupDatatype (env : env) n = adamc@168: case IM.find (#datatypes env, n) of adamc@168: NONE => raise UnboundNamed n adamc@168: | SOME x => x adamc@168: adamc@177: fun lookupConstructor (env : env) n = adamc@177: case IM.find (#constructors env, n) of adamc@177: NONE => raise UnboundNamed n adamc@177: | SOME x => x adamc@177: adamc@16: fun pushERel (env : env) x t = adamc@626: {relK = #relK env, adamc@626: adamc@626: relC = #relC env, adamc@16: namedC = #namedC env, adamc@16: adamc@168: datatypes = #datatypes env, adamc@177: constructors = #constructors env, adamc@168: adamc@16: relE = (x, t) :: #relE env, adamc@16: namedE = #namedE env} adamc@16: adamc@16: fun lookupERel (env : env) n = adamc@16: (List.nth (#relE env, n)) adamc@16: handle Subscript => raise UnboundRel n adamc@16: adamc@109: fun pushENamed (env : env) x n t eo s = adamc@626: {relK = #relK env, adamc@626: adamc@626: relC = #relC env, adamc@16: namedC = #namedC env, adamc@16: adamc@168: datatypes = #datatypes env, adamc@177: constructors = #constructors env, adamc@168: adamc@16: relE = #relE env, adamc@109: namedE = IM.insert (#namedE env, n, (x, t, eo, s))} adamc@16: adamc@16: fun lookupENamed (env : env) n = adamc@16: case IM.find (#namedE env, n) of adamc@16: NONE => raise UnboundNamed n adamc@16: | SOME x => x adamc@16: adamc@163: fun declBinds env (d, loc) = adamc@16: case d of adamc@16: DCon (x, n, k, c) => pushCNamed env x n k (SOME c) adamc@192: | DDatatype (x, n, xs, xncs) => adamc@163: let adamc@192: val env = pushDatatype env x n xs xncs adamc@163: val env = pushCNamed env x n (KType, loc) NONE adamc@163: in adamc@163: foldl (fn ((x', n', NONE), env) => pushENamed env x' n' (CNamed n, loc) NONE "" adamc@163: | ((x', n', SOME t), env) => pushENamed env x' n' (TFun (t, (CNamed n, loc)), loc) NONE "") adamc@163: env xncs adamc@163: end adamc@109: | DVal (x, n, t, e, s) => pushENamed env x n t (SOME e) s adamc@126: | DValRec vis => foldl (fn ((x, n, t, e, s), env) => pushENamed env x n t NONE s) env vis adamc@109: | DExport _ => env adamc@247: | DTable (x, n, c, s) => adamc@247: let adamc@338: val t = (CApp ((CFfi ("Basis", "sql_table"), loc), c), loc) adamc@338: in adamc@338: pushENamed env x n t NONE s adamc@338: end adamc@338: | DSequence (x, n, s) => adamc@338: let adamc@338: val t = (CFfi ("Basis", "sql_sequence"), loc) adamc@247: in adamc@247: pushENamed env x n t NONE s adamc@247: end adamc@271: | DDatabase _ => env adamc@461: | DCookie (x, n, c, s) => adamc@461: let adamc@461: val t = (CApp ((CFfi ("Basis", "http_cookie"), loc), c), loc) adamc@461: in adamc@461: pushENamed env x n t NONE s adamc@461: end adamc@16: adamc@193: fun patBinds env (p, loc) = adamc@193: case p of adamc@193: PWild => env adamc@193: | PVar (x, t) => pushERel env x t adamc@193: | PPrim _ => env adamc@193: | PCon (_, _, _, NONE) => env adamc@193: | PCon (_, _, _, SOME p) => patBinds env p adamc@193: | PRecord xps => foldl (fn ((_, p, _), env) => patBinds env p) env xps adamc@193: adamc@16: end