<commit_before>//
// AggregateDef.scala -- Scala class AggregateDef
// Project OrcScala
//
// $Id$
//
// Created by dkitchin on Jun 3, 2010.
//
// Copyright (c) 2010 The University of Texas at Austin. All rights reserved.
//
// Use and redistribution of this file is governed by the license terms in
// the LICENSE file found in the project's top-level directory and also found at
// URL: http://orc.csres.utexas.edu/license.shtml .
//

package orc.compile.translate

import orc.OptionMapExtension._
import orc.compile.ext._
import orc.oil.named

class AggregateDef(clauses: List[Clause], 
		           typeformals: Option[List[String]], 
		           argtypes: Option[List[Type]], 
		           returntype: Option[Type]) extends orc.AST {
	
		def unify[A](x: Option[A], y: Option[A], err: => Nothing): Option[A] = 
		  (x,y) match {
			case (None, None) => None
			case (Some(x), None) => Some(x)
			case (None, Some(y)) => Some(y)
			case (Some(x), Some(y)) => err
		  }
		def unifyList[A](x: Option[List[A]], y: Option[List[A]], err: => Nothing): Option[List[A]] = 
          (x,y) match {
            case (None, None) => None
            case (Some(x), None) => Some(x)
            case (None, Some(y)) => Some(y)
            case (Some(Nil), Some(Nil)) => Some(Nil) // Nils are allowed to unify
            case (Some(x), Some(y)) => err
          }
	
		def +(defn: DefDeclaration): AggregateDef =
		  defn -> {
            case Def(_, List(formals), maybeReturnType, body) => {
              val (newformals, maybeArgTypes) = AggregateDef.formalsPartition(formals)
              val newclause = defn ->> Clause(newformals, body)
              val newArgTypes = unifyList(argtypes, maybeArgTypes, defn !! "Redundant argument typing")
              val newReturnType = unify(returntype, maybeReturnType, defn !! "Redundant return typing")
              new AggregateDef(clauses ::: List(newclause), typeformals, newArgTypes, newReturnType)
            }
            case DefCapsule(name, List(formals), maybeReturnType, body) => {
              this + Def(name, List(formals), maybeReturnType, new Capsule(body))
            }
            case DefSig(_, typeformals2, argtypes2, maybeReturnType) => {
              val argtypes3 = argtypes2 head // List[List[Type]] has only one entry
              val newTypeFormals = unifyList(typeformals, Some(typeformals2), defn !! "Redundant type parameters")
              val newArgTypes = unifyList(argtypes, Some(argtypes3), defn !! "Redundant argument typing")
              val newReturnType = unify(returntype, Some(maybeReturnType), defn !! "Redundant return typing")
              new AggregateDef(clauses, newTypeFormals, newArgTypes, newReturnType)
            }
		  }
		
		def +(lambda: Lambda): AggregateDef = {
			val (newformals, maybeArgTypes) = AggregateDef.formalsPartition(lambda.formals.head)
			val newclause = lambda ->> Clause(newformals, lambda.body)
			val newArgTypes = unifyList(argtypes, maybeArgTypes, lambda !! "Redundant argument typing")
			val newReturnType = unify(returntype, lambda.returntype, lambda !! "Redundant return typing")
			new AggregateDef(clauses ::: List(newclause), typeformals, newArgTypes, newReturnType)
		}
			
		def convert(x : named.BoundVar): named.Def = {
			if (clauses.isEmpty) { this !! "Unused function signature" }
			val (newformals, newbody) = Clause.convertClauses(clauses)
			
			val subs = for (tf <- typeformals.getOrElse(Nil)) yield (new named.BoundTypevar(), tf)
            val newTypeFormals = for ((u,_) <- subs) yield u
            val newArgTypes = argtypes map { _ map { Translator.convertType(_).substAllTypes(subs) } }
            val newReturnType = returntype map { Translator.convertType(_).substAllTypes(subs) }
            val newerbody = newbody.substAllTypes(subs)
						 
			named.Def(x, newformals, newerbody, newTypeFormals, newArgTypes, newReturnType)
		}
			
		
		
		def capsuleCheck {
		  var existsCapsule = false
		  var existsNotCapsule = false
		    for (aclause <- clauses) {
			  aclause match {
			    case Clause(_, Capsule(_)) => 
				  if (existsNotCapsule) { aclause !! "This function is not declared as a capsule." } 
				  else existsCapsule = true
			    case _ => 
				  if (existsCapsule) { aclause !! "This function is already declared as a capsule."}
				  else existsNotCapsule = true
              }
            }
	    }
		
}

object AggregateDef {
	
	def formalsPartition(formals: List[Pattern]): (List[Pattern], Option[List[Type]]) = {
		val maybePartitioned = 
			formals optionMap {
				case TypedPattern(p, t) => Some(p, t)
				case _ => None
			}
		maybePartitioned match {
			case Some(l) => {
				val (ps, ts) = l.unzip
				(ps, Some(ts))
			}
			case None => (formals, None) 
		}
	}
	
	val empty = new AggregateDef(Nil, None, None, None)
	
	def apply(defn : DefDeclaration) = defn -> {empty + _}
	def apply(lambda: Lambda) = lambda -> {empty + _}
	
}



<commit_msg>Fixed import typo.<commit_after>//
// AggregateDef.scala -- Scala class AggregateDef
// Project OrcScala
//
// $Id$
//
// Created by dkitchin on Jun 3, 2010.
//
// Copyright (c) 2010 The University of Texas at Austin. All rights reserved.
//
// Use and redistribution of this file is governed by the license terms in
// the LICENSE file found in the project's top-level directory and also found at
// URL: http://orc.csres.utexas.edu/license.shtml .
//

package orc.compile.translate

import orc.util.OptionMapExtension._
import orc.compile.ext._
import orc.oil.named

class AggregateDef(clauses: List[Clause], 
		           typeformals: Option[List[String]], 
		           argtypes: Option[List[Type]], 
		           returntype: Option[Type]) extends orc.AST {
	
		def unify[A](x: Option[A], y: Option[A], err: => Nothing): Option[A] = 
		  (x,y) match {
			case (None, None) => None
			case (Some(x), None) => Some(x)
			case (None, Some(y)) => Some(y)
			case (Some(x), Some(y)) => err
		  }
		def unifyList[A](x: Option[List[A]], y: Option[List[A]], err: => Nothing): Option[List[A]] = 
          (x,y) match {
            case (None, None) => None
            case (Some(x), None) => Some(x)
            case (None, Some(y)) => Some(y)
            case (Some(Nil), Some(Nil)) => Some(Nil) // Nils are allowed to unify
            case (Some(x), Some(y)) => err
          }
	
		def +(defn: DefDeclaration): AggregateDef =
		  defn -> {
            case Def(_, List(formals), maybeReturnType, body) => {
              val (newformals, maybeArgTypes) = AggregateDef.formalsPartition(formals)
              val newclause = defn ->> Clause(newformals, body)
              val newArgTypes = unifyList(argtypes, maybeArgTypes, defn !! "Redundant argument typing")
              val newReturnType = unify(returntype, maybeReturnType, defn !! "Redundant return typing")
              new AggregateDef(clauses ::: List(newclause), typeformals, newArgTypes, newReturnType)
            }
            case DefCapsule(name, List(formals), maybeReturnType, body) => {
              this + Def(name, List(formals), maybeReturnType, new Capsule(body))
            }
            case DefSig(_, typeformals2, argtypes2, maybeReturnType) => {
              val argtypes3 = argtypes2 head // List[List[Type]] has only one entry
              val newTypeFormals = unifyList(typeformals, Some(typeformals2), defn !! "Redundant type parameters")
              val newArgTypes = unifyList(argtypes, Some(argtypes3), defn !! "Redundant argument typing")
              val newReturnType = unify(returntype, Some(maybeReturnType), defn !! "Redundant return typing")
              new AggregateDef(clauses, newTypeFormals, newArgTypes, newReturnType)
            }
		  }
		
		def +(lambda: Lambda): AggregateDef = {
			val (newformals, maybeArgTypes) = AggregateDef.formalsPartition(lambda.formals.head)
			val newclause = lambda ->> Clause(newformals, lambda.body)
			val newArgTypes = unifyList(argtypes, maybeArgTypes, lambda !! "Redundant argument typing")
			val newReturnType = unify(returntype, lambda.returntype, lambda !! "Redundant return typing")
			new AggregateDef(clauses ::: List(newclause), typeformals, newArgTypes, newReturnType)
		}
			
		def convert(x : named.BoundVar): named.Def = {
			if (clauses.isEmpty) { this !! "Unused function signature" }
			val (newformals, newbody) = Clause.convertClauses(clauses)
			
			val subs = for (tf <- typeformals.getOrElse(Nil)) yield (new named.BoundTypevar(), tf)
            val newTypeFormals = for ((u,_) <- subs) yield u
            val newArgTypes = argtypes map { _ map { Translator.convertType(_).substAllTypes(subs) } }
            val newReturnType = returntype map { Translator.convertType(_).substAllTypes(subs) }
            val newerbody = newbody.substAllTypes(subs)
						 
			named.Def(x, newformals, newerbody, newTypeFormals, newArgTypes, newReturnType)
		}
			
		
		
		def capsuleCheck {
		  var existsCapsule = false
		  var existsNotCapsule = false
		    for (aclause <- clauses) {
			  aclause match {
			    case Clause(_, Capsule(_)) => 
				  if (existsNotCapsule) { aclause !! "This function is not declared as a capsule." } 
				  else existsCapsule = true
			    case _ => 
				  if (existsCapsule) { aclause !! "This function is already declared as a capsule."}
				  else existsNotCapsule = true
              }
            }
	    }
		
}

object AggregateDef {
	
	def formalsPartition(formals: List[Pattern]): (List[Pattern], Option[List[Type]]) = {
		val maybePartitioned = 
			formals optionMap {
				case TypedPattern(p, t) => Some(p, t)
				case _ => None
			}
		maybePartitioned match {
			case Some(l) => {
				val (ps, ts) = l.unzip
				(ps, Some(ts))
			}
			case None => (formals, None) 
		}
	}
	
	val empty = new AggregateDef(Nil, None, None, None)
	
	def apply(defn : DefDeclaration) = defn -> {empty + _}
	def apply(lambda: Lambda) = lambda -> {empty + _}
	
}




{'fraction_non_alphanumeric': 0.1237593680372696, 'fraction_numerical': 0.0030382823577071094, 'mean_word_length': 2.8053949903660884, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4665033', 'n_tokens_mistral': 3478, 'n_tokens_neox': 3110, 'n_words': 820}