(* HCoop Domtool (http://hcoop.sourceforge.net/) * Copyright (c) 2006, Adam Chlipala * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *) (* Domtool type-checking and reduction environments *) structure Env :> ENV = struct open Ast structure SS = StringSet structure SM = StringMap type typeRule = exp -> bool val typeRules : typeRule SM.map ref = ref SM.empty fun registerType (name, rule) = typeRules := SM.insert (!typeRules, name, rule) fun typeRule name = SM.find (!typeRules, name) type env_vars = exp SM.map type action = env_vars * Ast.exp list -> env_vars val actions : action SM.map ref = ref SM.empty fun registerAction (name, action) = actions := SM.insert (!actions, name, action) fun action name = SM.find (!actions, name) val containers : (action * (unit -> unit)) SM.map ref = ref SM.empty fun registerContainer (name, befor, after) = containers := SM.insert (!containers, name, (befor, after)) fun container name = SM.find (!containers, name) fun badArgs (name, args) = (print ("Invalid arguments to " ^ name ^ "\n"); app (fn arg => Print.preface ("Argument: ", Print.p_exp arg)) args; raise Domain) fun badArg (func, arg, v) = (print ("Invalid " ^ arg ^ " argument to " ^ func ^ "\n"); Print.preface ("Argument: ", Print.p_exp v); raise Domain) type 'a arg = exp -> 'a option fun int (EInt n, _) = SOME n | int _ = NONE fun string (EString s, _) = SOME s | string _ = NONE fun mapFail f [] = SOME [] | mapFail f (h :: t) = case f h of NONE => NONE | SOME h' => case mapFail f t of NONE => NONE | SOME t' => SOME (h' :: t') fun list f (EList ls, _) = mapFail f ls | list _ _ = NONE fun one func (name, arg) f (_, [e]) = (case arg e of NONE => badArg (func, name, e) | SOME v => (f v; SM.empty)) | one func _ _ (_, es) = badArgs (func, es) fun two func (name1, arg1, name2, arg2) f (_, [e1, e2]) = (case (arg1 e1, arg2 e2) of (NONE, _) => badArg (func, name1, e1) | (_, NONE) => badArg (func, name2, e2) | (SOME v1, SOME v2) => (f (v1, v2); SM.empty)) | two func _ _ (_, es) = badArgs (func, es) fun type_one func arg f = registerType (func, fn e => case arg e of NONE => false | SOME v => f v) fun action_one name args f = registerAction (name, one name args f) fun action_two name args f = registerAction (name, two name args f) fun container_one name args (f, g) = registerContainer (name, one name args f, g) type env = SS.set * (typ * exp option) SM.map * SS.set val empty : env = (SS.empty, SM.empty, SS.empty) fun lookupType (ts, _, _) name = SS.member (ts, name) fun lookupVal (_, vs, _) name = case SM.find (vs, name) of NONE => NONE | SOME (t, _) => SOME t fun lookupEquation (_, vs, _) name = case SM.find (vs, name) of NONE => NONE | SOME (_, eqo) => eqo fun lookupContext (_, _, cs) name = SS.member (cs, name) fun bindType (ts, vs, cs) name = (SS.add (ts, name), vs, cs) fun bindVal (ts, vs, cs) (name, t, eqo) = (ts, SM.insert (vs, name, (t, eqo)), cs) fun bindContext (ts, vs, cs) name = (ts, vs, SS.add (cs, name)) fun types (ts, _, _) = ts fun vals (_, vs, _) = SM.foldli (fn (name, _, vs) => SS.add (vs, name)) SS.empty vs fun contexts (_, _, cs) = cs end