#<HTML><pre>
# KEHOME/bin/mkr2cyc
# Sep/15/2003

#####exec 2>&1  # send errors to stdout

# translate MKR propositions to CycL expressions
#
# Usage:
#	mkr2cyc [-q]
#	proposition
#	...
#	EOF
#
# proposition ::=
#   exit;
#   at view = mt;
#   do action od directobject with modifier from/to indirectobject done;
#   set variable op value;
#   subject predicate object mod1 mod2 mod3 mod4 mod5 mod6;
# predicate ::=
#   MKR verb
#   CycL predicate
#
# expression ::=
#   see below
#
# Notes:
# MKR commands for special CycL commands
#	do ke-create-now od "name" done;
#	do write-image to "world/name" done;
#	do generate-phrase from expression done;
# context for most questions and statements is
#	#$UniversalVocabularyMt
# MKR verb (is,has,do,rel,means,causes,...)
#	see below
# MKR characteristic (part,attribute,relation,action,interaction)
#	begins with letter
# MKR variable (attribute)
#	begins with letter
# MKR question variable
#	begins with "?"
#
# CycL predicate
#	begins with "#$"
# CycL constant
#	begins with "#$"
# CycL variable
#	begins with "?"

#echo "# mkr2cyc $@" >&2  # echo command line

if [ -n "$1" ]
then prompt=''		# mkr2cyc -q
else prompt='ke$ \c'	# mkr2cyc
fi

#####query="(CYC-QUERY '"
#####assert="(CYC-ASSERT '"
#####context=' #$UniversalVocabularyMt)'
inverse="no"

if [ -n "$prompt" ]
then echo -e "$prompt" >&2
fi
while read subject predicate object mod1 mod2 mod3 mod4 mod5 mod6; do

	##### context, command, assignment #####
	subject="${subject%;}"
	case "$subject" in
	\(*)	# ( CycL pass thru )
		echo "$subject $predicate $object $mod1 $mod2 $mod3 $mod4 $mod5 $mod6";
		continue;;
	exit)	# exit;
		echo "(exit)";
		break;;
	at)	mt=${mod1%;};
		if -n "$mod3"
		then  # at view = mt { proposition } ;
			proposition="${mod3%;}";
			echo "( '#\$ist' $mt $proposition )"
		else  # at view = mt ;
			echo "( WITH-MT $mt )"
		fi;
		continue;;
	do|hdo|vdo)
		# do action od direct with modifier from/to indirect done;
		# do ke-create-now od "name" done;
		# do write-image to "world/name" done;
		# do generate-phrase from expression done;
		verb="$subject";
		subject="cyc";
		action="${predicate%;}";
		direct="${mod1%;}";
		modifier="${mod3%;}";
		fromto="${mod4%;}";
		indirect="${mod5%;}";
		echo "( $action $direct $modifier $indirect )";
		continue;;
	set|unset|vset)
		# set variable op value;
		verb="$subject";
		subject="cyc";
		variable="${predicate%;}";
		op="${object%;}";
		value="${mod1%;}";
		echo "( $verb $variable $op $value )";
		continue;;

	*)	# subject predicate object;
		predicate="${predicate%;}";
		object="${object%;}";
		value="${mod2%;}";;
	esac

	##### question, statement #####
	prefix="$assert"
	case "$subject" in
	\?*) prefix="$query";;
	esac
	case "$object" in
	\?*) prefix="$query";;
	esac
	suffix="$context"

	#####echo "# INFO: mkr2cyc: ( $predicate $subject $object )" >&2

	case "$predicate" in
	##### hierarchy verbs #####
	isa)	echo "$subject iss $object" | mkr2cyc -q;
		echo "$subject isu $object" | mkr2cyc -q;
		continue;;
	iss)	predicate='#$genls';;
	isu)	predicate='#$isa';;
	isc)	echo "$subject isg $object" | mkr2cyc -q;
		echo "$subject isp $object" | mkr2cyc -q;
		continue;;
	isg)	predicate='#$specs';;
	isp)	predicate='#$instances';;

	isa\*)	echo "$subject iss* $object" | mkr2cyc -q;
		echo "$subject isu* $object" | mkr2cyc -q;
		continue;;
	iss\*)	predicate='#$all-genls';;
	isu\*)	predicate='#$all-isa';;
	isc\*)	echo "$subject isg* $object" | mkr2cyc -q;
		echo "$subject isp* $object" | mkr2cyc -q;
		continue;;
	isg\*)	predicate='#$all-specs';;
	isp\*)	predicate='#$all-instances';;

	##### other MKR verbs #####
	haspart)predicate='#$physicalParts';;
	isapart)predicate='#$physicalParts';;
	ismem)	predicate='#$groupMembers';;
	isall)	predicate='#$groupMembers';;
	isalt)	predicate='#$groupMembers';;
	isany)	predicate='#$groupMembers';;

	is)	if [ -n "$mod1" ]  # "with"
		then  # subject is genus with differentia ;
			genus="$object";
			diff="$mod2 $mod3 $mod4 $mod5 $mod6";
			diff="${diff%;}"
			echo "( '#\$defnIff' $subject ('#\$and'";
			echo "  ( '#\$genls' $subject $genus }";
			echo "  ( $diff )))"
		else  # subject is object;
			case "$object" in
			\?) echo "( DEFINING-DEFNS '( $subject $mt ) )";;
			*)  echo "( equals  $subject $object )";;
			esac;
		fi;
		continue;;
	has)	case "$object" in
		\?) echo "( ALL-TERM-ASSERTIONS '( $subject T ))";;
		*)  echo "( $object $subject $value )";;
		esac;
		continue;;
	do)	predicate="$object"; object="$value";;
	rel)	predicate="$object"; object="$value";;
	isin)	predicate="$object"; object="$value";;

	means)	 predicate="signifies";;
	isref)	 predicate="signifies"; inverse="yes";;
	causes)	 predicate="causedBy"; inverse="yes";;
	causedBy)predicate="causedBy";;

	##### other predicates #####
	genlmt)	predicate='#$genlMt';;
	specmt)	predicate='#$genlMt'; inverse="yes";;
	*)	;;
	esac  # end case "$predicate"

	case "$inverse" in
	yes) temp="$subject"; subject="$object"; object="$temp";;
	esac
	#####echo "$prefix ($predicate $subject $object) $suffix"
	echo "($predicate $subject $object)"

	if [ -n "$prompt" ]
	then echo -e "$prompt" >&2
	fi
done

#</pre></HTML>
