/* * Function.java * Copyright (C) 2004-2008, Bjorn Bringert (bringert@cs.chalmers.se) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package se.chalmers.cs.pgf; import se.chalmers.cs.pgf.raw.*; import java.util.*; /** * Represents a PGF abstract syntax function. */ public class Function { private String name; private FunType funType; public Function(String name, Term type) { this.name = name; this.funType = termToFunType(type); } /** * Get the name of this function. */ public String getName() { return name; } /** * Get the argument types of this function. */ public List getArgCats() { return funType.argCats; } /** * Get one of the argument types of this function. */ public String getArgCat(int i) { return funType.argCats.get(i); } /** * Get the result type of this function */ public String getResultCat() { return funType.resultCat; } /** * Get the number of arguments that this function takes. */ public int getArity() { return funType.argCats.size(); } public boolean equals(Object o) { return o instanceof Function && equals((Function)o); } public boolean equals(Function f) { return name.equals(f.name) && funType.argCats.equals(f.funType.argCats) && funType.resultCat.equals(f.funType.resultCat); } public int hashCode() { return name.hashCode() + funType.argCats.hashCode() + funType.resultCat.hashCode(); } public String toString() { StringBuilder sb = new StringBuilder(); sb.append("fun "); sb.append(name); sb.append(" : "); for (String c : funType.argCats) sb.append(c).append(" -> "); sb.append(funType.resultCat); return sb.toString(); } /* GF: That : Kind -> Item ; PGF: (That (Item (H (_ (Kind H X))) X) Eq) */ private static FunType termToFunType(Term term) { Fun f = (Fun)term; Fun t = (Fun)f.listterm_[0]; List args = new LinkedList(); for (Term h : t.listterm_) { FunType argFunType = termToFunType(((Fun)h).listterm_[0]); args.add(argFunType.resultCat); } return new FunType(args, f.id_); } private static class FunType { public List argCats; public String resultCat; public FunType(List argCats, String resultCat) { this.argCats = argCats; this.resultCat = resultCat; } } }